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 is an inclusive range type, otherwise false.
184 This is only valid for types which are already known to be range
188 rust_inclusive_range_type_p (struct type
*type
)
190 return (strstr (TYPE_TAG_NAME (type
), "::RangeInclusive") != NULL
191 || strstr (TYPE_TAG_NAME (type
), "::RangeToInclusive") != NULL
);
194 /* Return true if TYPE seems to be the type "u8", otherwise false. */
197 rust_u8_type_p (struct type
*type
)
199 return (TYPE_CODE (type
) == TYPE_CODE_INT
200 && TYPE_UNSIGNED (type
)
201 && TYPE_LENGTH (type
) == 1);
204 /* Return true if TYPE is a Rust character type. */
207 rust_chartype_p (struct type
*type
)
209 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
210 && TYPE_LENGTH (type
) == 4
211 && TYPE_UNSIGNED (type
));
214 /* If VALUE represents a trait object pointer, return the underlying
215 pointer with the correct (i.e., runtime) type. Otherwise, return
218 static struct value
*
219 rust_get_trait_object_pointer (struct value
*value
)
221 struct type
*type
= check_typedef (value_type (value
));
223 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
226 /* Try to be a bit resilient if the ABI changes. */
227 int vtable_field
= 0;
228 for (int i
= 0; i
< 2; ++i
)
230 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
232 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
236 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
237 struct symbol
*symbol
= find_symbol_at_address (vtable
);
238 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
241 struct rust_vtable_symbol
*vtable_sym
242 = static_cast<struct rust_vtable_symbol
*> (symbol
);
243 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
244 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
249 /* la_emitchar implementation for Rust. */
252 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
254 if (!rust_chartype_p (type
))
255 generic_emit_char (c
, type
, stream
, quoter
,
256 target_charset (get_type_arch (type
)));
257 else if (c
== '\\' || c
== quoter
)
258 fprintf_filtered (stream
, "\\%c", c
);
260 fputs_filtered ("\\n", stream
);
262 fputs_filtered ("\\r", stream
);
264 fputs_filtered ("\\t", stream
);
266 fputs_filtered ("\\0", stream
);
267 else if (c
>= 32 && c
<= 127 && isprint (c
))
268 fputc_filtered (c
, stream
);
270 fprintf_filtered (stream
, "\\x%02x", c
);
272 fprintf_filtered (stream
, "\\u{%06x}", c
);
275 /* la_printchar implementation for Rust. */
278 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
280 fputs_filtered ("'", stream
);
281 LA_EMIT_CHAR (c
, type
, stream
, '\'');
282 fputs_filtered ("'", stream
);
285 /* la_printstr implementation for Rust. */
288 rust_printstr (struct ui_file
*stream
, struct type
*type
,
289 const gdb_byte
*string
, unsigned int length
,
290 const char *user_encoding
, int force_ellipses
,
291 const struct value_print_options
*options
)
293 /* Rust always uses UTF-8, but let the caller override this if need
295 const char *encoding
= user_encoding
;
296 if (user_encoding
== NULL
|| !*user_encoding
)
298 /* In Rust strings, characters are "u8". */
299 if (rust_u8_type_p (type
))
303 /* This is probably some C string, so let's let C deal with
305 c_printstr (stream
, type
, string
, length
, user_encoding
,
306 force_ellipses
, options
);
311 /* This is not ideal as it doesn't use our character printer. */
312 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
318 /* Helper function to print a string slice. */
321 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
322 const struct value_print_options
*options
)
324 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
326 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
328 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
329 value_as_address (base
), value_as_long (len
), stream
,
333 /* rust_val_print helper for structs and untagged unions. */
336 val_print_struct (struct type
*type
, int embedded_offset
,
337 CORE_ADDR address
, struct ui_file
*stream
,
338 int recurse
, struct value
*val
,
339 const struct value_print_options
*options
)
344 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
346 rust_val_print_str (stream
, val
, options
);
350 bool is_tuple
= rust_tuple_type_p (type
);
351 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
352 struct value_print_options opts
;
356 if (TYPE_TAG_NAME (type
) != NULL
)
357 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
359 if (TYPE_NFIELDS (type
) == 0)
362 if (TYPE_TAG_NAME (type
) != NULL
)
363 fputs_filtered (" ", stream
);
366 if (is_tuple
|| is_tuple_struct
)
367 fputs_filtered ("(", stream
);
369 fputs_filtered ("{", stream
);
375 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
377 if (field_is_static (&TYPE_FIELD (type
, i
)))
381 fputs_filtered (",", stream
);
383 if (options
->prettyformat
)
385 fputs_filtered ("\n", stream
);
386 print_spaces_filtered (2 + 2 * recurse
, stream
);
388 else if (!first_field
)
389 fputs_filtered (" ", stream
);
393 if (!is_tuple
&& !is_tuple_struct
)
395 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
396 fputs_filtered (": ", stream
);
399 val_print (TYPE_FIELD_TYPE (type
, i
),
400 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
402 stream
, recurse
+ 1, val
, &opts
,
406 if (options
->prettyformat
)
408 fputs_filtered ("\n", stream
);
409 print_spaces_filtered (2 * recurse
, stream
);
412 if (is_tuple
|| is_tuple_struct
)
413 fputs_filtered (")", stream
);
415 fputs_filtered ("}", stream
);
418 /* rust_val_print helper for discriminated unions (Rust enums). */
421 rust_print_enum (struct type
*type
, int embedded_offset
,
422 CORE_ADDR address
, struct ui_file
*stream
,
423 int recurse
, struct value
*val
,
424 const struct value_print_options
*options
)
426 struct value_print_options opts
= *options
;
430 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
431 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
432 embedded_offset
+= FIELD_BITPOS (*variant_field
) / 8;
433 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
435 int nfields
= TYPE_NFIELDS (variant_type
);
437 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
439 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
442 /* In case of a nullary variant like 'None', just output
447 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
449 fprintf_filtered (stream
, "(");
452 /* struct variant. */
453 fprintf_filtered (stream
, "{");
456 bool first_field
= true;
457 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
460 fputs_filtered (", ", stream
);
464 fprintf_filtered (stream
, "%s: ",
465 TYPE_FIELD_NAME (variant_type
, j
));
467 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
469 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
471 stream
, recurse
+ 1, val
, &opts
,
476 fputs_filtered (")", stream
);
478 fputs_filtered ("}", stream
);
481 static const struct generic_val_print_decorations rust_decorations
=
483 /* Complex isn't used in Rust, but we provide C-ish values just in
495 /* la_val_print implementation for Rust. */
498 rust_val_print (struct type
*type
, int embedded_offset
,
499 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
501 const struct value_print_options
*options
)
503 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
505 type
= check_typedef (type
);
506 switch (TYPE_CODE (type
))
510 LONGEST low_bound
, high_bound
;
512 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
513 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
514 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
516 /* We have a pointer to a byte string, so just print
518 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
520 struct gdbarch
*arch
= get_type_arch (type
);
521 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
523 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
524 if (options
->addressprint
)
526 fputs_filtered (paddress (arch
, addr
), stream
);
527 fputs_filtered (" ", stream
);
530 fputs_filtered ("b", stream
);
531 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
532 high_bound
- low_bound
+ 1, stream
,
539 case TYPE_CODE_METHODPTR
:
540 case TYPE_CODE_MEMBERPTR
:
541 c_val_print (type
, embedded_offset
, address
, stream
,
542 recurse
, val
, options
);
546 /* Recognize the unit type. */
547 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
548 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
550 fputs_filtered ("()", stream
);
555 case TYPE_CODE_STRING
:
557 struct gdbarch
*arch
= get_type_arch (type
);
558 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
559 LONGEST low_bound
, high_bound
;
561 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
562 error (_("Could not determine the array bounds"));
564 /* If we see a plain TYPE_CODE_STRING, then we're printing a
565 byte string, hence the choice of "ASCII" as the
567 fputs_filtered ("b", stream
);
568 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
569 valaddr
+ embedded_offset
* unit_size
,
570 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
574 case TYPE_CODE_ARRAY
:
576 LONGEST low_bound
, high_bound
;
578 if (get_array_bounds (type
, &low_bound
, &high_bound
)
579 && high_bound
- low_bound
+ 1 == 0)
580 fputs_filtered ("[]", stream
);
586 case TYPE_CODE_UNION
:
587 /* Untagged unions are printed as if they are structs. Since
588 the field bit positions overlap in the debuginfo, the code
589 for printing a union is same as that for a struct, the only
590 difference is that the input type will have overlapping
592 val_print_struct (type
, embedded_offset
, address
, stream
,
593 recurse
, val
, options
);
596 case TYPE_CODE_STRUCT
:
597 if (rust_enum_p (type
))
598 rust_print_enum (type
, embedded_offset
, address
, stream
,
599 recurse
, val
, options
);
601 val_print_struct (type
, embedded_offset
, address
, stream
,
602 recurse
, val
, options
);
607 /* Nothing special yet. */
608 generic_val_print (type
, embedded_offset
, address
, stream
,
609 recurse
, val
, options
, &rust_decorations
);
616 rust_internal_print_type (struct type
*type
, const char *varstring
,
617 struct ui_file
*stream
, int show
, int level
,
618 const struct type_print_options
*flags
,
621 /* Print a struct or union typedef. */
623 rust_print_struct_def (struct type
*type
, const char *varstring
,
624 struct ui_file
*stream
, int show
, int level
,
625 const struct type_print_options
*flags
,
628 /* Print a tuple type simply. */
629 if (rust_tuple_type_p (type
))
631 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
635 /* If we see a base class, delegate to C. */
636 if (TYPE_N_BASECLASSES (type
) > 0)
637 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
639 /* Compute properties of TYPE here because, in the enum case, the
640 rest of the code ends up looking only at the variant part. */
641 const char *tagname
= TYPE_TAG_NAME (type
);
642 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
643 bool is_tuple
= rust_tuple_type_p (type
);
644 bool is_enum
= rust_enum_p (type
);
646 int enum_discriminant_index
= -1;
650 /* Already printing an outer enum, so nothing to print here. */
654 /* This code path is also used by unions and enums. */
657 fputs_filtered ("enum ", stream
);
658 type
= TYPE_FIELD_TYPE (type
, 0);
660 struct dynamic_prop
*discriminant_prop
661 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
662 struct discriminant_info
*info
663 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
664 enum_discriminant_index
= info
->discriminant_index
;
666 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
667 fputs_filtered ("struct ", stream
);
669 fputs_filtered ("union ", stream
);
672 fputs_filtered (tagname
, stream
);
675 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
678 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
680 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
682 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
685 if (field_is_static (&TYPE_FIELD (type
, i
)))
688 /* We'd like to print "pub" here as needed, but rustc
689 doesn't emit the debuginfo, and our types don't have
690 cplus_struct_type attached. */
692 /* For a tuple struct we print the type but nothing
695 print_spaces_filtered (level
+ 2, stream
);
698 if (i
== enum_discriminant_index
)
700 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
702 else if (!is_tuple_struct
)
703 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
705 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
706 stream
, (is_enum
? show
: show
- 1),
707 level
+ 2, flags
, is_enum
);
709 fputs_filtered (",\n", stream
);
710 else if (i
+ 1 < TYPE_NFIELDS (type
))
711 fputs_filtered (", ", stream
);
715 print_spaces_filtered (level
, stream
);
716 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
719 /* la_print_typedef implementation for Rust. */
722 rust_print_typedef (struct type
*type
,
723 struct symbol
*new_symbol
,
724 struct ui_file
*stream
)
726 type
= check_typedef (type
);
727 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
728 type_print (type
, "", stream
, 0);
729 fprintf_filtered (stream
, ";\n");
732 /* la_print_type implementation for Rust. */
735 rust_internal_print_type (struct type
*type
, const char *varstring
,
736 struct ui_file
*stream
, int show
, int level
,
737 const struct type_print_options
*flags
,
744 && TYPE_NAME (type
) != NULL
)
746 /* Rust calls the unit type "void" in its debuginfo,
747 but we don't want to print it as that. */
748 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
749 fputs_filtered ("()", stream
);
751 fputs_filtered (TYPE_NAME (type
), stream
);
755 type
= check_typedef (type
);
756 switch (TYPE_CODE (type
))
759 /* If we have an enum, we've already printed the type's
760 unqualified name, and there is nothing else to print
763 fputs_filtered ("()", stream
);
767 /* Delegate varargs to the C printer. */
768 if (TYPE_VARARGS (type
))
771 fputs_filtered ("fn ", stream
);
772 if (varstring
!= NULL
)
773 fputs_filtered (varstring
, stream
);
774 fputs_filtered ("(", stream
);
775 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
779 fputs_filtered (", ", stream
);
780 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
781 -1, 0, flags
, false);
783 fputs_filtered (")", stream
);
784 /* If it returns unit, we can omit the return type. */
785 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
787 fputs_filtered (" -> ", stream
);
788 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
789 -1, 0, flags
, false);
793 case TYPE_CODE_ARRAY
:
795 LONGEST low_bound
, high_bound
;
797 fputs_filtered ("[", stream
);
798 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
799 stream
, show
- 1, level
, flags
, false);
801 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
802 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
803 fprintf_filtered (stream
, "; variable length");
804 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
805 fprintf_filtered (stream
, "; %s",
806 plongest (high_bound
- low_bound
+ 1));
807 fputs_filtered ("]", stream
);
811 case TYPE_CODE_UNION
:
812 case TYPE_CODE_STRUCT
:
813 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
821 fputs_filtered ("enum ", stream
);
822 if (TYPE_TAG_NAME (type
) != NULL
)
824 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
825 fputs_filtered (" ", stream
);
826 len
= strlen (TYPE_TAG_NAME (type
));
828 fputs_filtered ("{\n", stream
);
830 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
832 const char *name
= TYPE_FIELD_NAME (type
, i
);
837 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
839 && name
[len
+ 1] == ':')
841 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
844 fputs_filtered ("}", stream
);
850 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
855 rust_print_type (struct type
*type
, const char *varstring
,
856 struct ui_file
*stream
, int show
, int level
,
857 const struct type_print_options
*flags
)
859 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
865 /* Compute the alignment of the type T. */
868 rust_type_alignment (struct type
*t
)
870 t
= check_typedef (t
);
871 switch (TYPE_CODE (t
))
874 error (_("Could not compute alignment of type"));
883 return TYPE_LENGTH (t
);
885 case TYPE_CODE_ARRAY
:
886 case TYPE_CODE_COMPLEX
:
887 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
889 case TYPE_CODE_STRUCT
:
890 case TYPE_CODE_UNION
:
895 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
897 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
906 /* Like arch_composite_type, but uses TYPE to decide how to allocate
907 -- either on an obstack or on a gdbarch. */
910 rust_composite_type (struct type
*original
,
912 const char *field1
, struct type
*type1
,
913 const char *field2
, struct type
*type2
)
915 struct type
*result
= alloc_type_copy (original
);
916 int i
, nfields
, bitpos
;
924 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
925 TYPE_NAME (result
) = name
;
926 TYPE_TAG_NAME (result
) = name
;
928 TYPE_NFIELDS (result
) = nfields
;
930 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
936 struct field
*field
= &TYPE_FIELD (result
, i
);
938 SET_FIELD_BITPOS (*field
, bitpos
);
939 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
941 FIELD_NAME (*field
) = field1
;
942 FIELD_TYPE (*field
) = type1
;
947 struct field
*field
= &TYPE_FIELD (result
, i
);
948 int align
= rust_type_alignment (type2
);
954 align
*= TARGET_CHAR_BIT
;
955 delta
= bitpos
% align
;
957 bitpos
+= align
- delta
;
959 SET_FIELD_BITPOS (*field
, bitpos
);
961 FIELD_NAME (*field
) = field2
;
962 FIELD_TYPE (*field
) = type2
;
968 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
969 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
973 /* See rust-lang.h. */
976 rust_slice_type (const char *name
, struct type
*elt_type
,
977 struct type
*usize_type
)
981 elt_type
= lookup_pointer_type (elt_type
);
982 type
= rust_composite_type (elt_type
, name
,
983 "data_ptr", elt_type
,
984 "length", usize_type
);
989 enum rust_primitive_types
1001 rust_primitive_isize
,
1002 rust_primitive_usize
,
1005 rust_primitive_unit
,
1007 nr_rust_primitive_types
1010 /* la_language_arch_info implementation for Rust. */
1013 rust_language_arch_info (struct gdbarch
*gdbarch
,
1014 struct language_arch_info
*lai
)
1016 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1018 struct type
**types
;
1019 unsigned int length
;
1021 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1024 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1025 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1026 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1027 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1028 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1029 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1030 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1031 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1032 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1033 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1035 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1036 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1037 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1039 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1040 floatformats_ieee_single
);
1041 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1042 floatformats_ieee_double
);
1044 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1046 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1047 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1048 types
[rust_primitive_usize
]);
1050 lai
->primitive_type_vector
= types
;
1051 lai
->bool_type_default
= types
[rust_primitive_bool
];
1052 lai
->string_char_type
= types
[rust_primitive_u8
];
1057 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1059 static struct value
*
1060 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1063 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1065 struct value
*function
, *result
, *arg0
;
1066 struct type
*type
, *fn_type
;
1067 const struct block
*block
;
1068 struct block_symbol sym
;
1070 /* For an ordinary function call we can simply defer to the
1071 generic implementation. */
1072 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1073 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1075 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1077 method
= &exp
->elts
[*pos
+ 1].string
;
1078 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1080 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1081 type in order to look up the method. */
1082 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1084 if (noside
== EVAL_SKIP
)
1086 for (i
= 0; i
< num_args
; ++i
)
1087 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1091 std::vector
<struct value
*> args (num_args
+ 1);
1094 /* We don't yet implement real Deref semantics. */
1095 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1096 args
[0] = value_ind (args
[0]);
1098 type
= value_type (args
[0]);
1099 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1100 && TYPE_CODE (type
) != TYPE_CODE_UNION
1101 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1102 || rust_tuple_type_p (type
))
1103 error (_("Method calls only supported on struct or enum types"));
1104 if (TYPE_TAG_NAME (type
) == NULL
)
1105 error (_("Method call on nameless type"));
1107 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1109 block
= get_selected_block (0);
1110 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1111 if (sym
.symbol
== NULL
)
1112 error (_("Could not find function named '%s'"), name
.c_str ());
1114 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1115 if (TYPE_NFIELDS (fn_type
) == 0)
1116 error (_("Function '%s' takes no arguments"), name
.c_str ());
1118 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1119 args
[0] = value_addr (args
[0]);
1121 function
= address_of_variable (sym
.symbol
, block
);
1123 for (i
= 0; i
< num_args
; ++i
)
1124 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1126 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1127 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1129 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1133 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1135 static struct value
*
1136 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1138 enum range_type kind
;
1139 struct value
*low
= NULL
, *high
= NULL
;
1140 struct value
*addrval
, *result
;
1142 struct type
*range_type
;
1143 struct type
*index_type
;
1144 struct type
*temp_type
;
1147 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1150 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1151 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1152 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1153 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1154 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1155 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1156 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
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
);
1172 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1179 index_type
= value_type (low
);
1180 name
= "std::ops::RangeFrom";
1184 if (!types_equal (value_type (low
), value_type (high
)))
1185 error (_("Range expression with different types"));
1186 index_type
= value_type (low
);
1187 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1191 /* If we don't have an index type, just allocate this on the
1192 arch. Here any type will do. */
1193 temp_type
= (index_type
== NULL
1194 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1196 /* It would be nicer to cache the range type. */
1197 range_type
= rust_composite_type (temp_type
, name
,
1198 low
== NULL
? NULL
: "start", index_type
,
1199 high
== NULL
? NULL
: "end", index_type
);
1201 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1202 return value_zero (range_type
, lval_memory
);
1204 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1205 addr
= value_as_long (addrval
);
1206 result
= value_at_lazy (range_type
, addr
);
1210 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1213 value_assign (start
, low
);
1218 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1221 value_assign (end
, high
);
1224 result
= value_at_lazy (range_type
, addr
);
1228 /* A helper function to compute the range and kind given a range
1229 value. TYPE is the type of the range value. RANGE is the range
1230 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1231 parameters might be filled in, or might not be, depending on the
1232 kind of range this is. KIND will always be set to the appropriate
1233 value describing the kind of range, and this can be used to
1234 determine whether LOW or HIGH are valid. */
1237 rust_compute_range (struct type
*type
, struct value
*range
,
1238 LONGEST
*low
, LONGEST
*high
,
1239 enum range_type
*kind
)
1245 *kind
= BOTH_BOUND_DEFAULT
;
1247 if (TYPE_NFIELDS (type
) == 0)
1251 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1253 *kind
= HIGH_BOUND_DEFAULT
;
1254 *low
= value_as_long (value_field (range
, 0));
1257 if (TYPE_NFIELDS (type
) > i
1258 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1260 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1261 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1262 *high
= value_as_long (value_field (range
, i
));
1264 if (rust_inclusive_range_type_p (type
))
1269 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1271 static struct value
*
1272 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1275 struct value
*lhs
, *rhs
, *result
;
1276 struct type
*rhstype
;
1277 LONGEST low
, high_bound
;
1278 /* Initialized to appease the compiler. */
1279 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1284 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1285 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1287 if (noside
== EVAL_SKIP
)
1290 rhstype
= check_typedef (value_type (rhs
));
1291 if (rust_range_type_p (rhstype
))
1294 error (_("Can't take slice of array without '&'"));
1295 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1299 low
= value_as_long (rhs
);
1301 struct type
*type
= check_typedef (value_type (lhs
));
1302 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1304 struct type
*base_type
= nullptr;
1305 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1306 base_type
= TYPE_TARGET_TYPE (type
);
1307 else if (rust_slice_type_p (type
))
1309 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1311 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1313 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1317 if (base_type
== nullptr)
1318 error (_("Could not find 'data_ptr' in slice type"));
1320 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1321 base_type
= TYPE_TARGET_TYPE (type
);
1323 error (_("Cannot subscript non-array type"));
1325 struct type
*new_type
;
1328 if (rust_slice_type_p (type
))
1333 = language_lookup_primitive_type (exp
->language_defn
,
1336 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1340 new_type
= base_type
;
1342 return value_zero (new_type
, VALUE_LVAL (lhs
));
1349 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1352 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1353 error (_("Can't compute array bounds"));
1355 error (_("Found array with non-zero lower bound"));
1358 else if (rust_slice_type_p (type
))
1362 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1363 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1365 high_bound
= value_as_long (len
);
1367 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1371 high_bound
= LONGEST_MAX
;
1374 error (_("Cannot subscript non-array type"));
1377 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1380 error (_("Index less than zero"));
1381 if (low
> high_bound
)
1382 error (_("Index greater than length"));
1384 result
= value_subscript (base
, low
);
1391 struct type
*usize
, *slice
;
1393 struct value
*addrval
, *tem
;
1395 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1398 error (_("High index less than zero"));
1400 error (_("Low index greater than high index"));
1401 if (high
> high_bound
)
1402 error (_("High index greater than length"));
1404 usize
= language_lookup_primitive_type (exp
->language_defn
,
1407 const char *new_name
= ((type
!= nullptr
1408 && rust_slice_type_p (type
))
1409 ? TYPE_NAME (type
) : "&[*gdb*]");
1411 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1413 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1414 addr
= value_as_long (addrval
);
1415 tem
= value_at_lazy (slice
, addr
);
1417 value_assign (value_field (tem
, 0), value_addr (result
));
1418 value_assign (value_field (tem
, 1),
1419 value_from_longest (usize
, high
- low
));
1421 result
= value_at_lazy (slice
, addr
);
1424 result
= value_addr (result
);
1430 /* evaluate_exp implementation for Rust. */
1432 static struct value
*
1433 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1434 int *pos
, enum noside noside
)
1436 struct value
*result
;
1438 switch (exp
->elts
[*pos
].opcode
)
1442 if (noside
!= EVAL_NORMAL
)
1443 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1447 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1450 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1451 if (trait_ptr
!= NULL
)
1454 result
= value_ind (value
);
1459 case UNOP_COMPLEMENT
:
1461 struct value
*value
;
1464 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1465 if (noside
== EVAL_SKIP
)
1467 /* Preserving the type is enough. */
1470 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1471 result
= value_from_longest (value_type (value
),
1472 value_logical_not (value
));
1474 result
= value_complement (value
);
1478 case BINOP_SUBSCRIPT
:
1479 result
= rust_subscript (exp
, pos
, noside
, 0);
1483 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1489 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1490 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1493 struct value
*addrval
= NULL
;
1497 if (noside
== EVAL_NORMAL
)
1499 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1500 addr
= value_as_long (addrval
);
1501 result
= value_at_lazy (type
, addr
);
1504 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1509 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1510 if (noside
== EVAL_NORMAL
)
1512 /* This isn't quite right but will do for the time
1513 being, seeing that we can't implement the Copy
1515 value_assign (result
, init
);
1521 gdb_assert (arglen
% 2 == 0);
1522 for (i
= 0; i
< arglen
; i
+= 2)
1525 const char *fieldname
;
1526 struct value
*value
, *field
;
1528 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1530 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1532 fieldname
= &exp
->elts
[*pos
].string
;
1533 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1535 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1536 if (noside
== EVAL_NORMAL
)
1538 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1540 value_assign (field
, value
);
1544 if (noside
== EVAL_SKIP
)
1545 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1547 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1548 result
= allocate_value (type
);
1550 result
= value_at_lazy (type
, addr
);
1559 struct value
*ncopies
;
1561 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1562 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1563 copies
= value_as_long (ncopies
);
1565 error (_("Array with negative number of elements"));
1567 if (noside
== EVAL_NORMAL
)
1570 std::vector
<struct value
*> eltvec (copies
);
1572 for (i
= 0; i
< copies
; ++i
)
1574 result
= value_array (0, copies
- 1, eltvec
.data ());
1578 struct type
*arraytype
1579 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1580 result
= allocate_value (arraytype
);
1585 case STRUCTOP_ANONYMOUS
:
1587 /* Anonymous field access, i.e. foo.1. */
1589 int pc
, field_number
, nfields
;
1590 struct type
*type
, *variant_type
;
1593 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1595 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1597 type
= value_type (lhs
);
1599 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1601 struct type
*outer_type
= NULL
;
1603 if (rust_enum_p (type
))
1605 const gdb_byte
*valaddr
= value_contents (lhs
);
1606 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1608 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1611 int fieldno
= (variant_field
1612 - &TYPE_FIELD (value_type (union_value
), 0));
1613 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1614 value_type (union_value
));
1616 type
= value_type (lhs
);
1619 /* Tuples and tuple structs */
1620 nfields
= TYPE_NFIELDS (type
);
1622 if (field_number
>= nfields
|| field_number
< 0)
1624 if (outer_type
!= NULL
)
1625 error(_("Cannot access field %d of variant %s::%s, "
1626 "there are only %d fields"),
1627 field_number
, TYPE_TAG_NAME (outer_type
),
1628 rust_last_path_segment (TYPE_TAG_NAME (type
)),
1631 error(_("Cannot access field %d of %s, "
1632 "there are only %d fields"),
1633 field_number
, TYPE_TAG_NAME (type
), nfields
);
1636 /* Tuples are tuple structs too. */
1637 if (!rust_tuple_struct_type_p (type
))
1639 if (outer_type
!= NULL
)
1640 error(_("Variant %s::%s is not a tuple variant"),
1641 TYPE_TAG_NAME (outer_type
),
1642 rust_last_path_segment (TYPE_TAG_NAME (type
)));
1644 error(_("Attempting to access anonymous field %d "
1645 "of %s, which is not a tuple, tuple struct, or "
1646 "tuple-like variant"),
1647 field_number
, TYPE_TAG_NAME (type
));
1650 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1653 error(_("Anonymous field access is only allowed on tuples, \
1654 tuple structs, and tuple-like enum variants"));
1658 case STRUCTOP_STRUCT
:
1665 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1666 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1667 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1669 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1670 type
= value_type (lhs
);
1671 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1673 const gdb_byte
*valaddr
= value_contents (lhs
);
1674 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1676 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1679 int fieldno
= (variant_field
1680 - &TYPE_FIELD (value_type (union_value
), 0));
1681 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1682 value_type (union_value
));
1684 struct type
*outer_type
= type
;
1685 type
= value_type (lhs
);
1686 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1687 error (_("Attempting to access named field foo of tuple "
1688 "variant %s::%s, which has only anonymous fields"),
1689 TYPE_TAG_NAME (outer_type
),
1690 rust_last_path_segment (TYPE_NAME (type
)));
1694 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1697 CATCH (except
, RETURN_MASK_ERROR
)
1699 error (_("Could not find field %s of struct variant %s::%s"),
1700 field_name
, TYPE_TAG_NAME (outer_type
),
1701 rust_last_path_segment (TYPE_NAME (type
)));
1706 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1707 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1708 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1713 result
= rust_range (exp
, pos
, noside
);
1717 /* We might have &array[range], in which case we need to make a
1719 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1722 result
= rust_subscript (exp
, pos
, noside
, 1);
1727 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1734 /* operator_length implementation for Rust. */
1737 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1743 switch (exp
->elts
[pc
- 1].opcode
)
1746 /* We handle aggregate as a type and argument count. The first
1747 argument might be OP_OTHERS. After that the arguments
1748 alternate: first an OP_NAME, then an expression. */
1750 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1758 case STRUCTOP_ANONYMOUS
:
1769 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1777 /* op_name implementation for Rust. */
1780 rust_op_name (enum exp_opcode opcode
)
1785 return "OP_AGGREGATE";
1789 return op_name_standard (opcode
);
1793 /* dump_subexp_body implementation for Rust. */
1796 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1799 switch (exp
->elts
[elt
].opcode
)
1803 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1806 fprintf_filtered (stream
, "Type @");
1807 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1808 fprintf_filtered (stream
, " (");
1809 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1810 fprintf_filtered (stream
, "), length %d", length
);
1813 for (i
= 0; i
< length
; ++i
)
1814 elt
= dump_subexp (exp
, stream
, elt
);
1821 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1823 fprintf_filtered (stream
, "%s: %s",
1824 (exp
->elts
[elt
].opcode
== OP_STRING
1825 ? "string" : "name"),
1826 &exp
->elts
[elt
+ 2].string
);
1827 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1832 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1835 case STRUCTOP_ANONYMOUS
:
1839 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1841 fprintf_filtered (stream
, "Field number: %d", field_number
);
1842 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1851 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1858 /* print_subexp implementation for Rust. */
1861 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1862 enum precedence prec
)
1864 switch (exp
->elts
[*pos
].opcode
)
1868 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1871 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1872 fputs_filtered (" { ", stream
);
1875 for (i
= 0; i
< length
; ++i
)
1877 rust_print_subexp (exp
, pos
, stream
, prec
);
1878 fputs_filtered (", ", stream
);
1880 fputs_filtered (" }", stream
);
1886 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1888 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1889 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1895 fputs_filtered ("<<others>> (", stream
);
1897 rust_print_subexp (exp
, pos
, stream
, prec
);
1898 fputs_filtered (")", stream
);
1902 case STRUCTOP_ANONYMOUS
:
1904 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1907 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1908 fprintf_filtered (stream
, ".%d", tem
);
1914 fprintf_filtered (stream
, "[");
1915 rust_print_subexp (exp
, pos
, stream
, prec
);
1916 fprintf_filtered (stream
, "; ");
1917 rust_print_subexp (exp
, pos
, stream
, prec
);
1918 fprintf_filtered (stream
, "]");
1922 print_subexp_standard (exp
, pos
, stream
, prec
);
1927 /* operator_check implementation for Rust. */
1930 rust_operator_check (struct expression
*exp
, int pos
,
1931 int (*objfile_func
) (struct objfile
*objfile
,
1935 switch (exp
->elts
[pos
].opcode
)
1939 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1940 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1942 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1953 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1961 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1963 static struct block_symbol
1964 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1966 const struct block
*block
,
1967 const domain_enum domain
)
1969 struct block_symbol result
= {NULL
, NULL
};
1971 if (symbol_lookup_debug
)
1973 fprintf_unfiltered (gdb_stdlog
,
1974 "rust_lookup_symbol_non_local"
1975 " (%s, %s (scope %s), %s)\n",
1976 name
, host_address_to_string (block
),
1977 block_scope (block
), domain_name (domain
));
1980 /* Look up bare names in the block's scope. */
1981 std::string scopedname
;
1982 if (name
[cp_find_first_component (name
)] == '\0')
1984 const char *scope
= block_scope (block
);
1986 if (scope
[0] != '\0')
1988 scopedname
= std::string (scope
) + "::" + name
;
1989 name
= scopedname
.c_str ();
1997 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1998 if (result
.symbol
== NULL
)
1999 result
= lookup_global_symbol (name
, block
, domain
);
2006 /* la_sniff_from_mangled_name for Rust. */
2009 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2011 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2012 return *demangled
!= NULL
;
2017 /* la_watch_location_expression for Rust. */
2019 static gdb::unique_xmalloc_ptr
<char>
2020 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2022 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2023 std::string name
= type_to_string (type
);
2024 return gdb::unique_xmalloc_ptr
<char>
2025 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2031 static const struct exp_descriptor exp_descriptor_rust
=
2034 rust_operator_length
,
2035 rust_operator_check
,
2037 rust_dump_subexp_body
,
2038 rust_evaluate_subexp
2041 static const char *rust_extensions
[] =
2046 extern const struct language_defn rust_language_defn
=
2056 &exp_descriptor_rust
,
2060 rust_printchar
, /* Print a character constant */
2061 rust_printstr
, /* Function to print string constant */
2062 rust_emitchar
, /* Print a single char */
2063 rust_print_type
, /* Print a type using appropriate syntax */
2064 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2065 rust_val_print
, /* Print a value using appropriate syntax */
2066 c_value_print
, /* Print a top-level value */
2067 default_read_var_value
, /* la_read_var_value */
2068 NULL
, /* Language specific skip_trampoline */
2069 NULL
, /* name_of_this */
2070 false, /* la_store_sym_names_in_linkage_form_p */
2071 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2072 basic_lookup_transparent_type
,/* lookup_transparent_type */
2073 gdb_demangle
, /* Language specific symbol demangler */
2074 rust_sniff_from_mangled_name
,
2075 NULL
, /* Language specific
2076 class_name_from_physname */
2077 c_op_print_tab
, /* expression operators for printing */
2078 1, /* c-style arrays */
2079 0, /* String lower bound */
2080 default_word_break_characters
,
2081 default_collect_symbol_completion_matches
,
2082 rust_language_arch_info
,
2083 default_print_array_index
,
2084 default_pass_by_reference
,
2086 rust_watch_location_expression
,
2087 NULL
, /* la_get_symbol_name_matcher */
2088 iterate_over_symbols
,
2089 default_search_name_hash
,
2090 &default_varobj_ops
,