1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2018 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"
33 #include "rust-lang.h"
39 /* Returns the last segment of a Rust path like foo::bar::baz. Will
40 not handle cases where the last segment contains generics. This
41 will return NULL if the last segment cannot be found. */
44 rust_last_path_segment (const char * path
)
46 const char *result
= strrchr (path
, ':');
53 /* See rust-lang.h. */
56 rust_crate_for_block (const struct block
*block
)
58 const char *scope
= block_scope (block
);
61 return std::string ();
63 return std::string (scope
, cp_find_first_component (scope
));
66 /* Information about the discriminant/variant of an enum */
72 /* Field number in union. Negative on error. For an encoded enum,
73 the "hidden" member will always be field 1, and the "real" member
74 will always be field 0. */
76 /* True if this is an encoded enum that has a single "real" member
77 and a single "hidden" member. */
78 unsigned int is_encoded
: 1;
81 /* The prefix of a specially-encoded enum. */
83 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
85 /* The number of the real field. */
87 #define RUST_ENCODED_ENUM_REAL 0
89 /* The number of the hidden field. */
91 #define RUST_ENCODED_ENUM_HIDDEN 1
93 /* Whether or not a TYPE_CODE_UNION value is an untagged union
94 as opposed to being a regular Rust enum. */
96 rust_union_is_untagged (struct type
*type
)
98 /* Unions must have at least one field. */
99 if (TYPE_NFIELDS (type
) == 0)
101 /* If the first field is named, but the name has the rust enum prefix,
103 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
104 strlen (RUST_ENUM_PREFIX
)) == 0)
106 /* Unions only have named fields. */
107 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
109 if (strlen (TYPE_FIELD_NAME (type
, i
)) == 0)
115 /* Utility function to get discriminant info for a given value. */
117 static struct disr_info
118 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
119 int embedded_offset
, CORE_ADDR address
,
123 struct disr_info ret
;
124 struct type
*disr_type
;
125 struct value_print_options opts
;
126 const char *name_segment
;
128 get_no_prettyformat_print_options (&opts
);
133 if (TYPE_NFIELDS (type
) == 0)
134 error (_("Encountered void enum value"));
136 /* If an enum has two values where one is empty and the other holds
137 a pointer that cannot be zero; then the Rust compiler optimizes
138 away the discriminant and instead uses a zero value in the
139 pointer field to indicate the empty variant. */
140 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
141 strlen (RUST_ENUM_PREFIX
)) == 0)
143 char *tail
, *token
, *saveptr
= NULL
;
144 unsigned long fieldno
;
145 struct type
*member_type
;
150 if (TYPE_NFIELDS (type
) != 1)
151 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
153 /* Optimized enums have only one field. */
154 member_type
= TYPE_FIELD_TYPE (type
, 0);
156 std::string
name (TYPE_FIELD_NAME (type
, 0));
157 tail
= &name
[0] + strlen (RUST_ENUM_PREFIX
);
159 /* The location of the value that doubles as a discriminant is
160 stored in the name of the field, as
161 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
162 where the fieldnos are the indices of the fields that should be
163 traversed in order to find the field (which may be several fields deep)
164 and the variantname is the name of the variant of the case when the
166 for (token
= strtok_r (tail
, "$", &saveptr
);
168 token
= strtok_r (NULL
, "$", &saveptr
))
170 if (sscanf (token
, "%lu", &fieldno
) != 1)
172 /* We have reached the enum name, which cannot start
176 if (fieldno
>= TYPE_NFIELDS (member_type
))
177 error (_("%s refers to field after end of member type"),
180 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
181 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
185 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
186 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
190 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
191 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + token
;
195 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
196 ret
.name
= (std::string (TYPE_NAME (type
)) + "::"
197 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))));
203 disr_type
= TYPE_FIELD_TYPE (type
, 0);
205 if (TYPE_NFIELDS (disr_type
) == 0)
207 /* This is a bounds check and should never be hit unless Rust
208 has changed its debuginfo format. */
209 error (_("Could not find enum discriminant field"));
211 else if (TYPE_NFIELDS (type
) == 1)
213 /* Sometimes univariant enums are encoded without a
214 discriminant. In that case, treating it as an encoded enum
215 with the first field being the actual type works. */
216 const char *field_name
= TYPE_NAME (TYPE_FIELD_TYPE (type
, 0));
217 const char *last
= rust_last_path_segment (field_name
);
218 ret
.name
= std::string (TYPE_NAME (type
)) + "::" + last
;
219 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
224 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
225 error (_("Rust debug format has changed"));
227 string_file temp_file
;
228 /* The first value of the first field (or any field)
229 is the discriminant value. */
230 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0),
231 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
232 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
236 ret
.name
= std::move (temp_file
.string ());
237 name_segment
= rust_last_path_segment (ret
.name
.c_str ());
238 if (name_segment
!= NULL
)
240 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
242 /* Sadly, the discriminant value paths do not match the type
243 field name paths ('core::option::Option::Some' vs
244 'core::option::Some'). However, enum variant names are
245 unique in the last path segment and the generics are not
246 part of this path, so we can just compare those. This is
247 hackish and would be better fixed by improving rustc's
248 metadata for enums. */
249 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
251 if (field_type
!= NULL
252 && strcmp (name_segment
,
253 rust_last_path_segment (field_type
)) == 0)
261 if (ret
.field_no
== -1 && !ret
.name
.empty ())
263 /* Somehow the discriminant wasn't found. */
264 error (_("Could not find variant of %s with discriminant %s"),
265 TYPE_TAG_NAME (type
), ret
.name
.c_str ());
271 /* See rust-lang.h. */
274 rust_tuple_type_p (struct type
*type
)
276 /* The current implementation is a bit of a hack, but there's
277 nothing else in the debuginfo to distinguish a tuple from a
279 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
280 && TYPE_TAG_NAME (type
) != NULL
281 && TYPE_TAG_NAME (type
)[0] == '(');
285 /* Return true if all non-static fields of a structlike type are in a
286 sequence like __0, __1, __2. OFFSET lets us skip fields. */
289 rust_underscore_fields (struct type
*type
, int offset
)
295 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
297 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
299 if (!field_is_static (&TYPE_FIELD (type
, i
)))
307 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
308 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
317 /* See rust-lang.h. */
320 rust_tuple_struct_type_p (struct type
*type
)
322 /* This is just an approximation until DWARF can represent Rust more
323 precisely. We exclude zero-length structs because they may not
324 be tuple structs, and there's no way to tell. */
325 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
, 0);
328 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
331 rust_tuple_variant_type_p (struct type
*type
)
333 /* First field is discriminant */
334 return rust_underscore_fields (type
, 1);
337 /* Return true if TYPE is a slice type, otherwise false. */
340 rust_slice_type_p (struct type
*type
)
342 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
343 && TYPE_TAG_NAME (type
) != NULL
344 && (strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0
345 || strcmp (TYPE_TAG_NAME (type
), "&str") == 0));
348 /* Return true if TYPE is a range type, otherwise false. */
351 rust_range_type_p (struct type
*type
)
355 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
356 || TYPE_NFIELDS (type
) > 2
357 || TYPE_TAG_NAME (type
) == NULL
358 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
361 if (TYPE_NFIELDS (type
) == 0)
365 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
367 if (TYPE_NFIELDS (type
) == 1)
371 else if (TYPE_NFIELDS (type
) == 2)
373 /* First field had to be "start". */
377 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
380 /* Return true if TYPE seems to be the type "u8", otherwise false. */
383 rust_u8_type_p (struct type
*type
)
385 return (TYPE_CODE (type
) == TYPE_CODE_INT
386 && TYPE_UNSIGNED (type
)
387 && TYPE_LENGTH (type
) == 1);
390 /* Return true if TYPE is a Rust character type. */
393 rust_chartype_p (struct type
*type
)
395 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
396 && TYPE_LENGTH (type
) == 4
397 && TYPE_UNSIGNED (type
));
400 /* If VALUE represents a trait object pointer, return the underlying
401 pointer with the correct (i.e., runtime) type. Otherwise, return
404 static struct value
*
405 rust_get_trait_object_pointer (struct value
*value
)
407 struct type
*type
= check_typedef (value_type (value
));
409 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
412 /* Try to be a bit resilient if the ABI changes. */
413 int vtable_field
= 0;
414 for (int i
= 0; i
< 2; ++i
)
416 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
418 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
422 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
423 struct symbol
*symbol
= find_symbol_at_address (vtable
);
424 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
427 struct rust_vtable_symbol
*vtable_sym
428 = static_cast<struct rust_vtable_symbol
*> (symbol
);
429 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
430 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
435 /* la_emitchar implementation for Rust. */
438 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
440 if (!rust_chartype_p (type
))
441 generic_emit_char (c
, type
, stream
, quoter
,
442 target_charset (get_type_arch (type
)));
443 else if (c
== '\\' || c
== quoter
)
444 fprintf_filtered (stream
, "\\%c", c
);
446 fputs_filtered ("\\n", stream
);
448 fputs_filtered ("\\r", stream
);
450 fputs_filtered ("\\t", stream
);
452 fputs_filtered ("\\0", stream
);
453 else if (c
>= 32 && c
<= 127 && isprint (c
))
454 fputc_filtered (c
, stream
);
456 fprintf_filtered (stream
, "\\x%02x", c
);
458 fprintf_filtered (stream
, "\\u{%06x}", c
);
461 /* la_printchar implementation for Rust. */
464 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
466 fputs_filtered ("'", stream
);
467 LA_EMIT_CHAR (c
, type
, stream
, '\'');
468 fputs_filtered ("'", stream
);
471 /* la_printstr implementation for Rust. */
474 rust_printstr (struct ui_file
*stream
, struct type
*type
,
475 const gdb_byte
*string
, unsigned int length
,
476 const char *user_encoding
, int force_ellipses
,
477 const struct value_print_options
*options
)
479 /* Rust always uses UTF-8, but let the caller override this if need
481 const char *encoding
= user_encoding
;
482 if (user_encoding
== NULL
|| !*user_encoding
)
484 /* In Rust strings, characters are "u8". */
485 if (rust_u8_type_p (type
))
489 /* This is probably some C string, so let's let C deal with
491 c_printstr (stream
, type
, string
, length
, user_encoding
,
492 force_ellipses
, options
);
497 /* This is not ideal as it doesn't use our character printer. */
498 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
504 /* Helper function to print a string slice. */
507 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
508 const struct value_print_options
*options
)
510 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
512 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
514 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
515 value_as_address (base
), value_as_long (len
), stream
,
519 /* rust_print_type branch for structs and untagged unions. */
522 val_print_struct (struct type
*type
, int embedded_offset
,
523 CORE_ADDR address
, struct ui_file
*stream
,
524 int recurse
, struct value
*val
,
525 const struct value_print_options
*options
)
530 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
532 rust_val_print_str (stream
, val
, options
);
536 bool is_tuple
= rust_tuple_type_p (type
);
537 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
538 struct value_print_options opts
;
542 if (TYPE_TAG_NAME (type
) != NULL
)
543 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
545 if (TYPE_NFIELDS (type
) == 0)
548 if (TYPE_TAG_NAME (type
) != NULL
)
549 fputs_filtered (" ", stream
);
552 if (is_tuple
|| is_tuple_struct
)
553 fputs_filtered ("(", stream
);
555 fputs_filtered ("{", stream
);
561 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
563 if (field_is_static (&TYPE_FIELD (type
, i
)))
567 fputs_filtered (",", stream
);
569 if (options
->prettyformat
)
571 fputs_filtered ("\n", stream
);
572 print_spaces_filtered (2 + 2 * recurse
, stream
);
574 else if (!first_field
)
575 fputs_filtered (" ", stream
);
579 if (!is_tuple
&& !is_tuple_struct
)
581 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
582 fputs_filtered (": ", stream
);
585 val_print (TYPE_FIELD_TYPE (type
, i
),
586 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
588 stream
, recurse
+ 1, val
, &opts
,
592 if (options
->prettyformat
)
594 fputs_filtered ("\n", stream
);
595 print_spaces_filtered (2 * recurse
, stream
);
598 if (is_tuple
|| is_tuple_struct
)
599 fputs_filtered (")", stream
);
601 fputs_filtered ("}", stream
);
604 static const struct generic_val_print_decorations rust_decorations
=
606 /* Complex isn't used in Rust, but we provide C-ish values just in
618 /* la_val_print implementation for Rust. */
621 rust_val_print (struct type
*type
, int embedded_offset
,
622 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
624 const struct value_print_options
*options
)
626 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
628 type
= check_typedef (type
);
629 switch (TYPE_CODE (type
))
633 LONGEST low_bound
, high_bound
;
635 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
636 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
637 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
639 /* We have a pointer to a byte string, so just print
641 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
643 struct gdbarch
*arch
= get_type_arch (type
);
644 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
646 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
647 if (options
->addressprint
)
649 fputs_filtered (paddress (arch
, addr
), stream
);
650 fputs_filtered (" ", stream
);
653 fputs_filtered ("b", stream
);
654 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
655 high_bound
- low_bound
+ 1, stream
,
662 case TYPE_CODE_METHODPTR
:
663 case TYPE_CODE_MEMBERPTR
:
664 c_val_print (type
, embedded_offset
, address
, stream
,
665 recurse
, val
, options
);
669 /* Recognize the unit type. */
670 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
671 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
673 fputs_filtered ("()", stream
);
678 case TYPE_CODE_STRING
:
680 struct gdbarch
*arch
= get_type_arch (type
);
681 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
682 LONGEST low_bound
, high_bound
;
684 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
685 error (_("Could not determine the array bounds"));
687 /* If we see a plain TYPE_CODE_STRING, then we're printing a
688 byte string, hence the choice of "ASCII" as the
690 fputs_filtered ("b", stream
);
691 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
692 valaddr
+ embedded_offset
* unit_size
,
693 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
697 case TYPE_CODE_ARRAY
:
699 LONGEST low_bound
, high_bound
;
701 if (get_array_bounds (type
, &low_bound
, &high_bound
)
702 && high_bound
- low_bound
+ 1 == 0)
703 fputs_filtered ("[]", stream
);
709 case TYPE_CODE_UNION
:
711 int j
, nfields
, first_field
, is_tuple
, start
;
712 struct type
*variant_type
;
713 struct disr_info disr
;
714 struct value_print_options opts
;
716 /* Untagged unions are printed as if they are structs.
717 Since the field bit positions overlap in the debuginfo,
718 the code for printing a union is same as that for a struct,
719 the only difference is that the input type will have overlapping
721 if (rust_union_is_untagged (type
))
723 val_print_struct (type
, embedded_offset
, address
, stream
,
724 recurse
, val
, options
);
731 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
734 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
736 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
741 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
742 nfields
= TYPE_NFIELDS (variant_type
);
744 is_tuple
= (disr
.is_encoded
745 ? rust_tuple_struct_type_p (variant_type
)
746 : rust_tuple_variant_type_p (variant_type
));
747 start
= disr
.is_encoded
? 0 : 1;
751 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
753 fprintf_filtered (stream
, "%s(", disr
.name
.c_str ());
756 /* struct variant. */
757 fprintf_filtered (stream
, "%s{", disr
.name
.c_str ());
762 /* In case of a nullary variant like 'None', just output
764 fprintf_filtered (stream
, "%s", disr
.name
.c_str ());
768 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
771 fputs_filtered (", ", stream
);
775 fprintf_filtered (stream
, "%s: ",
776 TYPE_FIELD_NAME (variant_type
, j
));
778 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
780 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
781 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
783 stream
, recurse
+ 1, val
, &opts
,
788 fputs_filtered (")", stream
);
790 fputs_filtered ("}", stream
);
794 case TYPE_CODE_STRUCT
:
795 val_print_struct (type
, embedded_offset
, address
, stream
,
796 recurse
, val
, options
);
801 /* Nothing special yet. */
802 generic_val_print (type
, embedded_offset
, address
, stream
,
803 recurse
, val
, options
, &rust_decorations
);
810 rust_print_type (struct type
*type
, const char *varstring
,
811 struct ui_file
*stream
, int show
, int level
,
812 const struct type_print_options
*flags
);
814 /* Print a struct or union typedef. */
816 rust_print_struct_def (struct type
*type
, const char *varstring
,
817 struct ui_file
*stream
, int show
, int level
,
818 const struct type_print_options
*flags
)
820 bool is_tuple_struct
;
823 /* Print a tuple type simply. */
824 if (rust_tuple_type_p (type
))
826 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
830 /* If we see a base class, delegate to C. */
831 if (TYPE_N_BASECLASSES (type
) > 0)
832 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
834 /* This code path is also used by unions. */
835 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
836 fputs_filtered ("struct ", stream
);
838 fputs_filtered ("union ", stream
);
840 if (TYPE_TAG_NAME (type
) != NULL
)
841 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
843 is_tuple_struct
= rust_tuple_struct_type_p (type
);
845 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
847 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
849 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
852 if (field_is_static (&TYPE_FIELD (type
, i
)))
855 /* We'd like to print "pub" here as needed, but rustc
856 doesn't emit the debuginfo, and our types don't have
857 cplus_struct_type attached. */
859 /* For a tuple struct we print the type but nothing
861 print_spaces_filtered (level
+ 2, stream
);
862 if (!is_tuple_struct
)
863 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
865 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
866 stream
, show
- 1, level
+ 2,
868 fputs_filtered (",\n", stream
);
871 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
874 /* la_print_typedef implementation for Rust. */
877 rust_print_typedef (struct type
*type
,
878 struct symbol
*new_symbol
,
879 struct ui_file
*stream
)
881 type
= check_typedef (type
);
882 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
883 type_print (type
, "", stream
, 0);
884 fprintf_filtered (stream
, ";\n");
887 /* la_print_type implementation for Rust. */
890 rust_print_type (struct type
*type
, const char *varstring
,
891 struct ui_file
*stream
, int show
, int level
,
892 const struct type_print_options
*flags
)
898 && TYPE_NAME (type
) != NULL
)
900 /* Rust calls the unit type "void" in its debuginfo,
901 but we don't want to print it as that. */
902 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
903 fputs_filtered ("()", stream
);
905 fputs_filtered (TYPE_NAME (type
), stream
);
909 type
= check_typedef (type
);
910 switch (TYPE_CODE (type
))
913 fputs_filtered ("()", stream
);
917 /* Delegate varargs to the C printer. */
918 if (TYPE_VARARGS (type
))
921 fputs_filtered ("fn ", stream
);
922 if (varstring
!= NULL
)
923 fputs_filtered (varstring
, stream
);
924 fputs_filtered ("(", stream
);
925 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
929 fputs_filtered (", ", stream
);
930 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
933 fputs_filtered (")", stream
);
934 /* If it returns unit, we can omit the return type. */
935 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
937 fputs_filtered (" -> ", stream
);
938 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
942 case TYPE_CODE_ARRAY
:
944 LONGEST low_bound
, high_bound
;
946 fputs_filtered ("[", stream
);
947 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
948 stream
, show
- 1, level
, flags
);
950 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
951 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
952 fprintf_filtered (stream
, "; variable length");
953 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
954 fprintf_filtered (stream
, "; %s",
955 plongest (high_bound
- low_bound
+ 1));
956 fputs_filtered ("]", stream
);
960 case TYPE_CODE_STRUCT
:
961 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
968 fputs_filtered ("enum ", stream
);
969 if (TYPE_TAG_NAME (type
) != NULL
)
971 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
972 fputs_filtered (" ", stream
);
973 len
= strlen (TYPE_TAG_NAME (type
));
975 fputs_filtered ("{\n", stream
);
977 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
979 const char *name
= TYPE_FIELD_NAME (type
, i
);
984 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
986 && name
[len
+ 1] == ':')
988 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
991 fputs_filtered ("}", stream
);
995 case TYPE_CODE_UNION
:
999 /* Skip the discriminant field. */
1002 /* Unions and structs have the same syntax in Rust,
1003 the only difference is that structs are declared with `struct`
1004 and union with `union`. This difference is handled in the struct
1006 if (rust_union_is_untagged (type
))
1008 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
);
1012 fputs_filtered ("enum ", stream
);
1013 if (TYPE_TAG_NAME (type
) != NULL
)
1015 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
1016 fputs_filtered (" ", stream
);
1018 fputs_filtered ("{\n", stream
);
1020 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
1021 strlen (RUST_ENUM_PREFIX
)) == 0)
1023 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
1024 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
1026 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
1027 /* There is no explicit discriminant field, skip nothing. */
1031 else if (TYPE_NFIELDS (type
) == 1)
1034 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1036 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
1038 = rust_last_path_segment (TYPE_NAME (variant_type
));
1040 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
1042 if (TYPE_NFIELDS (variant_type
) > skip_to
)
1045 bool is_tuple
= (TYPE_NFIELDS (type
) == 1
1046 ? rust_tuple_struct_type_p (variant_type
)
1047 : rust_tuple_variant_type_p (variant_type
));
1050 fputs_filtered (is_tuple
? "(" : "{", stream
);
1051 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
1056 fputs_filtered (", ", stream
);
1059 fprintf_filtered (stream
, "%s: ",
1060 TYPE_FIELD_NAME (variant_type
, j
));
1062 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
1063 stream
, show
- 1, level
+ 2,
1066 fputs_filtered (is_tuple
? ")" : "}", stream
);
1069 fputs_filtered (",\n", stream
);
1072 fputs_filtered ("}", stream
);
1078 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
1084 /* Compute the alignment of the type T. */
1087 rust_type_alignment (struct type
*t
)
1089 t
= check_typedef (t
);
1090 switch (TYPE_CODE (t
))
1093 error (_("Could not compute alignment of type"));
1096 case TYPE_CODE_ENUM
:
1100 case TYPE_CODE_CHAR
:
1101 case TYPE_CODE_BOOL
:
1102 return TYPE_LENGTH (t
);
1104 case TYPE_CODE_ARRAY
:
1105 case TYPE_CODE_COMPLEX
:
1106 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
1108 case TYPE_CODE_STRUCT
:
1109 case TYPE_CODE_UNION
:
1114 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
1116 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
1125 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1126 -- either on an obstack or on a gdbarch. */
1128 static struct type
*
1129 rust_composite_type (struct type
*original
,
1131 const char *field1
, struct type
*type1
,
1132 const char *field2
, struct type
*type2
)
1134 struct type
*result
= alloc_type_copy (original
);
1135 int i
, nfields
, bitpos
;
1143 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1144 TYPE_NAME (result
) = name
;
1145 TYPE_TAG_NAME (result
) = name
;
1147 TYPE_NFIELDS (result
) = nfields
;
1148 TYPE_FIELDS (result
)
1149 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1155 struct field
*field
= &TYPE_FIELD (result
, i
);
1157 SET_FIELD_BITPOS (*field
, bitpos
);
1158 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1160 FIELD_NAME (*field
) = field1
;
1161 FIELD_TYPE (*field
) = type1
;
1166 struct field
*field
= &TYPE_FIELD (result
, i
);
1167 int align
= rust_type_alignment (type2
);
1173 align
*= TARGET_CHAR_BIT
;
1174 delta
= bitpos
% align
;
1176 bitpos
+= align
- delta
;
1178 SET_FIELD_BITPOS (*field
, bitpos
);
1180 FIELD_NAME (*field
) = field2
;
1181 FIELD_TYPE (*field
) = type2
;
1186 TYPE_LENGTH (result
)
1187 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1188 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1192 /* See rust-lang.h. */
1195 rust_slice_type (const char *name
, struct type
*elt_type
,
1196 struct type
*usize_type
)
1200 elt_type
= lookup_pointer_type (elt_type
);
1201 type
= rust_composite_type (elt_type
, name
,
1202 "data_ptr", elt_type
,
1203 "length", usize_type
);
1208 enum rust_primitive_types
1210 rust_primitive_bool
,
1211 rust_primitive_char
,
1220 rust_primitive_isize
,
1221 rust_primitive_usize
,
1224 rust_primitive_unit
,
1226 nr_rust_primitive_types
1229 /* la_language_arch_info implementation for Rust. */
1232 rust_language_arch_info (struct gdbarch
*gdbarch
,
1233 struct language_arch_info
*lai
)
1235 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1237 struct type
**types
;
1238 unsigned int length
;
1240 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1243 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1244 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1245 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1246 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1247 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1248 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1249 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1250 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1251 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1252 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1254 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1255 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1256 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1258 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1259 floatformats_ieee_single
);
1260 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1261 floatformats_ieee_double
);
1263 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1265 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1266 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1267 types
[rust_primitive_usize
]);
1269 lai
->primitive_type_vector
= types
;
1270 lai
->bool_type_default
= types
[rust_primitive_bool
];
1271 lai
->string_char_type
= types
[rust_primitive_u8
];
1276 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1278 static struct value
*
1279 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1282 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1284 struct value
*function
, *result
, *arg0
;
1285 struct type
*type
, *fn_type
;
1286 const struct block
*block
;
1287 struct block_symbol sym
;
1289 /* For an ordinary function call we can simply defer to the
1290 generic implementation. */
1291 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1292 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1294 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1296 method
= &exp
->elts
[*pos
+ 1].string
;
1297 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1299 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1300 type in order to look up the method. */
1301 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1303 if (noside
== EVAL_SKIP
)
1305 for (i
= 0; i
< num_args
; ++i
)
1306 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1310 std::vector
<struct value
*> args (num_args
+ 1);
1313 /* We don't yet implement real Deref semantics. */
1314 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1315 args
[0] = value_ind (args
[0]);
1317 type
= value_type (args
[0]);
1318 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1319 && TYPE_CODE (type
) != TYPE_CODE_UNION
1320 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1321 || rust_tuple_type_p (type
))
1322 error (_("Method calls only supported on struct or enum types"));
1323 if (TYPE_TAG_NAME (type
) == NULL
)
1324 error (_("Method call on nameless type"));
1326 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1328 block
= get_selected_block (0);
1329 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1330 if (sym
.symbol
== NULL
)
1331 error (_("Could not find function named '%s'"), name
.c_str ());
1333 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1334 if (TYPE_NFIELDS (fn_type
) == 0)
1335 error (_("Function '%s' takes no arguments"), name
.c_str ());
1337 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1338 args
[0] = value_addr (args
[0]);
1340 function
= address_of_variable (sym
.symbol
, block
);
1342 for (i
= 0; i
< num_args
; ++i
)
1343 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1345 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1346 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1348 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1352 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1354 static struct value
*
1355 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1357 enum range_type kind
;
1358 struct value
*low
= NULL
, *high
= NULL
;
1359 struct value
*addrval
, *result
;
1361 struct type
*range_type
;
1362 struct type
*index_type
;
1363 struct type
*temp_type
;
1366 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1369 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1370 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1371 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1372 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1374 if (noside
== EVAL_SKIP
)
1375 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1382 name
= "std::ops::RangeFull";
1386 index_type
= value_type (high
);
1387 name
= "std::ops::RangeTo";
1394 index_type
= value_type (low
);
1395 name
= "std::ops::RangeFrom";
1399 if (!types_equal (value_type (low
), value_type (high
)))
1400 error (_("Range expression with different types"));
1401 index_type
= value_type (low
);
1402 name
= "std::ops::Range";
1406 /* If we don't have an index type, just allocate this on the
1407 arch. Here any type will do. */
1408 temp_type
= (index_type
== NULL
1409 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1411 /* It would be nicer to cache the range type. */
1412 range_type
= rust_composite_type (temp_type
, name
,
1413 low
== NULL
? NULL
: "start", index_type
,
1414 high
== NULL
? NULL
: "end", index_type
);
1416 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1417 return value_zero (range_type
, lval_memory
);
1419 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1420 addr
= value_as_long (addrval
);
1421 result
= value_at_lazy (range_type
, addr
);
1425 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1428 value_assign (start
, low
);
1433 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1436 value_assign (end
, high
);
1439 result
= value_at_lazy (range_type
, addr
);
1443 /* A helper function to compute the range and kind given a range
1444 value. TYPE is the type of the range value. RANGE is the range
1445 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1446 parameters might be filled in, or might not be, depending on the
1447 kind of range this is. KIND will always be set to the appropriate
1448 value describing the kind of range, and this can be used to
1449 determine whether LOW or HIGH are valid. */
1452 rust_compute_range (struct type
*type
, struct value
*range
,
1453 LONGEST
*low
, LONGEST
*high
,
1454 enum range_type
*kind
)
1460 *kind
= BOTH_BOUND_DEFAULT
;
1462 if (TYPE_NFIELDS (type
) == 0)
1466 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1468 *kind
= HIGH_BOUND_DEFAULT
;
1469 *low
= value_as_long (value_field (range
, 0));
1472 if (TYPE_NFIELDS (type
) > i
1473 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1475 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1476 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1477 *high
= value_as_long (value_field (range
, i
));
1481 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1483 static struct value
*
1484 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1487 struct value
*lhs
, *rhs
, *result
;
1488 struct type
*rhstype
;
1489 LONGEST low
, high_bound
;
1490 /* Initialized to appease the compiler. */
1491 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1496 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1497 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1499 if (noside
== EVAL_SKIP
)
1502 rhstype
= check_typedef (value_type (rhs
));
1503 if (rust_range_type_p (rhstype
))
1506 error (_("Can't take slice of array without '&'"));
1507 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1511 low
= value_as_long (rhs
);
1513 struct type
*type
= check_typedef (value_type (lhs
));
1514 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1516 struct type
*base_type
= nullptr;
1517 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1518 base_type
= TYPE_TARGET_TYPE (type
);
1519 else if (rust_slice_type_p (type
))
1521 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1523 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1525 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1529 if (base_type
== nullptr)
1530 error (_("Could not find 'data_ptr' in slice type"));
1532 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1533 base_type
= TYPE_TARGET_TYPE (type
);
1535 error (_("Cannot subscript non-array type"));
1537 struct type
*new_type
;
1540 if (rust_slice_type_p (type
))
1545 = language_lookup_primitive_type (exp
->language_defn
,
1548 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1552 new_type
= base_type
;
1554 return value_zero (new_type
, VALUE_LVAL (lhs
));
1561 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1564 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1565 error (_("Can't compute array bounds"));
1567 error (_("Found array with non-zero lower bound"));
1570 else if (rust_slice_type_p (type
))
1574 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1575 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1577 high_bound
= value_as_long (len
);
1579 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1583 high_bound
= LONGEST_MAX
;
1586 error (_("Cannot subscript non-array type"));
1589 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1592 error (_("Index less than zero"));
1593 if (low
> high_bound
)
1594 error (_("Index greater than length"));
1596 result
= value_subscript (base
, low
);
1603 struct type
*usize
, *slice
;
1605 struct value
*addrval
, *tem
;
1607 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1610 error (_("High index less than zero"));
1612 error (_("Low index greater than high index"));
1613 if (high
> high_bound
)
1614 error (_("High index greater than length"));
1616 usize
= language_lookup_primitive_type (exp
->language_defn
,
1619 const char *new_name
= ((type
!= nullptr
1620 && rust_slice_type_p (type
))
1621 ? TYPE_NAME (type
) : "&[*gdb*]");
1623 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1625 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1626 addr
= value_as_long (addrval
);
1627 tem
= value_at_lazy (slice
, addr
);
1629 value_assign (value_field (tem
, 0), value_addr (result
));
1630 value_assign (value_field (tem
, 1),
1631 value_from_longest (usize
, high
- low
));
1633 result
= value_at_lazy (slice
, addr
);
1636 result
= value_addr (result
);
1642 /* evaluate_exp implementation for Rust. */
1644 static struct value
*
1645 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1646 int *pos
, enum noside noside
)
1648 struct value
*result
;
1650 switch (exp
->elts
[*pos
].opcode
)
1654 if (noside
!= EVAL_NORMAL
)
1655 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1659 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1662 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1663 if (trait_ptr
!= NULL
)
1666 result
= value_ind (value
);
1671 case UNOP_COMPLEMENT
:
1673 struct value
*value
;
1676 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1677 if (noside
== EVAL_SKIP
)
1679 /* Preserving the type is enough. */
1682 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1683 result
= value_from_longest (value_type (value
),
1684 value_logical_not (value
));
1686 result
= value_complement (value
);
1690 case BINOP_SUBSCRIPT
:
1691 result
= rust_subscript (exp
, pos
, noside
, 0);
1695 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1701 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1702 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1705 struct value
*addrval
= NULL
;
1709 if (noside
== EVAL_NORMAL
)
1711 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1712 addr
= value_as_long (addrval
);
1713 result
= value_at_lazy (type
, addr
);
1716 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1721 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1722 if (noside
== EVAL_NORMAL
)
1724 /* This isn't quite right but will do for the time
1725 being, seeing that we can't implement the Copy
1727 value_assign (result
, init
);
1733 gdb_assert (arglen
% 2 == 0);
1734 for (i
= 0; i
< arglen
; i
+= 2)
1737 const char *fieldname
;
1738 struct value
*value
, *field
;
1740 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1742 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1744 fieldname
= &exp
->elts
[*pos
].string
;
1745 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1747 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1748 if (noside
== EVAL_NORMAL
)
1750 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1752 value_assign (field
, value
);
1756 if (noside
== EVAL_SKIP
)
1757 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1759 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1760 result
= allocate_value (type
);
1762 result
= value_at_lazy (type
, addr
);
1771 struct value
*ncopies
;
1773 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1774 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1775 copies
= value_as_long (ncopies
);
1777 error (_("Array with negative number of elements"));
1779 if (noside
== EVAL_NORMAL
)
1782 std::vector
<struct value
*> eltvec (copies
);
1784 for (i
= 0; i
< copies
; ++i
)
1786 result
= value_array (0, copies
- 1, eltvec
.data ());
1790 struct type
*arraytype
1791 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1792 result
= allocate_value (arraytype
);
1797 case STRUCTOP_ANONYMOUS
:
1799 /* Anonymous field access, i.e. foo.1. */
1801 int pc
, field_number
, nfields
;
1802 struct type
*type
, *variant_type
;
1803 struct disr_info disr
;
1806 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1808 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1810 type
= value_type (lhs
);
1811 /* Untagged unions can't have anonymous field access since
1812 they can only have named fields. */
1813 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1814 && !rust_union_is_untagged (type
))
1816 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1817 value_embedded_offset (lhs
),
1818 value_address (lhs
), lhs
);
1820 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1822 variant_type
= NULL
;
1827 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1828 nfields
= TYPE_NFIELDS (variant_type
);
1831 if (!disr
.is_encoded
)
1834 if (field_number
>= nfields
|| field_number
< 0)
1835 error(_("Cannot access field %d of variant %s, \
1836 there are only %d fields"),
1837 disr
.is_encoded
? field_number
: field_number
- 1,
1839 disr
.is_encoded
? nfields
: nfields
- 1);
1841 if (!(disr
.is_encoded
1842 ? rust_tuple_struct_type_p (variant_type
)
1843 : rust_tuple_variant_type_p (variant_type
)))
1844 error(_("Variant %s is not a tuple variant"), disr
.name
.c_str ());
1846 result
= value_primitive_field (lhs
, 0, field_number
,
1849 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1851 /* Tuples and tuple structs */
1852 nfields
= TYPE_NFIELDS(type
);
1854 if (field_number
>= nfields
|| field_number
< 0)
1855 error(_("Cannot access field %d of %s, there are only %d fields"),
1856 field_number
, TYPE_TAG_NAME (type
), nfields
);
1858 /* Tuples are tuple structs too. */
1859 if (!rust_tuple_struct_type_p (type
))
1860 error(_("Attempting to access anonymous field %d of %s, which is \
1861 not a tuple, tuple struct, or tuple-like variant"),
1862 field_number
, TYPE_TAG_NAME (type
));
1864 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1867 error(_("Anonymous field access is only allowed on tuples, \
1868 tuple structs, and tuple-like enum variants"));
1872 case STRUCTOP_STRUCT
:
1879 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1880 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1881 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1883 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1884 type
= value_type (lhs
);
1885 if (TYPE_CODE (type
) == TYPE_CODE_UNION
1886 && !rust_union_is_untagged (type
))
1889 struct disr_info disr
;
1890 struct type
*variant_type
;
1892 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1893 value_embedded_offset (lhs
),
1894 value_address (lhs
), lhs
);
1896 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1897 error(_("Could not find field %s of struct variant %s"),
1898 field_name
, disr
.name
.c_str ());
1900 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1902 if (variant_type
== NULL
1904 ? rust_tuple_struct_type_p (variant_type
)
1905 : rust_tuple_variant_type_p (variant_type
)))
1906 error(_("Attempting to access named field %s of tuple variant %s, \
1907 which has only anonymous fields"),
1908 field_name
, disr
.name
.c_str ());
1910 start
= disr
.is_encoded
? 0 : 1;
1911 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1913 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1915 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1920 if (i
== TYPE_NFIELDS (variant_type
))
1921 /* We didn't find it. */
1922 error(_("Could not find field %s of struct variant %s"),
1923 field_name
, disr
.name
.c_str ());
1927 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
,
1929 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1930 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1936 result
= rust_range (exp
, pos
, noside
);
1940 /* We might have &array[range], in which case we need to make a
1942 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1945 result
= rust_subscript (exp
, pos
, noside
, 1);
1950 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1957 /* operator_length implementation for Rust. */
1960 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1966 switch (exp
->elts
[pc
- 1].opcode
)
1969 /* We handle aggregate as a type and argument count. The first
1970 argument might be OP_OTHERS. After that the arguments
1971 alternate: first an OP_NAME, then an expression. */
1973 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1981 case STRUCTOP_ANONYMOUS
:
1992 operator_length_standard (exp
, pc
, oplenp
, argsp
);
2000 /* op_name implementation for Rust. */
2003 rust_op_name (enum exp_opcode opcode
)
2008 return "OP_AGGREGATE";
2012 return op_name_standard (opcode
);
2016 /* dump_subexp_body implementation for Rust. */
2019 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
2022 switch (exp
->elts
[elt
].opcode
)
2026 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
2029 fprintf_filtered (stream
, "Type @");
2030 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
2031 fprintf_filtered (stream
, " (");
2032 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
2033 fprintf_filtered (stream
, "), length %d", length
);
2036 for (i
= 0; i
< length
; ++i
)
2037 elt
= dump_subexp (exp
, stream
, elt
);
2044 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
2046 fprintf_filtered (stream
, "%s: %s",
2047 (exp
->elts
[elt
].opcode
== OP_STRING
2048 ? "string" : "name"),
2049 &exp
->elts
[elt
+ 2].string
);
2050 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2055 elt
= dump_subexp (exp
, stream
, elt
+ 1);
2058 case STRUCTOP_ANONYMOUS
:
2062 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
2064 fprintf_filtered (stream
, "Field number: %d", field_number
);
2065 elt
= dump_subexp (exp
, stream
, elt
+ 3);
2074 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
2081 /* print_subexp implementation for Rust. */
2084 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
2085 enum precedence prec
)
2087 switch (exp
->elts
[*pos
].opcode
)
2091 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
2094 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
2095 fputs_filtered (" { ", stream
);
2098 for (i
= 0; i
< length
; ++i
)
2100 rust_print_subexp (exp
, pos
, stream
, prec
);
2101 fputs_filtered (", ", stream
);
2103 fputs_filtered (" }", stream
);
2109 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
2111 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
2112 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
2118 fputs_filtered ("<<others>> (", stream
);
2120 rust_print_subexp (exp
, pos
, stream
, prec
);
2121 fputs_filtered (")", stream
);
2125 case STRUCTOP_ANONYMOUS
:
2127 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
2130 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2131 fprintf_filtered (stream
, ".%d", tem
);
2137 fprintf_filtered (stream
, "[");
2138 rust_print_subexp (exp
, pos
, stream
, prec
);
2139 fprintf_filtered (stream
, "; ");
2140 rust_print_subexp (exp
, pos
, stream
, prec
);
2141 fprintf_filtered (stream
, "]");
2145 print_subexp_standard (exp
, pos
, stream
, prec
);
2150 /* operator_check implementation for Rust. */
2153 rust_operator_check (struct expression
*exp
, int pos
,
2154 int (*objfile_func
) (struct objfile
*objfile
,
2158 switch (exp
->elts
[pos
].opcode
)
2162 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2163 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2165 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2176 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2184 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2186 static struct block_symbol
2187 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2189 const struct block
*block
,
2190 const domain_enum domain
)
2192 struct block_symbol result
= {NULL
, NULL
};
2194 if (symbol_lookup_debug
)
2196 fprintf_unfiltered (gdb_stdlog
,
2197 "rust_lookup_symbol_non_local"
2198 " (%s, %s (scope %s), %s)\n",
2199 name
, host_address_to_string (block
),
2200 block_scope (block
), domain_name (domain
));
2203 /* Look up bare names in the block's scope. */
2204 std::string scopedname
;
2205 if (name
[cp_find_first_component (name
)] == '\0')
2207 const char *scope
= block_scope (block
);
2209 if (scope
[0] != '\0')
2211 scopedname
= std::string (scope
) + "::" + name
;
2212 name
= scopedname
.c_str ();
2220 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2221 if (result
.symbol
== NULL
)
2222 result
= lookup_global_symbol (name
, block
, domain
);
2229 /* la_sniff_from_mangled_name for Rust. */
2232 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2234 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2235 return *demangled
!= NULL
;
2240 /* la_watch_location_expression for Rust. */
2242 static gdb::unique_xmalloc_ptr
<char>
2243 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2245 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2246 std::string name
= type_to_string (type
);
2247 return gdb::unique_xmalloc_ptr
<char>
2248 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2254 static const struct exp_descriptor exp_descriptor_rust
=
2257 rust_operator_length
,
2258 rust_operator_check
,
2260 rust_dump_subexp_body
,
2261 rust_evaluate_subexp
2264 static const char *rust_extensions
[] =
2269 extern const struct language_defn rust_language_defn
=
2279 &exp_descriptor_rust
,
2283 rust_printchar
, /* Print a character constant */
2284 rust_printstr
, /* Function to print string constant */
2285 rust_emitchar
, /* Print a single char */
2286 rust_print_type
, /* Print a type using appropriate syntax */
2287 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2288 rust_val_print
, /* Print a value using appropriate syntax */
2289 c_value_print
, /* Print a top-level value */
2290 default_read_var_value
, /* la_read_var_value */
2291 NULL
, /* Language specific skip_trampoline */
2292 NULL
, /* name_of_this */
2293 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2294 basic_lookup_transparent_type
,/* lookup_transparent_type */
2295 gdb_demangle
, /* Language specific symbol demangler */
2296 rust_sniff_from_mangled_name
,
2297 NULL
, /* Language specific
2298 class_name_from_physname */
2299 c_op_print_tab
, /* expression operators for printing */
2300 1, /* c-style arrays */
2301 0, /* String lower bound */
2302 default_word_break_characters
,
2303 default_collect_symbol_completion_matches
,
2304 rust_language_arch_info
,
2305 default_print_array_index
,
2306 default_pass_by_reference
,
2308 rust_watch_location_expression
,
2309 NULL
, /* la_get_symbol_name_matcher */
2310 iterate_over_symbols
,
2311 default_search_name_hash
,
2312 &default_varobj_ops
,