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 */
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 /* Whether or not a TYPE_CODE_UNION value is an untagged union
97 as opposed to being a regular Rust enum. */
99 rust_union_is_untagged (struct type
*type
)
101 /* Unions must have at least one field. */
102 if (TYPE_NFIELDS (type
) == 0)
104 /* If the first field is named, but the name has the rust enum prefix,
106 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
107 strlen (RUST_ENUM_PREFIX
)) == 0)
109 /* Unions only have named fields. */
110 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
112 if (strlen (TYPE_FIELD_NAME (type
, i
)) == 0)
118 /* Utility function to get discriminant info for a given value. */
120 static struct disr_info
121 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
122 int embedded_offset
, CORE_ADDR address
,
126 struct disr_info ret
;
127 struct type
*disr_type
;
128 struct ui_file
*temp_file
;
129 struct value_print_options opts
;
130 struct cleanup
*cleanup
;
131 const char *name_segment
;
133 get_no_prettyformat_print_options (&opts
);
138 if (TYPE_NFIELDS (type
) == 0)
139 error (_("Encountered void enum value"));
141 /* If an enum has two values where one is empty and the other holds
142 a pointer that cannot be zero; then the Rust compiler optimizes
143 away the discriminant and instead uses a zero value in the
144 pointer field to indicate the empty variant. */
145 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
146 strlen (RUST_ENUM_PREFIX
)) == 0)
148 char *tail
, *token
, *name
, *saveptr
= NULL
;
149 unsigned long fieldno
;
150 struct type
*member_type
;
155 if (TYPE_NFIELDS (type
) != 1)
156 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
158 /* Optimized enums have only one field. */
159 member_type
= TYPE_FIELD_TYPE (type
, 0);
161 name
= xstrdup (TYPE_FIELD_NAME (type
, 0));
162 cleanup
= make_cleanup (xfree
, name
);
163 tail
= name
+ strlen (RUST_ENUM_PREFIX
);
165 /* The location of the value that doubles as a discriminant is
166 stored in the name of the field, as
167 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
168 where the fieldnos are the indices of the fields that should be
169 traversed in order to find the field (which may be several fields deep)
170 and the variantname is the name of the variant of the case when the
172 for (token
= strtok_r (tail
, "$", &saveptr
);
174 token
= strtok_r (NULL
, "$", &saveptr
))
176 if (sscanf (token
, "%lu", &fieldno
) != 1)
178 /* We have reached the enum name, which cannot start
182 if (fieldno
>= TYPE_NFIELDS (member_type
))
183 error (_("%s refers to field after end of member type"),
186 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
187 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
191 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
192 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
196 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
197 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + token
;
201 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
202 ret
.name
= (std::string (TYPE_NAME (type
)) + "::"
203 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))));
206 do_cleanups (cleanup
);
210 disr_type
= TYPE_FIELD_TYPE (type
, 0);
212 if (TYPE_NFIELDS (disr_type
) == 0)
214 /* This is a bounds check and should never be hit unless Rust
215 has changed its debuginfo format. */
216 error (_("Could not find enum discriminant field"));
218 else if (TYPE_NFIELDS (type
) == 1)
220 /* Sometimes univariant enums are encoded without a
221 discriminant. In that case, treating it as an encoded enum
222 with the first field being the actual type works. */
223 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
224 const char *last
= rust_last_path_segment (field_name
);
225 ret
.name
= concat (TYPE_NAME (type
), "::", last
, (char *) NULL
);
226 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
231 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
232 error (_("Rust debug format has changed"));
234 temp_file
= mem_fileopen ();
235 cleanup
= make_cleanup_ui_file_delete (temp_file
);
236 /* The first value of the first field (or any field)
237 is the discriminant value. */
238 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0),
239 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
240 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
244 ret
.name
= ui_file_as_string (temp_file
);
245 name_segment
= rust_last_path_segment (ret
.name
.c_str ());
246 if (name_segment
!= NULL
)
248 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
250 /* Sadly, the discriminant value paths do not match the type
251 field name paths ('core::option::Option::Some' vs
252 'core::option::Some'). However, enum variant names are
253 unique in the last path segment and the generics are not
254 part of this path, so we can just compare those. This is
255 hackish and would be better fixed by improving rustc's
256 metadata for enums. */
257 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
259 if (field_type
!= NULL
260 && strcmp (name_segment
,
261 rust_last_path_segment (field_type
)) == 0)
269 if (ret
.field_no
== -1 && !ret
.name
.empty ())
271 /* Somehow the discriminant wasn't found. */
272 error (_("Could not find variant of %s with discriminant %s"),
273 TYPE_TAG_NAME (type
), ret
.name
.c_str ());
276 do_cleanups (cleanup
);
280 /* See rust-lang.h. */
283 rust_tuple_type_p (struct type
*type
)
285 /* The current implementation is a bit of a hack, but there's
286 nothing else in the debuginfo to distinguish a tuple from a
288 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
289 && TYPE_TAG_NAME (type
) != NULL
290 && TYPE_TAG_NAME (type
)[0] == '(');
294 /* Return true if all non-static fields of a structlike type are in a
295 sequence like __0, __1, __2. OFFSET lets us skip fields. */
298 rust_underscore_fields (struct type
*type
, int offset
)
304 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
306 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
308 if (!field_is_static (&TYPE_FIELD (type
, i
)))
316 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
317 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
326 /* See rust-lang.h. */
329 rust_tuple_struct_type_p (struct type
*type
)
331 /* This is just an approximation until DWARF can represent Rust more
332 precisely. We exclude zero-length structs because they may not
333 be tuple structs, and there's no way to tell. */
334 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
337 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
340 rust_tuple_variant_type_p (struct type
*type
)
342 /* First field is discriminant */
343 return rust_underscore_fields (type
, 1);
346 /* Return true if TYPE is a slice type, otherwise false. */
349 rust_slice_type_p (struct type
*type
)
351 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
352 && TYPE_TAG_NAME (type
) != NULL
353 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
356 /* Return true if TYPE is a range type, otherwise false. */
359 rust_range_type_p (struct type
*type
)
363 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
364 || TYPE_NFIELDS (type
) > 2
365 || TYPE_TAG_NAME (type
) == NULL
366 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
369 if (TYPE_NFIELDS (type
) == 0)
373 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
375 if (TYPE_NFIELDS (type
) == 1)
379 else if (TYPE_NFIELDS (type
) == 2)
381 /* First field had to be "start". */
385 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
388 /* Return true if TYPE seems to be the type "u8", otherwise false. */
391 rust_u8_type_p (struct type
*type
)
393 return (TYPE_CODE (type
) == TYPE_CODE_INT
394 && TYPE_UNSIGNED (type
)
395 && TYPE_LENGTH (type
) == 1);
398 /* Return true if TYPE is a Rust character type. */
401 rust_chartype_p (struct type
*type
)
403 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
404 && TYPE_LENGTH (type
) == 4
405 && TYPE_UNSIGNED (type
));
410 /* la_emitchar implementation for Rust. */
413 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
415 if (!rust_chartype_p (type
))
416 generic_emit_char (c
, type
, stream
, quoter
,
417 target_charset (get_type_arch (type
)));
418 else if (c
== '\\' || c
== quoter
)
419 fprintf_filtered (stream
, "\\%c", c
);
421 fputs_filtered ("\\n", stream
);
423 fputs_filtered ("\\r", stream
);
425 fputs_filtered ("\\t", stream
);
427 fputs_filtered ("\\0", stream
);
428 else if (c
>= 32 && c
<= 127 && isprint (c
))
429 fputc_filtered (c
, stream
);
431 fprintf_filtered (stream
, "\\x%02x", c
);
433 fprintf_filtered (stream
, "\\u{%06x}", c
);
436 /* la_printchar implementation for Rust. */
439 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
441 fputs_filtered ("'", stream
);
442 LA_EMIT_CHAR (c
, type
, stream
, '\'');
443 fputs_filtered ("'", stream
);
446 /* la_printstr implementation for Rust. */
449 rust_printstr (struct ui_file
*stream
, struct type
*type
,
450 const gdb_byte
*string
, unsigned int length
,
451 const char *user_encoding
, int force_ellipses
,
452 const struct value_print_options
*options
)
454 /* Rust always uses UTF-8, but let the caller override this if need
456 const char *encoding
= user_encoding
;
457 if (user_encoding
== NULL
|| !*user_encoding
)
459 /* In Rust strings, characters are "u8". */
460 if (rust_u8_type_p (type
))
464 /* This is probably some C string, so let's let C deal with
466 c_printstr (stream
, type
, string
, length
, user_encoding
,
467 force_ellipses
, options
);
472 /* This is not ideal as it doesn't use our character printer. */
473 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
479 /* rust_print_type branch for structs and untagged unions. */
482 val_print_struct (struct type
*type
, int embedded_offset
,
483 CORE_ADDR address
, struct ui_file
*stream
,
484 int recurse
, struct value
*val
,
485 const struct value_print_options
*options
)
489 int is_tuple
= rust_tuple_type_p (type
);
490 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
491 struct value_print_options opts
;
495 if (TYPE_TAG_NAME (type
) != NULL
)
496 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
498 if (TYPE_NFIELDS (type
) == 0)
501 if (TYPE_TAG_NAME (type
) != NULL
)
502 fputs_filtered (" ", stream
);
505 if (is_tuple
|| is_tuple_struct
)
506 fputs_filtered ("(", stream
);
508 fputs_filtered ("{", stream
);
514 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
516 if (field_is_static (&TYPE_FIELD (type
, i
)))
520 fputs_filtered (",", stream
);
522 if (options
->prettyformat
)
524 fputs_filtered ("\n", stream
);
525 print_spaces_filtered (2 + 2 * recurse
, stream
);
527 else if (!first_field
)
528 fputs_filtered (" ", stream
);
532 if (!is_tuple
&& !is_tuple_struct
)
534 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
535 fputs_filtered (": ", stream
);
538 val_print (TYPE_FIELD_TYPE (type
, i
),
539 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
541 stream
, recurse
+ 1, val
, &opts
,
545 if (options
->prettyformat
)
547 fputs_filtered ("\n", stream
);
548 print_spaces_filtered (2 * recurse
, stream
);
551 if (is_tuple
|| is_tuple_struct
)
552 fputs_filtered (")", stream
);
554 fputs_filtered ("}", stream
);
557 static const struct generic_val_print_decorations rust_decorations
=
559 /* Complex isn't used in Rust, but we provide C-ish values just in
571 /* la_val_print implementation for Rust. */
574 rust_val_print (struct type
*type
, int embedded_offset
,
575 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
577 const struct value_print_options
*options
)
579 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
581 type
= check_typedef (type
);
582 switch (TYPE_CODE (type
))
586 LONGEST low_bound
, high_bound
;
588 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
589 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
590 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
592 /* We have a pointer to a byte string, so just print
594 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
596 struct gdbarch
*arch
= get_type_arch (type
);
597 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
599 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
600 if (options
->addressprint
)
602 fputs_filtered (paddress (arch
, addr
), stream
);
603 fputs_filtered (" ", stream
);
606 fputs_filtered ("b", stream
);
607 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
608 high_bound
- low_bound
+ 1, stream
,
615 case TYPE_CODE_METHODPTR
:
616 case TYPE_CODE_MEMBERPTR
:
617 c_val_print (type
, embedded_offset
, address
, stream
,
618 recurse
, val
, options
);
622 /* Recognize the unit type. */
623 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
624 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
626 fputs_filtered ("()", stream
);
631 case TYPE_CODE_STRING
:
633 struct gdbarch
*arch
= get_type_arch (type
);
634 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
635 LONGEST low_bound
, high_bound
;
637 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
638 error (_("Could not determine the array bounds"));
640 /* If we see a plain TYPE_CODE_STRING, then we're printing a
641 byte string, hence the choice of "ASCII" as the
643 fputs_filtered ("b", stream
);
644 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
645 valaddr
+ embedded_offset
* unit_size
,
646 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
650 case TYPE_CODE_ARRAY
:
652 LONGEST low_bound
, high_bound
;
654 if (get_array_bounds (type
, &low_bound
, &high_bound
)
655 && high_bound
- low_bound
+ 1 == 0)
656 fputs_filtered ("[]", stream
);
662 case TYPE_CODE_UNION
:
664 int j
, nfields
, first_field
, is_tuple
, start
;
665 struct type
*variant_type
;
666 struct disr_info disr
;
667 struct value_print_options opts
;
669 /* Untagged unions are printed as if they are structs.
670 Since the field bit positions overlap in the debuginfo,
671 the code for printing a union is same as that for a struct,
672 the only difference is that the input type will have overlapping
674 if (rust_union_is_untagged (type
))
676 val_print_struct (type
, embedded_offset
, address
, stream
,
677 recurse
, val
, options
);
684 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
687 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
689 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
694 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
695 nfields
= TYPE_NFIELDS (variant_type
);
697 is_tuple
= (disr
.is_encoded
698 ? rust_tuple_struct_type_p (variant_type
)
699 : rust_tuple_variant_type_p (variant_type
));
700 start
= disr
.is_encoded
? 0 : 1;
704 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
706 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
709 /* struct variant. */
710 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
715 /* In case of a nullary variant like 'None', just output
717 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
721 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
724 fputs_filtered (", ", stream
);
728 fprintf_filtered (stream
, "%s: ",
729 TYPE_FIELD_NAME (variant_type
, j
));
731 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
733 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
734 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
736 stream
, recurse
+ 1, val
, &opts
,
741 fputs_filtered (")", stream
);
743 fputs_filtered ("}", stream
);
747 case TYPE_CODE_STRUCT
:
748 val_print_struct (type
, embedded_offset
, address
, stream
,
749 recurse
, val
, options
);
754 /* Nothing special yet. */
755 generic_val_print (type
, embedded_offset
, address
, stream
,
756 recurse
, val
, options
, &rust_decorations
);
763 rust_print_type (struct type
*type
, const char *varstring
,
764 struct ui_file
*stream
, int show
, int level
,
765 const struct type_print_options
*flags
);
767 /* Print a struct or union typedef. */
769 rust_print_struct_def (struct type
*type
, const char *varstring
,
770 struct ui_file
*stream
, int show
, int level
,
771 const struct type_print_options
*flags
)
773 int is_tuple_struct
, i
;
775 /* Print a tuple type simply. */
776 if (rust_tuple_type_p (type
))
778 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
782 /* If we see a base class, delegate to C. */
783 if (TYPE_N_BASECLASSES (type
) > 0)
784 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
786 /* This code path is also used by unions. */
787 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
788 fputs_filtered ("struct ", stream
);
790 fputs_filtered ("union ", stream
);
792 if (TYPE_TAG_NAME (type
) != NULL
)
793 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
795 is_tuple_struct
= rust_tuple_struct_type_p (type
);
797 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
799 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
801 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
806 if (field_is_static (&TYPE_FIELD (type
, i
)))
809 /* We'd like to print "pub" here as needed, but rustc
810 doesn't emit the debuginfo, and our types don't have
811 cplus_struct_type attached. */
813 /* For a tuple struct we print the type but nothing
815 print_spaces_filtered (level
+ 2, stream
);
816 if (!is_tuple_struct
)
817 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
819 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
820 stream
, show
- 1, level
+ 2,
822 fputs_filtered (",\n", stream
);
825 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
828 /* la_print_typedef implementation for Rust. */
831 rust_print_typedef (struct type
*type
,
832 struct symbol
*new_symbol
,
833 struct ui_file
*stream
)
835 type
= check_typedef (type
);
836 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
837 type_print (type
, "", stream
, 0);
838 fprintf_filtered (stream
, ";\n");
841 /* la_print_type implementation for Rust. */
844 rust_print_type (struct type
*type
, const char *varstring
,
845 struct ui_file
*stream
, int show
, int level
,
846 const struct type_print_options
*flags
)
852 && TYPE_NAME (type
) != NULL
)
854 /* Rust calls the unit type "void" in its debuginfo,
855 but we don't want to print it as that. */
856 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
857 fputs_filtered ("()", stream
);
859 fputs_filtered (TYPE_NAME (type
), stream
);
863 type
= check_typedef (type
);
864 switch (TYPE_CODE (type
))
867 fputs_filtered ("()", stream
);
871 /* Delegate varargs to the C printer. */
872 if (TYPE_VARARGS (type
))
875 fputs_filtered ("fn ", stream
);
876 if (varstring
!= NULL
)
877 fputs_filtered (varstring
, stream
);
878 fputs_filtered ("(", stream
);
879 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
883 fputs_filtered (", ", stream
);
884 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
887 fputs_filtered (")", stream
);
888 /* If it returns unit, we can omit the return type. */
889 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
891 fputs_filtered (" -> ", stream
);
892 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
896 case TYPE_CODE_ARRAY
:
898 LONGEST low_bound
, high_bound
;
900 fputs_filtered ("[", stream
);
901 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
902 stream
, show
- 1, level
, flags
);
903 fputs_filtered ("; ", stream
);
905 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
906 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
907 fprintf_filtered (stream
, "variable length");
908 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
909 fprintf_filtered (stream
, "%s",
910 plongest (high_bound
- low_bound
+ 1));
911 fputs_filtered ("]", stream
);
915 case TYPE_CODE_STRUCT
:
916 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
923 fputs_filtered ("enum ", stream
);
924 if (TYPE_TAG_NAME (type
) != NULL
)
926 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
927 fputs_filtered (" ", stream
);
928 len
= strlen (TYPE_TAG_NAME (type
));
930 fputs_filtered ("{\n", stream
);
932 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
934 const char *name
= TYPE_FIELD_NAME (type
, i
);
939 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
941 && name
[len
+ 1] == ':')
943 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
946 fputs_filtered ("}", stream
);
950 case TYPE_CODE_UNION
:
954 /* Skip the discriminant field. */
957 /* Unions and structs have the same syntax in Rust,
958 the only difference is that structs are declared with `struct`
959 and union with `union`. This difference is handled in the struct
961 if (rust_union_is_untagged (type
))
963 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
967 fputs_filtered ("enum ", stream
);
968 if (TYPE_TAG_NAME (type
) != NULL
)
970 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
971 fputs_filtered (" ", stream
);
973 fputs_filtered ("{\n", stream
);
975 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
976 strlen (RUST_ENUM_PREFIX
)) == 0)
978 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
979 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
981 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
982 /* There is no explicit discriminant field, skip nothing. */
987 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
989 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
991 = rust_last_path_segment (TYPE_NAME (variant_type
));
993 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
995 if (TYPE_NFIELDS (variant_type
) > skip_to
)
998 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
1001 fputs_filtered (is_tuple
? "(" : "{", stream
);
1002 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1007 fputs_filtered (", ", stream
);
1010 fprintf_filtered (stream
, "%s: ",
1011 TYPE_FIELD_NAME (variant_type
, j
));
1013 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1014 stream
, show
- 1, level
+ 2,
1017 fputs_filtered (is_tuple
? ")" : "}", stream
);
1020 fputs_filtered (",\n", stream
);
1023 fputs_filtered ("}", stream
);
1029 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1035 /* Compute the alignment of the type T. */
1038 rust_type_alignment (struct type
*t
)
1040 t
= check_typedef (t
);
1041 switch (TYPE_CODE (t
))
1044 error (_("Could not compute alignment of type"));
1047 case TYPE_CODE_ENUM
:
1051 case TYPE_CODE_CHAR
:
1052 case TYPE_CODE_BOOL
:
1053 return TYPE_LENGTH (t
);
1055 case TYPE_CODE_ARRAY
:
1056 case TYPE_CODE_COMPLEX
:
1057 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1059 case TYPE_CODE_STRUCT
:
1060 case TYPE_CODE_UNION
:
1065 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1067 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1076 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1077 -- either on an obstack or on a gdbarch. */
1079 static struct type
*
1080 rust_composite_type (struct type
*original
,
1082 const char *field1
, struct type
*type1
,
1083 const char *field2
, struct type
*type2
)
1085 struct type
*result
= alloc_type_copy (original
);
1086 int i
, nfields
, bitpos
;
1094 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1095 TYPE_NAME (result
) = name
;
1096 TYPE_TAG_NAME (result
) = name
;
1098 TYPE_NFIELDS (result
) = nfields
;
1099 TYPE_FIELDS (result
)
1100 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1106 struct field
*field
= &TYPE_FIELD (result
, i
);
1108 SET_FIELD_BITPOS (*field
, bitpos
);
1109 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1111 FIELD_NAME (*field
) = field1
;
1112 FIELD_TYPE (*field
) = type1
;
1117 struct field
*field
= &TYPE_FIELD (result
, i
);
1118 int align
= rust_type_alignment (type2
);
1124 align
*= TARGET_CHAR_BIT
;
1125 delta
= bitpos
% align
;
1127 bitpos
+= align
- delta
;
1129 SET_FIELD_BITPOS (*field
, bitpos
);
1131 FIELD_NAME (*field
) = field2
;
1132 FIELD_TYPE (*field
) = type2
;
1137 TYPE_LENGTH (result
)
1138 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1139 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1143 /* See rust-lang.h. */
1146 rust_slice_type (const char *name
, struct type
*elt_type
,
1147 struct type
*usize_type
)
1151 elt_type
= lookup_pointer_type (elt_type
);
1152 type
= rust_composite_type (elt_type
, name
,
1153 "data_ptr", elt_type
,
1154 "length", usize_type
);
1159 enum rust_primitive_types
1161 rust_primitive_bool
,
1162 rust_primitive_char
,
1171 rust_primitive_isize
,
1172 rust_primitive_usize
,
1175 rust_primitive_unit
,
1177 nr_rust_primitive_types
1180 /* la_language_arch_info implementation for Rust. */
1183 rust_language_arch_info (struct gdbarch
*gdbarch
,
1184 struct language_arch_info
*lai
)
1186 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1188 struct type
**types
;
1189 unsigned int length
;
1191 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1194 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1195 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1196 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1197 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1198 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1199 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1200 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1201 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1202 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1203 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1205 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1206 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1207 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1209 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1210 floatformats_ieee_single
);
1211 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1212 floatformats_ieee_double
);
1214 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1216 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1217 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1218 types
[rust_primitive_usize
]);
1220 lai
->primitive_type_vector
= types
;
1221 lai
->bool_type_default
= types
[rust_primitive_bool
];
1222 lai
->string_char_type
= types
[rust_primitive_u8
];
1227 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1229 static struct value
*
1230 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1233 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1235 struct value
*function
, *result
, *arg0
;
1236 struct type
*type
, *fn_type
;
1237 const struct block
*block
;
1238 struct block_symbol sym
;
1240 /* For an ordinary function call we can simply defer to the
1241 generic implementation. */
1242 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1243 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1245 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1247 method
= &exp
->elts
[*pos
+ 1].string
;
1248 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1250 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1251 type in order to look up the method. */
1252 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1254 if (noside
== EVAL_SKIP
)
1256 for (i
= 0; i
< num_args
; ++i
)
1257 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1261 std::vector
<struct value
*> args (num_args
+ 1);
1264 /* We don't yet implement real Deref semantics. */
1265 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1266 args
[0] = value_ind (args
[0]);
1268 type
= value_type (args
[0]);
1269 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1270 && TYPE_CODE (type
) != TYPE_CODE_UNION
1271 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1272 || rust_tuple_type_p (type
))
1273 error (_("Method calls only supported on struct or enum types"));
1274 if (TYPE_TAG_NAME (type
) == NULL
)
1275 error (_("Method call on nameless type"));
1277 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1279 block
= get_selected_block (0);
1280 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1281 if (sym
.symbol
== NULL
)
1282 error (_("Could not find function named '%s'"), name
.c_str ());
1284 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1285 if (TYPE_NFIELDS (fn_type
) == 0)
1286 error (_("Function '%s' takes no arguments"), name
.c_str ());
1288 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1289 args
[0] = value_addr (args
[0]);
1291 function
= address_of_variable (sym
.symbol
, block
);
1293 for (i
= 0; i
< num_args
; ++i
)
1294 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1296 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1297 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1299 result
= call_function_by_hand (function
, num_args
+ 1, args
.data ());
1303 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1305 static struct value
*
1306 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1308 enum range_type kind
;
1309 struct value
*low
= NULL
, *high
= NULL
;
1310 struct value
*addrval
, *result
;
1312 struct type
*range_type
;
1313 struct type
*index_type
;
1314 struct type
*temp_type
;
1317 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1320 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1321 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1322 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1323 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1325 if (noside
== EVAL_SKIP
)
1326 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1333 name
= "std::ops::RangeFull";
1337 index_type
= value_type (high
);
1338 name
= "std::ops::RangeTo";
1345 index_type
= value_type (low
);
1346 name
= "std::ops::RangeFrom";
1350 if (!types_equal (value_type (low
), value_type (high
)))
1351 error (_("Range expression with different types"));
1352 index_type
= value_type (low
);
1353 name
= "std::ops::Range";
1357 /* If we don't have an index type, just allocate this on the
1358 arch. Here any type will do. */
1359 temp_type
= (index_type
== NULL
1360 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1362 /* It would be nicer to cache the range type. */
1363 range_type
= rust_composite_type (temp_type
, name
,
1364 low
== NULL
? NULL
: "start", index_type
,
1365 high
== NULL
? NULL
: "end", index_type
);
1367 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1368 return value_zero (range_type
, lval_memory
);
1370 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1371 addr
= value_as_long (addrval
);
1372 result
= value_at_lazy (range_type
, addr
);
1376 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1379 value_assign (start
, low
);
1384 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1387 value_assign (end
, high
);
1390 result
= value_at_lazy (range_type
, addr
);
1394 /* A helper function to compute the range and kind given a range
1395 value. TYPE is the type of the range value. RANGE is the range
1396 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1397 parameters might be filled in, or might not be, depending on the
1398 kind of range this is. KIND will always be set to the appropriate
1399 value describing the kind of range, and this can be used to
1400 determine whether LOW or HIGH are valid. */
1403 rust_compute_range (struct type
*type
, struct value
*range
,
1404 LONGEST
*low
, LONGEST
*high
,
1405 enum range_type
*kind
)
1411 *kind
= BOTH_BOUND_DEFAULT
;
1413 if (TYPE_NFIELDS (type
) == 0)
1417 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1419 *kind
= HIGH_BOUND_DEFAULT
;
1420 *low
= value_as_long (value_field (range
, 0));
1423 if (TYPE_NFIELDS (type
) > i
1424 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1426 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1427 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1428 *high
= value_as_long (value_field (range
, i
));
1432 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1434 static struct value
*
1435 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1438 struct value
*lhs
, *rhs
, *result
;
1439 struct type
*rhstype
;
1440 LONGEST low
, high_bound
;
1441 /* Initialized to appease the compiler. */
1442 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1447 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1448 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1450 if (noside
== EVAL_SKIP
)
1453 rhstype
= check_typedef (value_type (rhs
));
1454 if (rust_range_type_p (rhstype
))
1457 error (_("Can't take slice of array without '&'"));
1458 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1462 low
= value_as_long (rhs
);
1464 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1466 struct type
*type
= check_typedef (value_type (lhs
));
1468 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1474 struct type
*type
= check_typedef (value_type (lhs
));
1476 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1479 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1480 error (_("Can't compute array bounds"));
1482 error (_("Found array with non-zero lower bound"));
1485 else if (rust_slice_type_p (type
))
1489 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1490 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1492 high_bound
= value_as_long (len
);
1494 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1498 high_bound
= LONGEST_MAX
;
1501 error (_("Cannot subscript non-array type"));
1504 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1507 error (_("Index less than zero"));
1508 if (low
> high_bound
)
1509 error (_("Index greater than length"));
1511 result
= value_subscript (base
, low
);
1518 struct type
*usize
, *slice
;
1520 struct value
*addrval
, *tem
;
1522 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1525 error (_("High index less than zero"));
1527 error (_("Low index greater than high index"));
1528 if (high
> high_bound
)
1529 error (_("High index greater than length"));
1531 usize
= language_lookup_primitive_type (exp
->language_defn
,
1534 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1537 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1538 addr
= value_as_long (addrval
);
1539 tem
= value_at_lazy (slice
, addr
);
1541 value_assign (value_field (tem
, 0), value_addr (result
));
1542 value_assign (value_field (tem
, 1),
1543 value_from_longest (usize
, high
- low
));
1545 result
= value_at_lazy (slice
, addr
);
1548 result
= value_addr (result
);
1554 /* evaluate_exp implementation for Rust. */
1556 static struct value
*
1557 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1558 int *pos
, enum noside noside
)
1560 struct value
*result
;
1562 switch (exp
->elts
[*pos
].opcode
)
1564 case UNOP_COMPLEMENT
:
1566 struct value
*value
;
1569 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1570 if (noside
== EVAL_SKIP
)
1572 /* Preserving the type is enough. */
1575 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1576 result
= value_from_longest (value_type (value
),
1577 value_logical_not (value
));
1579 result
= value_complement (value
);
1583 case BINOP_SUBSCRIPT
:
1584 result
= rust_subscript (exp
, pos
, noside
, 0);
1588 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1594 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1595 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1598 struct value
*addrval
= NULL
;
1602 if (noside
== EVAL_NORMAL
)
1604 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1605 addr
= value_as_long (addrval
);
1606 result
= value_at_lazy (type
, addr
);
1609 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1614 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1615 if (noside
== EVAL_NORMAL
)
1617 /* This isn't quite right but will do for the time
1618 being, seeing that we can't implement the Copy
1620 value_assign (result
, init
);
1626 gdb_assert (arglen
% 2 == 0);
1627 for (i
= 0; i
< arglen
; i
+= 2)
1630 const char *fieldname
;
1631 struct value
*value
, *field
;
1633 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1635 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1637 fieldname
= &exp
->elts
[*pos
].string
;
1638 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1640 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1641 if (noside
== EVAL_NORMAL
)
1643 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1645 value_assign (field
, value
);
1649 if (noside
== EVAL_SKIP
)
1650 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1652 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1653 result
= allocate_value (type
);
1655 result
= value_at_lazy (type
, addr
);
1664 struct value
*ncopies
;
1666 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1667 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1668 copies
= value_as_long (ncopies
);
1670 error (_("Array with negative number of elements"));
1672 if (noside
== EVAL_NORMAL
)
1676 std::vector
<struct value
*> eltvec (copies
);
1678 for (i
= 0; i
< copies
; ++i
)
1680 result
= value_array (0, copies
- 1, eltvec
.data ());
1684 struct type
*arraytype
1685 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1686 result
= allocate_value (arraytype
);
1691 case STRUCTOP_ANONYMOUS
:
1693 /* Anonymous field access, i.e. foo.1. */
1695 int pc
, field_number
, nfields
;
1696 struct type
*type
, *variant_type
;
1697 struct disr_info disr
;
1700 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1702 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1704 type
= value_type (lhs
);
1705 /* Untagged unions can't have anonymous field access since
1706 they can only have named fields. */
1707 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1708 && !rust_union_is_untagged (type
))
1710 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1711 value_embedded_offset (lhs
),
1712 value_address (lhs
), lhs
);
1714 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1716 variant_type
= NULL
;
1721 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1722 nfields
= TYPE_NFIELDS (variant_type
);
1725 if (!disr
.is_encoded
)
1728 if (field_number
>= nfields
|| field_number
< 0)
1729 error(_("Cannot access field %d of variant %s, \
1730 there are only %d fields"),
1731 disr
.is_encoded
? field_number
: field_number
- 1,
1733 disr
.is_encoded
? nfields
: nfields
- 1);
1735 if (!(disr
.is_encoded
1736 ? rust_tuple_struct_type_p (variant_type
)
1737 : rust_tuple_variant_type_p (variant_type
)))
1738 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1740 result
= value_primitive_field (lhs
, 0, field_number
,
1743 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1745 /* Tuples and tuple structs */
1746 nfields
= TYPE_NFIELDS(type
);
1748 if (field_number
>= nfields
|| field_number
< 0)
1749 error(_("Cannot access field %d of %s, there are only %d fields"),
1750 field_number
, TYPE_TAG_NAME (type
), nfields
);
1752 /* Tuples are tuple structs too. */
1753 if (!rust_tuple_struct_type_p (type
))
1754 error(_("Attempting to access anonymous field %d of %s, which is \
1755 not a tuple, tuple struct, or tuple-like variant"),
1756 field_number
, TYPE_TAG_NAME (type
));
1758 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1761 error(_("Anonymous field access is only allowed on tuples, \
1762 tuple structs, and tuple-like enum variants"));
1766 case STRUCTOP_STRUCT
:
1773 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1774 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1775 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1777 type
= value_type (lhs
);
1778 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1779 && !rust_union_is_untagged (type
))
1782 struct disr_info disr
;
1783 struct type
* variant_type
;
1786 field_name
= &exp
->elts
[pc
+ 2].string
;
1788 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1789 value_embedded_offset (lhs
),
1790 value_address (lhs
), lhs
);
1792 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1793 error(_("Could not find field %s of struct variant %s"),
1794 field_name
, disr
.name
.c_str ());
1796 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1798 if (variant_type
== NULL
1800 ? rust_tuple_struct_type_p (variant_type
)
1801 : rust_tuple_variant_type_p (variant_type
)))
1802 error(_("Attempting to access named field %s of tuple variant %s, \
1803 which has only anonymous fields"),
1804 field_name
, disr
.name
.c_str ());
1806 start
= disr
.is_encoded
? 0 : 1;
1807 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1809 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1811 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1816 if (i
== TYPE_NFIELDS (variant_type
))
1817 /* We didn't find it. */
1818 error(_("Could not find field %s of struct variant %s"),
1819 field_name
, disr
.name
.c_str ());
1823 /* Field access in structs and untagged unions works like C. */
1825 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1831 result
= rust_range (exp
, pos
, noside
);
1835 /* We might have &array[range], in which case we need to make a
1837 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1840 result
= rust_subscript (exp
, pos
, noside
, 1);
1845 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1852 /* operator_length implementation for Rust. */
1855 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1861 switch (exp
->elts
[pc
- 1].opcode
)
1864 /* We handle aggregate as a type and argument count. The first
1865 argument might be OP_OTHERS. After that the arguments
1866 alternate: first an OP_NAME, then an expression. */
1868 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1876 case STRUCTOP_ANONYMOUS
:
1887 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1895 /* op_name implementation for Rust. */
1898 rust_op_name (enum exp_opcode opcode
)
1903 return "OP_AGGREGATE";
1907 return op_name_standard (opcode
);
1911 /* dump_subexp_body implementation for Rust. */
1914 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1917 switch (exp
->elts
[elt
].opcode
)
1921 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1924 fprintf_filtered (stream
, "Type @");
1925 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1926 fprintf_filtered (stream
, " (");
1927 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1928 fprintf_filtered (stream
, "), length %d", length
);
1931 for (i
= 0; i
< length
; ++i
)
1932 elt
= dump_subexp (exp
, stream
, elt
);
1939 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1941 fprintf_filtered (stream
, "%s: %s",
1942 (exp
->elts
[elt
].opcode
== OP_STRING
1943 ? "string" : "name"),
1944 &exp
->elts
[elt
+ 2].string
);
1945 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1950 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1953 case STRUCTOP_ANONYMOUS
:
1957 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1959 fprintf_filtered (stream
, "Field number: %d", field_number
);
1960 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1968 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1975 /* print_subexp implementation for Rust. */
1978 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1979 enum precedence prec
)
1981 switch (exp
->elts
[*pos
].opcode
)
1985 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1988 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1989 fputs_filtered (" { ", stream
);
1992 for (i
= 0; i
< length
; ++i
)
1994 rust_print_subexp (exp
, pos
, stream
, prec
);
1995 fputs_filtered (", ", stream
);
1997 fputs_filtered (" }", stream
);
2003 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2005 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2006 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2012 fputs_filtered ("<<others>> (", stream
);
2014 rust_print_subexp (exp
, pos
, stream
, prec
);
2015 fputs_filtered (")", stream
);
2019 case STRUCTOP_ANONYMOUS
:
2021 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2024 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2025 fprintf_filtered (stream
, ".%d", tem
);
2031 fprintf_filtered (stream
, "[");
2032 rust_print_subexp (exp
, pos
, stream
, prec
);
2033 fprintf_filtered (stream
, "; ");
2034 rust_print_subexp (exp
, pos
, stream
, prec
);
2035 fprintf_filtered (stream
, "]");
2039 print_subexp_standard (exp
, pos
, stream
, prec
);
2044 /* operator_check implementation for Rust. */
2047 rust_operator_check (struct expression
*exp
, int pos
,
2048 int (*objfile_func
) (struct objfile
*objfile
,
2052 switch (exp
->elts
[pos
].opcode
)
2056 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2057 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2059 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2070 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2078 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2080 static struct block_symbol
2081 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2083 const struct block
*block
,
2084 const domain_enum domain
)
2086 struct block_symbol result
= {NULL
, NULL
};
2088 if (symbol_lookup_debug
)
2090 fprintf_unfiltered (gdb_stdlog
,
2091 "rust_lookup_symbol_non_local"
2092 " (%s, %s (scope %s), %s)\n",
2093 name
, host_address_to_string (block
),
2094 block_scope (block
), domain_name (domain
));
2097 /* Look up bare names in the block's scope. */
2098 if (name
[cp_find_first_component (name
)] == '\0')
2100 const char *scope
= block_scope (block
);
2102 if (scope
[0] != '\0')
2104 std::string scopedname
= std::string (scope
) + "::" + name
;
2106 result
= lookup_symbol_in_static_block (scopedname
.c_str (), block
,
2108 if (result
.symbol
== NULL
)
2109 result
= lookup_global_symbol (scopedname
.c_str (), block
, domain
);
2117 /* la_sniff_from_mangled_name for Rust. */
2120 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2122 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2123 return *demangled
!= NULL
;
2128 static const struct exp_descriptor exp_descriptor_rust
=
2131 rust_operator_length
,
2132 rust_operator_check
,
2134 rust_dump_subexp_body
,
2135 rust_evaluate_subexp
2138 static const char *rust_extensions
[] =
2143 static const struct language_defn rust_language_defn
=
2153 &exp_descriptor_rust
,
2157 rust_printchar
, /* Print a character constant */
2158 rust_printstr
, /* Function to print string constant */
2159 rust_emitchar
, /* Print a single char */
2160 rust_print_type
, /* Print a type using appropriate syntax */
2161 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2162 rust_val_print
, /* Print a value using appropriate syntax */
2163 c_value_print
, /* Print a top-level value */
2164 default_read_var_value
, /* la_read_var_value */
2165 NULL
, /* Language specific skip_trampoline */
2166 NULL
, /* name_of_this */
2167 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2168 basic_lookup_transparent_type
,/* lookup_transparent_type */
2169 gdb_demangle
, /* Language specific symbol demangler */
2170 rust_sniff_from_mangled_name
,
2171 NULL
, /* Language specific
2172 class_name_from_physname */
2173 c_op_print_tab
, /* expression operators for printing */
2174 1, /* c-style arrays */
2175 0, /* String lower bound */
2176 default_word_break_characters
,
2177 default_make_symbol_completion_list
,
2178 rust_language_arch_info
,
2179 default_print_array_index
,
2180 default_pass_by_reference
,
2182 NULL
, /* la_get_symbol_name_cmp */
2183 iterate_over_symbols
,
2184 &default_varobj_ops
,
2191 _initialize_rust_language (void)
2193 add_language (&rust_language_defn
);