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 /* See rust-lang.h. */
42 rust_last_path_segment (const char *path
)
44 const char *result
= strrchr (path
, ':');
51 /* See rust-lang.h. */
54 rust_crate_for_block (const struct block
*block
)
56 const char *scope
= block_scope (block
);
59 return std::string ();
61 return std::string (scope
, cp_find_first_component (scope
));
64 /* Return true if TYPE, which must be a struct type, represents a Rust
68 rust_enum_p (const struct type
*type
)
70 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
71 && TYPE_NFIELDS (type
) == 1
72 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type
, 0)));
75 /* Given an enum type and contents, find which variant is active. */
78 rust_enum_variant (struct type
*type
, const gdb_byte
*contents
)
80 /* In Rust the enum always fills the containing structure. */
81 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
83 struct type
*union_type
= TYPE_FIELD_TYPE (type
, 0);
85 int fieldno
= value_union_variant (union_type
, contents
);
86 return &TYPE_FIELD (union_type
, fieldno
);
89 /* See rust-lang.h. */
92 rust_tuple_type_p (struct type
*type
)
94 /* The current implementation is a bit of a hack, but there's
95 nothing else in the debuginfo to distinguish a tuple from a
97 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
98 && TYPE_TAG_NAME (type
) != NULL
99 && TYPE_TAG_NAME (type
)[0] == '(');
102 /* Return true if all non-static fields of a structlike type are in a
103 sequence like __0, __1, __2. */
106 rust_underscore_fields (struct type
*type
)
112 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
114 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
116 if (!field_is_static (&TYPE_FIELD (type
, i
)))
120 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
121 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
129 /* See rust-lang.h. */
132 rust_tuple_struct_type_p (struct type
*type
)
134 /* This is just an approximation until DWARF can represent Rust more
135 precisely. We exclude zero-length structs because they may not
136 be tuple structs, and there's no way to tell. */
137 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
);
140 /* Return true if TYPE is a slice type, otherwise false. */
143 rust_slice_type_p (struct type
*type
)
145 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
146 && TYPE_TAG_NAME (type
) != NULL
147 && (strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0
148 || strcmp (TYPE_TAG_NAME (type
), "&str") == 0));
151 /* Return true if TYPE is a range type, otherwise false. */
154 rust_range_type_p (struct type
*type
)
158 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
159 || TYPE_NFIELDS (type
) > 2
160 || TYPE_TAG_NAME (type
) == NULL
161 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
164 if (TYPE_NFIELDS (type
) == 0)
168 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
170 if (TYPE_NFIELDS (type
) == 1)
174 else if (TYPE_NFIELDS (type
) == 2)
176 /* First field had to be "start". */
180 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
183 /* Return true if TYPE seems to be the type "u8", otherwise false. */
186 rust_u8_type_p (struct type
*type
)
188 return (TYPE_CODE (type
) == TYPE_CODE_INT
189 && TYPE_UNSIGNED (type
)
190 && TYPE_LENGTH (type
) == 1);
193 /* Return true if TYPE is a Rust character type. */
196 rust_chartype_p (struct type
*type
)
198 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
199 && TYPE_LENGTH (type
) == 4
200 && TYPE_UNSIGNED (type
));
203 /* If VALUE represents a trait object pointer, return the underlying
204 pointer with the correct (i.e., runtime) type. Otherwise, return
207 static struct value
*
208 rust_get_trait_object_pointer (struct value
*value
)
210 struct type
*type
= check_typedef (value_type (value
));
212 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
215 /* Try to be a bit resilient if the ABI changes. */
216 int vtable_field
= 0;
217 for (int i
= 0; i
< 2; ++i
)
219 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
221 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
225 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
226 struct symbol
*symbol
= find_symbol_at_address (vtable
);
227 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
230 struct rust_vtable_symbol
*vtable_sym
231 = static_cast<struct rust_vtable_symbol
*> (symbol
);
232 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
233 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
238 /* la_emitchar implementation for Rust. */
241 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
243 if (!rust_chartype_p (type
))
244 generic_emit_char (c
, type
, stream
, quoter
,
245 target_charset (get_type_arch (type
)));
246 else if (c
== '\\' || c
== quoter
)
247 fprintf_filtered (stream
, "\\%c", c
);
249 fputs_filtered ("\\n", stream
);
251 fputs_filtered ("\\r", stream
);
253 fputs_filtered ("\\t", stream
);
255 fputs_filtered ("\\0", stream
);
256 else if (c
>= 32 && c
<= 127 && isprint (c
))
257 fputc_filtered (c
, stream
);
259 fprintf_filtered (stream
, "\\x%02x", c
);
261 fprintf_filtered (stream
, "\\u{%06x}", c
);
264 /* la_printchar implementation for Rust. */
267 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
269 fputs_filtered ("'", stream
);
270 LA_EMIT_CHAR (c
, type
, stream
, '\'');
271 fputs_filtered ("'", stream
);
274 /* la_printstr implementation for Rust. */
277 rust_printstr (struct ui_file
*stream
, struct type
*type
,
278 const gdb_byte
*string
, unsigned int length
,
279 const char *user_encoding
, int force_ellipses
,
280 const struct value_print_options
*options
)
282 /* Rust always uses UTF-8, but let the caller override this if need
284 const char *encoding
= user_encoding
;
285 if (user_encoding
== NULL
|| !*user_encoding
)
287 /* In Rust strings, characters are "u8". */
288 if (rust_u8_type_p (type
))
292 /* This is probably some C string, so let's let C deal with
294 c_printstr (stream
, type
, string
, length
, user_encoding
,
295 force_ellipses
, options
);
300 /* This is not ideal as it doesn't use our character printer. */
301 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
307 /* Helper function to print a string slice. */
310 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
311 const struct value_print_options
*options
)
313 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
315 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
317 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
318 value_as_address (base
), value_as_long (len
), stream
,
322 /* rust_val_print helper for structs and untagged unions. */
325 val_print_struct (struct type
*type
, int embedded_offset
,
326 CORE_ADDR address
, struct ui_file
*stream
,
327 int recurse
, struct value
*val
,
328 const struct value_print_options
*options
)
333 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
335 rust_val_print_str (stream
, val
, options
);
339 bool is_tuple
= rust_tuple_type_p (type
);
340 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
341 struct value_print_options opts
;
345 if (TYPE_TAG_NAME (type
) != NULL
)
346 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
348 if (TYPE_NFIELDS (type
) == 0)
351 if (TYPE_TAG_NAME (type
) != NULL
)
352 fputs_filtered (" ", stream
);
355 if (is_tuple
|| is_tuple_struct
)
356 fputs_filtered ("(", stream
);
358 fputs_filtered ("{", stream
);
364 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
366 if (field_is_static (&TYPE_FIELD (type
, i
)))
370 fputs_filtered (",", stream
);
372 if (options
->prettyformat
)
374 fputs_filtered ("\n", stream
);
375 print_spaces_filtered (2 + 2 * recurse
, stream
);
377 else if (!first_field
)
378 fputs_filtered (" ", stream
);
382 if (!is_tuple
&& !is_tuple_struct
)
384 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
385 fputs_filtered (": ", stream
);
388 val_print (TYPE_FIELD_TYPE (type
, i
),
389 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
391 stream
, recurse
+ 1, val
, &opts
,
395 if (options
->prettyformat
)
397 fputs_filtered ("\n", stream
);
398 print_spaces_filtered (2 * recurse
, stream
);
401 if (is_tuple
|| is_tuple_struct
)
402 fputs_filtered (")", stream
);
404 fputs_filtered ("}", stream
);
407 /* rust_val_print helper for discriminated unions (Rust enums). */
410 rust_print_enum (struct type
*type
, int embedded_offset
,
411 CORE_ADDR address
, struct ui_file
*stream
,
412 int recurse
, struct value
*val
,
413 const struct value_print_options
*options
)
415 struct value_print_options opts
= *options
;
419 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
420 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
421 embedded_offset
+= FIELD_BITPOS (*variant_field
) / 8;
422 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
424 int nfields
= TYPE_NFIELDS (variant_type
);
426 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
428 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
431 /* In case of a nullary variant like 'None', just output
436 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
438 fprintf_filtered (stream
, "(");
441 /* struct variant. */
442 fprintf_filtered (stream
, "{");
445 bool first_field
= true;
446 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
449 fputs_filtered (", ", stream
);
453 fprintf_filtered (stream
, "%s: ",
454 TYPE_FIELD_NAME (variant_type
, j
));
456 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
458 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
460 stream
, recurse
+ 1, val
, &opts
,
465 fputs_filtered (")", stream
);
467 fputs_filtered ("}", stream
);
470 static const struct generic_val_print_decorations rust_decorations
=
472 /* Complex isn't used in Rust, but we provide C-ish values just in
484 /* la_val_print implementation for Rust. */
487 rust_val_print (struct type
*type
, int embedded_offset
,
488 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
490 const struct value_print_options
*options
)
492 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
494 type
= check_typedef (type
);
495 switch (TYPE_CODE (type
))
499 LONGEST low_bound
, high_bound
;
501 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
502 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
503 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
505 /* We have a pointer to a byte string, so just print
507 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
509 struct gdbarch
*arch
= get_type_arch (type
);
510 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
512 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
513 if (options
->addressprint
)
515 fputs_filtered (paddress (arch
, addr
), stream
);
516 fputs_filtered (" ", stream
);
519 fputs_filtered ("b", stream
);
520 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
521 high_bound
- low_bound
+ 1, stream
,
528 case TYPE_CODE_METHODPTR
:
529 case TYPE_CODE_MEMBERPTR
:
530 c_val_print (type
, embedded_offset
, address
, stream
,
531 recurse
, val
, options
);
535 /* Recognize the unit type. */
536 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
537 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
539 fputs_filtered ("()", stream
);
544 case TYPE_CODE_STRING
:
546 struct gdbarch
*arch
= get_type_arch (type
);
547 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
548 LONGEST low_bound
, high_bound
;
550 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
551 error (_("Could not determine the array bounds"));
553 /* If we see a plain TYPE_CODE_STRING, then we're printing a
554 byte string, hence the choice of "ASCII" as the
556 fputs_filtered ("b", stream
);
557 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
558 valaddr
+ embedded_offset
* unit_size
,
559 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
563 case TYPE_CODE_ARRAY
:
565 LONGEST low_bound
, high_bound
;
567 if (get_array_bounds (type
, &low_bound
, &high_bound
)
568 && high_bound
- low_bound
+ 1 == 0)
569 fputs_filtered ("[]", stream
);
575 case TYPE_CODE_UNION
:
576 /* Untagged unions are printed as if they are structs. Since
577 the field bit positions overlap in the debuginfo, the code
578 for printing a union is same as that for a struct, the only
579 difference is that the input type will have overlapping
581 val_print_struct (type
, embedded_offset
, address
, stream
,
582 recurse
, val
, options
);
585 case TYPE_CODE_STRUCT
:
586 if (rust_enum_p (type
))
587 rust_print_enum (type
, embedded_offset
, address
, stream
,
588 recurse
, val
, options
);
590 val_print_struct (type
, embedded_offset
, address
, stream
,
591 recurse
, val
, options
);
596 /* Nothing special yet. */
597 generic_val_print (type
, embedded_offset
, address
, stream
,
598 recurse
, val
, options
, &rust_decorations
);
605 rust_internal_print_type (struct type
*type
, const char *varstring
,
606 struct ui_file
*stream
, int show
, int level
,
607 const struct type_print_options
*flags
,
610 /* Print a struct or union typedef. */
612 rust_print_struct_def (struct type
*type
, const char *varstring
,
613 struct ui_file
*stream
, int show
, int level
,
614 const struct type_print_options
*flags
,
617 /* Print a tuple type simply. */
618 if (rust_tuple_type_p (type
))
620 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
624 /* If we see a base class, delegate to C. */
625 if (TYPE_N_BASECLASSES (type
) > 0)
626 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
628 /* Compute properties of TYPE here because, in the enum case, the
629 rest of the code ends up looking only at the variant part. */
630 const char *tagname
= TYPE_TAG_NAME (type
);
631 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
632 bool is_tuple
= rust_tuple_type_p (type
);
633 bool is_enum
= rust_enum_p (type
);
634 bool is_univariant
= false;
636 int enum_discriminant_index
= -1;
640 /* Already printing an outer enum, so nothing to print here. */
644 /* This code path is also used by unions and enums. */
647 fputs_filtered ("enum ", stream
);
648 type
= TYPE_FIELD_TYPE (type
, 0);
650 struct dynamic_prop
*discriminant_prop
651 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
652 struct discriminant_info
*info
653 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
654 enum_discriminant_index
= info
->discriminant_index
;
656 else if (TYPE_CODE (type
) == TYPE_CODE_UNION
&& TYPE_NFIELDS (type
) == 1)
658 /* Probably a univariant enum. */
659 fputs_filtered ("enum ", stream
);
660 is_univariant
= true;
662 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
663 fputs_filtered ("struct ", stream
);
665 fputs_filtered ("union ", stream
);
668 fputs_filtered (tagname
, stream
);
671 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
674 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
676 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
678 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
681 if (field_is_static (&TYPE_FIELD (type
, i
)))
684 /* We'd like to print "pub" here as needed, but rustc
685 doesn't emit the debuginfo, and our types don't have
686 cplus_struct_type attached. */
688 /* For a tuple struct we print the type but nothing
691 print_spaces_filtered (level
+ 2, stream
);
694 if (i
== enum_discriminant_index
)
696 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
698 else if (is_univariant
)
701 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, i
)));
702 fputs_filtered (name
, stream
);
704 else if (!is_tuple_struct
)
705 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
707 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
709 ((is_enum
|| is_univariant
) ? show
: show
- 1),
710 level
+ 2, flags
, is_enum
|| is_univariant
);
712 fputs_filtered (",\n", stream
);
713 else if (i
+ 1 < TYPE_NFIELDS (type
))
714 fputs_filtered (", ", stream
);
718 print_spaces_filtered (level
, stream
);
719 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
722 /* la_print_typedef implementation for Rust. */
725 rust_print_typedef (struct type
*type
,
726 struct symbol
*new_symbol
,
727 struct ui_file
*stream
)
729 type
= check_typedef (type
);
730 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
731 type_print (type
, "", stream
, 0);
732 fprintf_filtered (stream
, ";\n");
735 /* la_print_type implementation for Rust. */
738 rust_internal_print_type (struct type
*type
, const char *varstring
,
739 struct ui_file
*stream
, int show
, int level
,
740 const struct type_print_options
*flags
,
747 && TYPE_NAME (type
) != NULL
)
749 /* Rust calls the unit type "void" in its debuginfo,
750 but we don't want to print it as that. */
751 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
752 fputs_filtered ("()", stream
);
754 fputs_filtered (TYPE_NAME (type
), stream
);
758 type
= check_typedef (type
);
759 switch (TYPE_CODE (type
))
762 /* If we have an enum, we've already printed the type's
763 unqualified name, and there is nothing else to print
766 fputs_filtered ("()", stream
);
770 /* Delegate varargs to the C printer. */
771 if (TYPE_VARARGS (type
))
774 fputs_filtered ("fn ", stream
);
775 if (varstring
!= NULL
)
776 fputs_filtered (varstring
, stream
);
777 fputs_filtered ("(", stream
);
778 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
782 fputs_filtered (", ", stream
);
783 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
784 -1, 0, flags
, false);
786 fputs_filtered (")", stream
);
787 /* If it returns unit, we can omit the return type. */
788 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
790 fputs_filtered (" -> ", stream
);
791 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
792 -1, 0, flags
, false);
796 case TYPE_CODE_ARRAY
:
798 LONGEST low_bound
, high_bound
;
800 fputs_filtered ("[", stream
);
801 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
802 stream
, show
- 1, level
, flags
, false);
804 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
805 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
806 fprintf_filtered (stream
, "; variable length");
807 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
808 fprintf_filtered (stream
, "; %s",
809 plongest (high_bound
- low_bound
+ 1));
810 fputs_filtered ("]", stream
);
814 case TYPE_CODE_UNION
:
815 case TYPE_CODE_STRUCT
:
816 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
824 fputs_filtered ("enum ", stream
);
825 if (TYPE_TAG_NAME (type
) != NULL
)
827 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
828 fputs_filtered (" ", stream
);
829 len
= strlen (TYPE_TAG_NAME (type
));
831 fputs_filtered ("{\n", stream
);
833 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
835 const char *name
= TYPE_FIELD_NAME (type
, i
);
840 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
842 && name
[len
+ 1] == ':')
844 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
847 fputs_filtered ("}", stream
);
853 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
858 rust_print_type (struct type
*type
, const char *varstring
,
859 struct ui_file
*stream
, int show
, int level
,
860 const struct type_print_options
*flags
)
862 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
868 /* Compute the alignment of the type T. */
871 rust_type_alignment (struct type
*t
)
873 t
= check_typedef (t
);
874 switch (TYPE_CODE (t
))
877 error (_("Could not compute alignment of type"));
886 return TYPE_LENGTH (t
);
888 case TYPE_CODE_ARRAY
:
889 case TYPE_CODE_COMPLEX
:
890 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
892 case TYPE_CODE_STRUCT
:
893 case TYPE_CODE_UNION
:
898 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
900 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
909 /* Like arch_composite_type, but uses TYPE to decide how to allocate
910 -- either on an obstack or on a gdbarch. */
913 rust_composite_type (struct type
*original
,
915 const char *field1
, struct type
*type1
,
916 const char *field2
, struct type
*type2
)
918 struct type
*result
= alloc_type_copy (original
);
919 int i
, nfields
, bitpos
;
927 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
928 TYPE_NAME (result
) = name
;
929 TYPE_TAG_NAME (result
) = name
;
931 TYPE_NFIELDS (result
) = nfields
;
933 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
939 struct field
*field
= &TYPE_FIELD (result
, i
);
941 SET_FIELD_BITPOS (*field
, bitpos
);
942 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
944 FIELD_NAME (*field
) = field1
;
945 FIELD_TYPE (*field
) = type1
;
950 struct field
*field
= &TYPE_FIELD (result
, i
);
951 int align
= rust_type_alignment (type2
);
957 align
*= TARGET_CHAR_BIT
;
958 delta
= bitpos
% align
;
960 bitpos
+= align
- delta
;
962 SET_FIELD_BITPOS (*field
, bitpos
);
964 FIELD_NAME (*field
) = field2
;
965 FIELD_TYPE (*field
) = type2
;
971 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
972 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
976 /* See rust-lang.h. */
979 rust_slice_type (const char *name
, struct type
*elt_type
,
980 struct type
*usize_type
)
984 elt_type
= lookup_pointer_type (elt_type
);
985 type
= rust_composite_type (elt_type
, name
,
986 "data_ptr", elt_type
,
987 "length", usize_type
);
992 enum rust_primitive_types
1004 rust_primitive_isize
,
1005 rust_primitive_usize
,
1008 rust_primitive_unit
,
1010 nr_rust_primitive_types
1013 /* la_language_arch_info implementation for Rust. */
1016 rust_language_arch_info (struct gdbarch
*gdbarch
,
1017 struct language_arch_info
*lai
)
1019 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1021 struct type
**types
;
1022 unsigned int length
;
1024 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1027 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1028 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1029 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1030 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1031 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1032 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1033 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1034 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1035 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1036 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1038 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1039 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1040 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1042 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1043 floatformats_ieee_single
);
1044 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1045 floatformats_ieee_double
);
1047 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1049 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1050 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1051 types
[rust_primitive_usize
]);
1053 lai
->primitive_type_vector
= types
;
1054 lai
->bool_type_default
= types
[rust_primitive_bool
];
1055 lai
->string_char_type
= types
[rust_primitive_u8
];
1060 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1062 static struct value
*
1063 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1066 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1068 struct value
*function
, *result
, *arg0
;
1069 struct type
*type
, *fn_type
;
1070 const struct block
*block
;
1071 struct block_symbol sym
;
1073 /* For an ordinary function call we can simply defer to the
1074 generic implementation. */
1075 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1076 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1078 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1080 method
= &exp
->elts
[*pos
+ 1].string
;
1081 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1083 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1084 type in order to look up the method. */
1085 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1087 if (noside
== EVAL_SKIP
)
1089 for (i
= 0; i
< num_args
; ++i
)
1090 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1094 std::vector
<struct value
*> args (num_args
+ 1);
1097 /* We don't yet implement real Deref semantics. */
1098 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1099 args
[0] = value_ind (args
[0]);
1101 type
= value_type (args
[0]);
1102 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1103 && TYPE_CODE (type
) != TYPE_CODE_UNION
1104 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1105 || rust_tuple_type_p (type
))
1106 error (_("Method calls only supported on struct or enum types"));
1107 if (TYPE_TAG_NAME (type
) == NULL
)
1108 error (_("Method call on nameless type"));
1110 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1112 block
= get_selected_block (0);
1113 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1114 if (sym
.symbol
== NULL
)
1115 error (_("Could not find function named '%s'"), name
.c_str ());
1117 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1118 if (TYPE_NFIELDS (fn_type
) == 0)
1119 error (_("Function '%s' takes no arguments"), name
.c_str ());
1121 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1122 args
[0] = value_addr (args
[0]);
1124 function
= address_of_variable (sym
.symbol
, block
);
1126 for (i
= 0; i
< num_args
; ++i
)
1127 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1129 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1130 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1132 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1136 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1138 static struct value
*
1139 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1141 enum range_type kind
;
1142 struct value
*low
= NULL
, *high
= NULL
;
1143 struct value
*addrval
, *result
;
1145 struct type
*range_type
;
1146 struct type
*index_type
;
1147 struct type
*temp_type
;
1150 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1153 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1154 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1155 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1156 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1158 if (noside
== EVAL_SKIP
)
1159 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1166 name
= "std::ops::RangeFull";
1170 index_type
= value_type (high
);
1171 name
= "std::ops::RangeTo";
1178 index_type
= value_type (low
);
1179 name
= "std::ops::RangeFrom";
1183 if (!types_equal (value_type (low
), value_type (high
)))
1184 error (_("Range expression with different types"));
1185 index_type
= value_type (low
);
1186 name
= "std::ops::Range";
1190 /* If we don't have an index type, just allocate this on the
1191 arch. Here any type will do. */
1192 temp_type
= (index_type
== NULL
1193 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1195 /* It would be nicer to cache the range type. */
1196 range_type
= rust_composite_type (temp_type
, name
,
1197 low
== NULL
? NULL
: "start", index_type
,
1198 high
== NULL
? NULL
: "end", index_type
);
1200 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1201 return value_zero (range_type
, lval_memory
);
1203 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1204 addr
= value_as_long (addrval
);
1205 result
= value_at_lazy (range_type
, addr
);
1209 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1212 value_assign (start
, low
);
1217 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1220 value_assign (end
, high
);
1223 result
= value_at_lazy (range_type
, addr
);
1227 /* A helper function to compute the range and kind given a range
1228 value. TYPE is the type of the range value. RANGE is the range
1229 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1230 parameters might be filled in, or might not be, depending on the
1231 kind of range this is. KIND will always be set to the appropriate
1232 value describing the kind of range, and this can be used to
1233 determine whether LOW or HIGH are valid. */
1236 rust_compute_range (struct type
*type
, struct value
*range
,
1237 LONGEST
*low
, LONGEST
*high
,
1238 enum range_type
*kind
)
1244 *kind
= BOTH_BOUND_DEFAULT
;
1246 if (TYPE_NFIELDS (type
) == 0)
1250 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1252 *kind
= HIGH_BOUND_DEFAULT
;
1253 *low
= value_as_long (value_field (range
, 0));
1256 if (TYPE_NFIELDS (type
) > i
1257 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1259 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1260 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1261 *high
= value_as_long (value_field (range
, i
));
1265 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1267 static struct value
*
1268 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1271 struct value
*lhs
, *rhs
, *result
;
1272 struct type
*rhstype
;
1273 LONGEST low
, high_bound
;
1274 /* Initialized to appease the compiler. */
1275 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1280 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1281 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1283 if (noside
== EVAL_SKIP
)
1286 rhstype
= check_typedef (value_type (rhs
));
1287 if (rust_range_type_p (rhstype
))
1290 error (_("Can't take slice of array without '&'"));
1291 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1295 low
= value_as_long (rhs
);
1297 struct type
*type
= check_typedef (value_type (lhs
));
1298 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1300 struct type
*base_type
= nullptr;
1301 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1302 base_type
= TYPE_TARGET_TYPE (type
);
1303 else if (rust_slice_type_p (type
))
1305 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1307 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1309 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1313 if (base_type
== nullptr)
1314 error (_("Could not find 'data_ptr' in slice type"));
1316 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1317 base_type
= TYPE_TARGET_TYPE (type
);
1319 error (_("Cannot subscript non-array type"));
1321 struct type
*new_type
;
1324 if (rust_slice_type_p (type
))
1329 = language_lookup_primitive_type (exp
->language_defn
,
1332 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1336 new_type
= base_type
;
1338 return value_zero (new_type
, VALUE_LVAL (lhs
));
1345 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1348 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1349 error (_("Can't compute array bounds"));
1351 error (_("Found array with non-zero lower bound"));
1354 else if (rust_slice_type_p (type
))
1358 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1359 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1361 high_bound
= value_as_long (len
);
1363 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1367 high_bound
= LONGEST_MAX
;
1370 error (_("Cannot subscript non-array type"));
1373 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1376 error (_("Index less than zero"));
1377 if (low
> high_bound
)
1378 error (_("Index greater than length"));
1380 result
= value_subscript (base
, low
);
1387 struct type
*usize
, *slice
;
1389 struct value
*addrval
, *tem
;
1391 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1394 error (_("High index less than zero"));
1396 error (_("Low index greater than high index"));
1397 if (high
> high_bound
)
1398 error (_("High index greater than length"));
1400 usize
= language_lookup_primitive_type (exp
->language_defn
,
1403 const char *new_name
= ((type
!= nullptr
1404 && rust_slice_type_p (type
))
1405 ? TYPE_NAME (type
) : "&[*gdb*]");
1407 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1409 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1410 addr
= value_as_long (addrval
);
1411 tem
= value_at_lazy (slice
, addr
);
1413 value_assign (value_field (tem
, 0), value_addr (result
));
1414 value_assign (value_field (tem
, 1),
1415 value_from_longest (usize
, high
- low
));
1417 result
= value_at_lazy (slice
, addr
);
1420 result
= value_addr (result
);
1426 /* evaluate_exp implementation for Rust. */
1428 static struct value
*
1429 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1430 int *pos
, enum noside noside
)
1432 struct value
*result
;
1434 switch (exp
->elts
[*pos
].opcode
)
1438 if (noside
!= EVAL_NORMAL
)
1439 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1443 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1446 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1447 if (trait_ptr
!= NULL
)
1450 result
= value_ind (value
);
1455 case UNOP_COMPLEMENT
:
1457 struct value
*value
;
1460 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1461 if (noside
== EVAL_SKIP
)
1463 /* Preserving the type is enough. */
1466 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1467 result
= value_from_longest (value_type (value
),
1468 value_logical_not (value
));
1470 result
= value_complement (value
);
1474 case BINOP_SUBSCRIPT
:
1475 result
= rust_subscript (exp
, pos
, noside
, 0);
1479 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1485 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1486 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1489 struct value
*addrval
= NULL
;
1493 if (noside
== EVAL_NORMAL
)
1495 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1496 addr
= value_as_long (addrval
);
1497 result
= value_at_lazy (type
, addr
);
1500 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1505 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1506 if (noside
== EVAL_NORMAL
)
1508 /* This isn't quite right but will do for the time
1509 being, seeing that we can't implement the Copy
1511 value_assign (result
, init
);
1517 gdb_assert (arglen
% 2 == 0);
1518 for (i
= 0; i
< arglen
; i
+= 2)
1521 const char *fieldname
;
1522 struct value
*value
, *field
;
1524 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1526 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1528 fieldname
= &exp
->elts
[*pos
].string
;
1529 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1531 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1532 if (noside
== EVAL_NORMAL
)
1534 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1536 value_assign (field
, value
);
1540 if (noside
== EVAL_SKIP
)
1541 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1543 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1544 result
= allocate_value (type
);
1546 result
= value_at_lazy (type
, addr
);
1555 struct value
*ncopies
;
1557 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1558 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1559 copies
= value_as_long (ncopies
);
1561 error (_("Array with negative number of elements"));
1563 if (noside
== EVAL_NORMAL
)
1566 std::vector
<struct value
*> eltvec (copies
);
1568 for (i
= 0; i
< copies
; ++i
)
1570 result
= value_array (0, copies
- 1, eltvec
.data ());
1574 struct type
*arraytype
1575 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1576 result
= allocate_value (arraytype
);
1581 case STRUCTOP_ANONYMOUS
:
1583 /* Anonymous field access, i.e. foo.1. */
1585 int pc
, field_number
, nfields
;
1586 struct type
*type
, *variant_type
;
1589 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1591 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1593 type
= value_type (lhs
);
1595 /* Treat a univariant union as if it were an enum. */
1596 if (TYPE_CODE (type
) == TYPE_CODE_UNION
&& TYPE_NFIELDS (type
) == 1)
1598 lhs
= value_primitive_field (lhs
, 0, 0, type
);
1599 type
= value_type (lhs
);
1602 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1604 struct type
*outer_type
= NULL
;
1606 if (rust_enum_p (type
))
1608 const gdb_byte
*valaddr
= value_contents (lhs
);
1609 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1611 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1614 int fieldno
= (variant_field
1615 - &TYPE_FIELD (value_type (union_value
), 0));
1616 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1617 value_type (union_value
));
1619 type
= value_type (lhs
);
1622 /* Tuples and tuple structs */
1623 nfields
= TYPE_NFIELDS (type
);
1625 if (field_number
>= nfields
|| field_number
< 0)
1627 if (outer_type
!= NULL
)
1628 error(_("Cannot access field %d of variant %s::%s, "
1629 "there are only %d fields"),
1630 field_number
, TYPE_TAG_NAME (outer_type
),
1631 rust_last_path_segment (TYPE_TAG_NAME (type
)),
1634 error(_("Cannot access field %d of %s, "
1635 "there are only %d fields"),
1636 field_number
, TYPE_TAG_NAME (type
), nfields
);
1639 /* Tuples are tuple structs too. */
1640 if (!rust_tuple_struct_type_p (type
))
1642 if (outer_type
!= NULL
)
1643 error(_("Variant %s::%s is not a tuple variant"),
1644 TYPE_TAG_NAME (outer_type
),
1645 rust_last_path_segment (TYPE_TAG_NAME (type
)));
1647 error(_("Attempting to access anonymous field %d "
1648 "of %s, which is not a tuple, tuple struct, or "
1649 "tuple-like variant"),
1650 field_number
, TYPE_TAG_NAME (type
));
1653 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1656 error(_("Anonymous field access is only allowed on tuples, \
1657 tuple structs, and tuple-like enum variants"));
1661 case STRUCTOP_STRUCT
:
1668 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1669 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1670 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1672 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1673 type
= value_type (lhs
);
1674 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1676 const gdb_byte
*valaddr
= value_contents (lhs
);
1677 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1679 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1682 int fieldno
= (variant_field
1683 - &TYPE_FIELD (value_type (union_value
), 0));
1684 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1685 value_type (union_value
));
1687 struct type
*outer_type
= type
;
1688 type
= value_type (lhs
);
1689 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1690 error (_("Attempting to access named field foo of tuple "
1691 "variant %s::%s, which has only anonymous fields"),
1692 TYPE_TAG_NAME (outer_type
),
1693 rust_last_path_segment (TYPE_NAME (type
)));
1697 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1700 CATCH (except
, RETURN_MASK_ERROR
)
1702 error (_("Could not find field %s of struct variant %s::%s"),
1703 field_name
, TYPE_TAG_NAME (outer_type
),
1704 rust_last_path_segment (TYPE_NAME (type
)));
1709 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1710 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1711 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1716 result
= rust_range (exp
, pos
, noside
);
1720 /* We might have &array[range], in which case we need to make a
1722 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1725 result
= rust_subscript (exp
, pos
, noside
, 1);
1730 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1737 /* operator_length implementation for Rust. */
1740 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1746 switch (exp
->elts
[pc
- 1].opcode
)
1749 /* We handle aggregate as a type and argument count. The first
1750 argument might be OP_OTHERS. After that the arguments
1751 alternate: first an OP_NAME, then an expression. */
1753 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1761 case STRUCTOP_ANONYMOUS
:
1772 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1780 /* op_name implementation for Rust. */
1783 rust_op_name (enum exp_opcode opcode
)
1788 return "OP_AGGREGATE";
1792 return op_name_standard (opcode
);
1796 /* dump_subexp_body implementation for Rust. */
1799 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1802 switch (exp
->elts
[elt
].opcode
)
1806 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1809 fprintf_filtered (stream
, "Type @");
1810 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1811 fprintf_filtered (stream
, " (");
1812 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1813 fprintf_filtered (stream
, "), length %d", length
);
1816 for (i
= 0; i
< length
; ++i
)
1817 elt
= dump_subexp (exp
, stream
, elt
);
1824 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1826 fprintf_filtered (stream
, "%s: %s",
1827 (exp
->elts
[elt
].opcode
== OP_STRING
1828 ? "string" : "name"),
1829 &exp
->elts
[elt
+ 2].string
);
1830 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1835 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1838 case STRUCTOP_ANONYMOUS
:
1842 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1844 fprintf_filtered (stream
, "Field number: %d", field_number
);
1845 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1854 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1861 /* print_subexp implementation for Rust. */
1864 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1865 enum precedence prec
)
1867 switch (exp
->elts
[*pos
].opcode
)
1871 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1874 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1875 fputs_filtered (" { ", stream
);
1878 for (i
= 0; i
< length
; ++i
)
1880 rust_print_subexp (exp
, pos
, stream
, prec
);
1881 fputs_filtered (", ", stream
);
1883 fputs_filtered (" }", stream
);
1889 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1891 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1892 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1898 fputs_filtered ("<<others>> (", stream
);
1900 rust_print_subexp (exp
, pos
, stream
, prec
);
1901 fputs_filtered (")", stream
);
1905 case STRUCTOP_ANONYMOUS
:
1907 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1910 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1911 fprintf_filtered (stream
, ".%d", tem
);
1917 fprintf_filtered (stream
, "[");
1918 rust_print_subexp (exp
, pos
, stream
, prec
);
1919 fprintf_filtered (stream
, "; ");
1920 rust_print_subexp (exp
, pos
, stream
, prec
);
1921 fprintf_filtered (stream
, "]");
1925 print_subexp_standard (exp
, pos
, stream
, prec
);
1930 /* operator_check implementation for Rust. */
1933 rust_operator_check (struct expression
*exp
, int pos
,
1934 int (*objfile_func
) (struct objfile
*objfile
,
1938 switch (exp
->elts
[pos
].opcode
)
1942 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1943 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1945 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1956 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1964 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1966 static struct block_symbol
1967 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1969 const struct block
*block
,
1970 const domain_enum domain
)
1972 struct block_symbol result
= {NULL
, NULL
};
1974 if (symbol_lookup_debug
)
1976 fprintf_unfiltered (gdb_stdlog
,
1977 "rust_lookup_symbol_non_local"
1978 " (%s, %s (scope %s), %s)\n",
1979 name
, host_address_to_string (block
),
1980 block_scope (block
), domain_name (domain
));
1983 /* Look up bare names in the block's scope. */
1984 std::string scopedname
;
1985 if (name
[cp_find_first_component (name
)] == '\0')
1987 const char *scope
= block_scope (block
);
1989 if (scope
[0] != '\0')
1991 scopedname
= std::string (scope
) + "::" + name
;
1992 name
= scopedname
.c_str ();
2000 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2001 if (result
.symbol
== NULL
)
2002 result
= lookup_global_symbol (name
, block
, domain
);
2009 /* la_sniff_from_mangled_name for Rust. */
2012 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2014 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2015 return *demangled
!= NULL
;
2020 /* la_watch_location_expression for Rust. */
2022 static gdb::unique_xmalloc_ptr
<char>
2023 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2025 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2026 std::string name
= type_to_string (type
);
2027 return gdb::unique_xmalloc_ptr
<char>
2028 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2034 static const struct exp_descriptor exp_descriptor_rust
=
2037 rust_operator_length
,
2038 rust_operator_check
,
2040 rust_dump_subexp_body
,
2041 rust_evaluate_subexp
2044 static const char *rust_extensions
[] =
2049 extern const struct language_defn rust_language_defn
=
2059 &exp_descriptor_rust
,
2063 rust_printchar
, /* Print a character constant */
2064 rust_printstr
, /* Function to print string constant */
2065 rust_emitchar
, /* Print a single char */
2066 rust_print_type
, /* Print a type using appropriate syntax */
2067 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2068 rust_val_print
, /* Print a value using appropriate syntax */
2069 c_value_print
, /* Print a top-level value */
2070 default_read_var_value
, /* la_read_var_value */
2071 NULL
, /* Language specific skip_trampoline */
2072 NULL
, /* name_of_this */
2073 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2074 basic_lookup_transparent_type
,/* lookup_transparent_type */
2075 gdb_demangle
, /* Language specific symbol demangler */
2076 rust_sniff_from_mangled_name
,
2077 NULL
, /* Language specific
2078 class_name_from_physname */
2079 c_op_print_tab
, /* expression operators for printing */
2080 1, /* c-style arrays */
2081 0, /* String lower bound */
2082 default_word_break_characters
,
2083 default_collect_symbol_completion_matches
,
2084 rust_language_arch_info
,
2085 default_print_array_index
,
2086 default_pass_by_reference
,
2088 rust_watch_location_expression
,
2089 NULL
, /* la_get_symbol_name_matcher */
2090 iterate_over_symbols
,
2091 default_search_name_hash
,
2092 &default_varobj_ops
,