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 /* Like arch_composite_type, but uses TYPE to decide how to allocate
866 -- either on an obstack or on a gdbarch. */
869 rust_composite_type (struct type
*original
,
871 const char *field1
, struct type
*type1
,
872 const char *field2
, struct type
*type2
)
874 struct type
*result
= alloc_type_copy (original
);
875 int i
, nfields
, bitpos
;
883 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
884 TYPE_NAME (result
) = name
;
885 TYPE_TAG_NAME (result
) = name
;
887 TYPE_NFIELDS (result
) = nfields
;
889 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
895 struct field
*field
= &TYPE_FIELD (result
, i
);
897 SET_FIELD_BITPOS (*field
, bitpos
);
898 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
900 FIELD_NAME (*field
) = field1
;
901 FIELD_TYPE (*field
) = type1
;
906 struct field
*field
= &TYPE_FIELD (result
, i
);
907 unsigned align
= type_align (type2
);
913 align
*= TARGET_CHAR_BIT
;
914 delta
= bitpos
% align
;
916 bitpos
+= align
- delta
;
918 SET_FIELD_BITPOS (*field
, bitpos
);
920 FIELD_NAME (*field
) = field2
;
921 FIELD_TYPE (*field
) = type2
;
927 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
928 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
932 /* See rust-lang.h. */
935 rust_slice_type (const char *name
, struct type
*elt_type
,
936 struct type
*usize_type
)
940 elt_type
= lookup_pointer_type (elt_type
);
941 type
= rust_composite_type (elt_type
, name
,
942 "data_ptr", elt_type
,
943 "length", usize_type
);
948 enum rust_primitive_types
960 rust_primitive_isize
,
961 rust_primitive_usize
,
966 nr_rust_primitive_types
969 /* la_language_arch_info implementation for Rust. */
972 rust_language_arch_info (struct gdbarch
*gdbarch
,
973 struct language_arch_info
*lai
)
975 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
980 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
983 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
984 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
985 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
986 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
987 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
988 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
989 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
990 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
991 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
992 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
994 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
995 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
996 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
998 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
999 floatformats_ieee_single
);
1000 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1001 floatformats_ieee_double
);
1003 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1005 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1006 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1007 types
[rust_primitive_usize
]);
1009 lai
->primitive_type_vector
= types
;
1010 lai
->bool_type_default
= types
[rust_primitive_bool
];
1011 lai
->string_char_type
= types
[rust_primitive_u8
];
1016 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1018 static struct value
*
1019 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1022 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1024 struct value
*function
, *result
, *arg0
;
1025 struct type
*type
, *fn_type
;
1026 const struct block
*block
;
1027 struct block_symbol sym
;
1029 /* For an ordinary function call we can simply defer to the
1030 generic implementation. */
1031 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1032 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1034 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1036 method
= &exp
->elts
[*pos
+ 1].string
;
1037 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1039 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1040 type in order to look up the method. */
1041 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1043 if (noside
== EVAL_SKIP
)
1045 for (i
= 0; i
< num_args
; ++i
)
1046 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1050 std::vector
<struct value
*> args (num_args
+ 1);
1053 /* We don't yet implement real Deref semantics. */
1054 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1055 args
[0] = value_ind (args
[0]);
1057 type
= value_type (args
[0]);
1058 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1059 && TYPE_CODE (type
) != TYPE_CODE_UNION
1060 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1061 || rust_tuple_type_p (type
))
1062 error (_("Method calls only supported on struct or enum types"));
1063 if (TYPE_TAG_NAME (type
) == NULL
)
1064 error (_("Method call on nameless type"));
1066 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1068 block
= get_selected_block (0);
1069 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1070 if (sym
.symbol
== NULL
)
1071 error (_("Could not find function named '%s'"), name
.c_str ());
1073 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1074 if (TYPE_NFIELDS (fn_type
) == 0)
1075 error (_("Function '%s' takes no arguments"), name
.c_str ());
1077 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1078 args
[0] = value_addr (args
[0]);
1080 function
= address_of_variable (sym
.symbol
, block
);
1082 for (i
= 0; i
< num_args
; ++i
)
1083 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1085 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1086 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1088 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1092 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1094 static struct value
*
1095 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1097 enum range_type kind
;
1098 struct value
*low
= NULL
, *high
= NULL
;
1099 struct value
*addrval
, *result
;
1101 struct type
*range_type
;
1102 struct type
*index_type
;
1103 struct type
*temp_type
;
1106 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1109 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1110 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1111 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1112 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1113 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1114 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1115 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1117 if (noside
== EVAL_SKIP
)
1118 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1125 name
= "std::ops::RangeFull";
1129 index_type
= value_type (high
);
1131 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1138 index_type
= value_type (low
);
1139 name
= "std::ops::RangeFrom";
1143 if (!types_equal (value_type (low
), value_type (high
)))
1144 error (_("Range expression with different types"));
1145 index_type
= value_type (low
);
1146 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1150 /* If we don't have an index type, just allocate this on the
1151 arch. Here any type will do. */
1152 temp_type
= (index_type
== NULL
1153 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1155 /* It would be nicer to cache the range type. */
1156 range_type
= rust_composite_type (temp_type
, name
,
1157 low
== NULL
? NULL
: "start", index_type
,
1158 high
== NULL
? NULL
: "end", index_type
);
1160 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1161 return value_zero (range_type
, lval_memory
);
1163 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1164 addr
= value_as_long (addrval
);
1165 result
= value_at_lazy (range_type
, addr
);
1169 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1172 value_assign (start
, low
);
1177 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1180 value_assign (end
, high
);
1183 result
= value_at_lazy (range_type
, addr
);
1187 /* A helper function to compute the range and kind given a range
1188 value. TYPE is the type of the range value. RANGE is the range
1189 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1190 parameters might be filled in, or might not be, depending on the
1191 kind of range this is. KIND will always be set to the appropriate
1192 value describing the kind of range, and this can be used to
1193 determine whether LOW or HIGH are valid. */
1196 rust_compute_range (struct type
*type
, struct value
*range
,
1197 LONGEST
*low
, LONGEST
*high
,
1198 enum range_type
*kind
)
1204 *kind
= BOTH_BOUND_DEFAULT
;
1206 if (TYPE_NFIELDS (type
) == 0)
1210 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1212 *kind
= HIGH_BOUND_DEFAULT
;
1213 *low
= value_as_long (value_field (range
, 0));
1216 if (TYPE_NFIELDS (type
) > i
1217 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1219 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1220 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1221 *high
= value_as_long (value_field (range
, i
));
1223 if (rust_inclusive_range_type_p (type
))
1228 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1230 static struct value
*
1231 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1234 struct value
*lhs
, *rhs
, *result
;
1235 struct type
*rhstype
;
1236 LONGEST low
, high_bound
;
1237 /* Initialized to appease the compiler. */
1238 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1243 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1244 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1246 if (noside
== EVAL_SKIP
)
1249 rhstype
= check_typedef (value_type (rhs
));
1250 if (rust_range_type_p (rhstype
))
1253 error (_("Can't take slice of array without '&'"));
1254 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1258 low
= value_as_long (rhs
);
1260 struct type
*type
= check_typedef (value_type (lhs
));
1261 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1263 struct type
*base_type
= nullptr;
1264 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1265 base_type
= TYPE_TARGET_TYPE (type
);
1266 else if (rust_slice_type_p (type
))
1268 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1270 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1272 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1276 if (base_type
== nullptr)
1277 error (_("Could not find 'data_ptr' in slice type"));
1279 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1280 base_type
= TYPE_TARGET_TYPE (type
);
1282 error (_("Cannot subscript non-array type"));
1284 struct type
*new_type
;
1287 if (rust_slice_type_p (type
))
1292 = language_lookup_primitive_type (exp
->language_defn
,
1295 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1299 new_type
= base_type
;
1301 return value_zero (new_type
, VALUE_LVAL (lhs
));
1308 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1311 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1312 error (_("Can't compute array bounds"));
1314 error (_("Found array with non-zero lower bound"));
1317 else if (rust_slice_type_p (type
))
1321 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1322 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1324 high_bound
= value_as_long (len
);
1326 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1330 high_bound
= LONGEST_MAX
;
1333 error (_("Cannot subscript non-array type"));
1336 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1339 error (_("Index less than zero"));
1340 if (low
> high_bound
)
1341 error (_("Index greater than length"));
1343 result
= value_subscript (base
, low
);
1350 struct type
*usize
, *slice
;
1352 struct value
*addrval
, *tem
;
1354 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1357 error (_("High index less than zero"));
1359 error (_("Low index greater than high index"));
1360 if (high
> high_bound
)
1361 error (_("High index greater than length"));
1363 usize
= language_lookup_primitive_type (exp
->language_defn
,
1366 const char *new_name
= ((type
!= nullptr
1367 && rust_slice_type_p (type
))
1368 ? TYPE_NAME (type
) : "&[*gdb*]");
1370 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1372 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1373 addr
= value_as_long (addrval
);
1374 tem
= value_at_lazy (slice
, addr
);
1376 value_assign (value_field (tem
, 0), value_addr (result
));
1377 value_assign (value_field (tem
, 1),
1378 value_from_longest (usize
, high
- low
));
1380 result
= value_at_lazy (slice
, addr
);
1383 result
= value_addr (result
);
1389 /* evaluate_exp implementation for Rust. */
1391 static struct value
*
1392 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1393 int *pos
, enum noside noside
)
1395 struct value
*result
;
1397 switch (exp
->elts
[*pos
].opcode
)
1401 if (noside
!= EVAL_NORMAL
)
1402 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1406 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1409 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1410 if (trait_ptr
!= NULL
)
1413 result
= value_ind (value
);
1418 case UNOP_COMPLEMENT
:
1420 struct value
*value
;
1423 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1424 if (noside
== EVAL_SKIP
)
1426 /* Preserving the type is enough. */
1429 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1430 result
= value_from_longest (value_type (value
),
1431 value_logical_not (value
));
1433 result
= value_complement (value
);
1437 case BINOP_SUBSCRIPT
:
1438 result
= rust_subscript (exp
, pos
, noside
, 0);
1442 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1448 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1449 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1452 struct value
*addrval
= NULL
;
1456 if (noside
== EVAL_NORMAL
)
1458 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1459 addr
= value_as_long (addrval
);
1460 result
= value_at_lazy (type
, addr
);
1463 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1468 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1469 if (noside
== EVAL_NORMAL
)
1471 /* This isn't quite right but will do for the time
1472 being, seeing that we can't implement the Copy
1474 value_assign (result
, init
);
1480 gdb_assert (arglen
% 2 == 0);
1481 for (i
= 0; i
< arglen
; i
+= 2)
1484 const char *fieldname
;
1485 struct value
*value
, *field
;
1487 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1489 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1491 fieldname
= &exp
->elts
[*pos
].string
;
1492 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1494 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1495 if (noside
== EVAL_NORMAL
)
1497 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1499 value_assign (field
, value
);
1503 if (noside
== EVAL_SKIP
)
1504 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1506 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1507 result
= allocate_value (type
);
1509 result
= value_at_lazy (type
, addr
);
1518 struct value
*ncopies
;
1520 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1521 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1522 copies
= value_as_long (ncopies
);
1524 error (_("Array with negative number of elements"));
1526 if (noside
== EVAL_NORMAL
)
1529 std::vector
<struct value
*> eltvec (copies
);
1531 for (i
= 0; i
< copies
; ++i
)
1533 result
= value_array (0, copies
- 1, eltvec
.data ());
1537 struct type
*arraytype
1538 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1539 result
= allocate_value (arraytype
);
1544 case STRUCTOP_ANONYMOUS
:
1546 /* Anonymous field access, i.e. foo.1. */
1548 int pc
, field_number
, nfields
;
1549 struct type
*type
, *variant_type
;
1552 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1554 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1556 type
= value_type (lhs
);
1558 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1560 struct type
*outer_type
= NULL
;
1562 if (rust_enum_p (type
))
1564 const gdb_byte
*valaddr
= value_contents (lhs
);
1565 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1567 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1570 int fieldno
= (variant_field
1571 - &TYPE_FIELD (value_type (union_value
), 0));
1572 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1573 value_type (union_value
));
1575 type
= value_type (lhs
);
1578 /* Tuples and tuple structs */
1579 nfields
= TYPE_NFIELDS (type
);
1581 if (field_number
>= nfields
|| field_number
< 0)
1583 if (outer_type
!= NULL
)
1584 error(_("Cannot access field %d of variant %s::%s, "
1585 "there are only %d fields"),
1586 field_number
, TYPE_TAG_NAME (outer_type
),
1587 rust_last_path_segment (TYPE_TAG_NAME (type
)),
1590 error(_("Cannot access field %d of %s, "
1591 "there are only %d fields"),
1592 field_number
, TYPE_TAG_NAME (type
), nfields
);
1595 /* Tuples are tuple structs too. */
1596 if (!rust_tuple_struct_type_p (type
))
1598 if (outer_type
!= NULL
)
1599 error(_("Variant %s::%s is not a tuple variant"),
1600 TYPE_TAG_NAME (outer_type
),
1601 rust_last_path_segment (TYPE_TAG_NAME (type
)));
1603 error(_("Attempting to access anonymous field %d "
1604 "of %s, which is not a tuple, tuple struct, or "
1605 "tuple-like variant"),
1606 field_number
, TYPE_TAG_NAME (type
));
1609 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1612 error(_("Anonymous field access is only allowed on tuples, \
1613 tuple structs, and tuple-like enum variants"));
1617 case STRUCTOP_STRUCT
:
1624 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1625 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1626 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1628 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1629 type
= value_type (lhs
);
1630 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1632 const gdb_byte
*valaddr
= value_contents (lhs
);
1633 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1635 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1638 int fieldno
= (variant_field
1639 - &TYPE_FIELD (value_type (union_value
), 0));
1640 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1641 value_type (union_value
));
1643 struct type
*outer_type
= type
;
1644 type
= value_type (lhs
);
1645 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1646 error (_("Attempting to access named field foo of tuple "
1647 "variant %s::%s, which has only anonymous fields"),
1648 TYPE_TAG_NAME (outer_type
),
1649 rust_last_path_segment (TYPE_NAME (type
)));
1653 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1656 CATCH (except
, RETURN_MASK_ERROR
)
1658 error (_("Could not find field %s of struct variant %s::%s"),
1659 field_name
, TYPE_TAG_NAME (outer_type
),
1660 rust_last_path_segment (TYPE_NAME (type
)));
1665 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1666 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1667 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1672 result
= rust_range (exp
, pos
, noside
);
1676 /* We might have &array[range], in which case we need to make a
1678 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1681 result
= rust_subscript (exp
, pos
, noside
, 1);
1686 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1693 /* operator_length implementation for Rust. */
1696 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1702 switch (exp
->elts
[pc
- 1].opcode
)
1705 /* We handle aggregate as a type and argument count. The first
1706 argument might be OP_OTHERS. After that the arguments
1707 alternate: first an OP_NAME, then an expression. */
1709 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1717 case STRUCTOP_ANONYMOUS
:
1728 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1736 /* op_name implementation for Rust. */
1739 rust_op_name (enum exp_opcode opcode
)
1744 return "OP_AGGREGATE";
1748 return op_name_standard (opcode
);
1752 /* dump_subexp_body implementation for Rust. */
1755 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1758 switch (exp
->elts
[elt
].opcode
)
1762 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1765 fprintf_filtered (stream
, "Type @");
1766 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1767 fprintf_filtered (stream
, " (");
1768 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1769 fprintf_filtered (stream
, "), length %d", length
);
1772 for (i
= 0; i
< length
; ++i
)
1773 elt
= dump_subexp (exp
, stream
, elt
);
1780 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1782 fprintf_filtered (stream
, "%s: %s",
1783 (exp
->elts
[elt
].opcode
== OP_STRING
1784 ? "string" : "name"),
1785 &exp
->elts
[elt
+ 2].string
);
1786 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1791 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1794 case STRUCTOP_ANONYMOUS
:
1798 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1800 fprintf_filtered (stream
, "Field number: %d", field_number
);
1801 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1810 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1817 /* print_subexp implementation for Rust. */
1820 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1821 enum precedence prec
)
1823 switch (exp
->elts
[*pos
].opcode
)
1827 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1830 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1831 fputs_filtered (" { ", stream
);
1834 for (i
= 0; i
< length
; ++i
)
1836 rust_print_subexp (exp
, pos
, stream
, prec
);
1837 fputs_filtered (", ", stream
);
1839 fputs_filtered (" }", stream
);
1845 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1847 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1848 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1854 fputs_filtered ("<<others>> (", stream
);
1856 rust_print_subexp (exp
, pos
, stream
, prec
);
1857 fputs_filtered (")", stream
);
1861 case STRUCTOP_ANONYMOUS
:
1863 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1866 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1867 fprintf_filtered (stream
, ".%d", tem
);
1873 fprintf_filtered (stream
, "[");
1874 rust_print_subexp (exp
, pos
, stream
, prec
);
1875 fprintf_filtered (stream
, "; ");
1876 rust_print_subexp (exp
, pos
, stream
, prec
);
1877 fprintf_filtered (stream
, "]");
1881 print_subexp_standard (exp
, pos
, stream
, prec
);
1886 /* operator_check implementation for Rust. */
1889 rust_operator_check (struct expression
*exp
, int pos
,
1890 int (*objfile_func
) (struct objfile
*objfile
,
1894 switch (exp
->elts
[pos
].opcode
)
1898 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1899 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1901 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1912 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1920 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1922 static struct block_symbol
1923 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1925 const struct block
*block
,
1926 const domain_enum domain
)
1928 struct block_symbol result
= {NULL
, NULL
};
1930 if (symbol_lookup_debug
)
1932 fprintf_unfiltered (gdb_stdlog
,
1933 "rust_lookup_symbol_non_local"
1934 " (%s, %s (scope %s), %s)\n",
1935 name
, host_address_to_string (block
),
1936 block_scope (block
), domain_name (domain
));
1939 /* Look up bare names in the block's scope. */
1940 std::string scopedname
;
1941 if (name
[cp_find_first_component (name
)] == '\0')
1943 const char *scope
= block_scope (block
);
1945 if (scope
[0] != '\0')
1947 scopedname
= std::string (scope
) + "::" + name
;
1948 name
= scopedname
.c_str ();
1956 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1957 if (result
.symbol
== NULL
)
1958 result
= lookup_global_symbol (name
, block
, domain
);
1965 /* la_sniff_from_mangled_name for Rust. */
1968 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
1970 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1971 return *demangled
!= NULL
;
1976 /* la_watch_location_expression for Rust. */
1978 static gdb::unique_xmalloc_ptr
<char>
1979 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
1981 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
1982 std::string name
= type_to_string (type
);
1983 return gdb::unique_xmalloc_ptr
<char>
1984 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
1990 static const struct exp_descriptor exp_descriptor_rust
=
1993 rust_operator_length
,
1994 rust_operator_check
,
1996 rust_dump_subexp_body
,
1997 rust_evaluate_subexp
2000 static const char *rust_extensions
[] =
2005 extern const struct language_defn rust_language_defn
=
2015 &exp_descriptor_rust
,
2019 rust_printchar
, /* Print a character constant */
2020 rust_printstr
, /* Function to print string constant */
2021 rust_emitchar
, /* Print a single char */
2022 rust_print_type
, /* Print a type using appropriate syntax */
2023 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2024 rust_val_print
, /* Print a value using appropriate syntax */
2025 c_value_print
, /* Print a top-level value */
2026 default_read_var_value
, /* la_read_var_value */
2027 NULL
, /* Language specific skip_trampoline */
2028 NULL
, /* name_of_this */
2029 false, /* la_store_sym_names_in_linkage_form_p */
2030 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2031 basic_lookup_transparent_type
,/* lookup_transparent_type */
2032 gdb_demangle
, /* Language specific symbol demangler */
2033 rust_sniff_from_mangled_name
,
2034 NULL
, /* Language specific
2035 class_name_from_physname */
2036 c_op_print_tab
, /* expression operators for printing */
2037 1, /* c-style arrays */
2038 0, /* String lower bound */
2039 default_word_break_characters
,
2040 default_collect_symbol_completion_matches
,
2041 rust_language_arch_info
,
2042 default_print_array_index
,
2043 default_pass_by_reference
,
2045 rust_watch_location_expression
,
2046 NULL
, /* la_get_symbol_name_matcher */
2047 iterate_over_symbols
,
2048 default_search_name_hash
,
2049 &default_varobj_ops
,