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_NAME (type
) != NULL
99 && TYPE_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_NAME (type
) != NULL
147 && (strncmp (TYPE_NAME (type
), "&[", 2) == 0
148 || strcmp (TYPE_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_NAME (type
) == NULL
161 || strstr (TYPE_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_NAME (type
), "::RangeInclusive") != NULL
191 || strstr (TYPE_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_NAME (type
) != NULL
)
357 fprintf_filtered (stream
, "%s", TYPE_NAME (type
));
359 if (TYPE_NFIELDS (type
) == 0)
362 if (TYPE_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_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_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_NAME (type
) != NULL
)
824 fputs_filtered (TYPE_NAME (type
), stream
);
825 fputs_filtered (" ", stream
);
826 len
= strlen (TYPE_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_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
;
886 TYPE_NFIELDS (result
) = nfields
;
888 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
894 struct field
*field
= &TYPE_FIELD (result
, i
);
896 SET_FIELD_BITPOS (*field
, bitpos
);
897 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
899 FIELD_NAME (*field
) = field1
;
900 FIELD_TYPE (*field
) = type1
;
905 struct field
*field
= &TYPE_FIELD (result
, i
);
906 unsigned align
= type_align (type2
);
912 align
*= TARGET_CHAR_BIT
;
913 delta
= bitpos
% align
;
915 bitpos
+= align
- delta
;
917 SET_FIELD_BITPOS (*field
, bitpos
);
919 FIELD_NAME (*field
) = field2
;
920 FIELD_TYPE (*field
) = type2
;
926 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
927 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
931 /* See rust-lang.h. */
934 rust_slice_type (const char *name
, struct type
*elt_type
,
935 struct type
*usize_type
)
939 elt_type
= lookup_pointer_type (elt_type
);
940 type
= rust_composite_type (elt_type
, name
,
941 "data_ptr", elt_type
,
942 "length", usize_type
);
947 enum rust_primitive_types
959 rust_primitive_isize
,
960 rust_primitive_usize
,
965 nr_rust_primitive_types
968 /* la_language_arch_info implementation for Rust. */
971 rust_language_arch_info (struct gdbarch
*gdbarch
,
972 struct language_arch_info
*lai
)
974 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
979 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
982 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
983 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
984 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
985 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
986 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
987 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
988 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
989 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
990 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
991 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
993 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
994 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
995 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
997 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
998 floatformats_ieee_single
);
999 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1000 floatformats_ieee_double
);
1002 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1004 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1005 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1006 types
[rust_primitive_usize
]);
1008 lai
->primitive_type_vector
= types
;
1009 lai
->bool_type_default
= types
[rust_primitive_bool
];
1010 lai
->string_char_type
= types
[rust_primitive_u8
];
1015 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1017 static struct value
*
1018 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1021 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1023 struct value
*function
, *result
, *arg0
;
1024 struct type
*type
, *fn_type
;
1025 const struct block
*block
;
1026 struct block_symbol sym
;
1028 /* For an ordinary function call we can simply defer to the
1029 generic implementation. */
1030 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1031 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1033 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1035 method
= &exp
->elts
[*pos
+ 1].string
;
1036 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1038 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1039 type in order to look up the method. */
1040 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1042 if (noside
== EVAL_SKIP
)
1044 for (i
= 0; i
< num_args
; ++i
)
1045 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1049 std::vector
<struct value
*> args (num_args
+ 1);
1052 /* We don't yet implement real Deref semantics. */
1053 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1054 args
[0] = value_ind (args
[0]);
1056 type
= value_type (args
[0]);
1057 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1058 && TYPE_CODE (type
) != TYPE_CODE_UNION
1059 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1060 || rust_tuple_type_p (type
))
1061 error (_("Method calls only supported on struct or enum types"));
1062 if (TYPE_NAME (type
) == NULL
)
1063 error (_("Method call on nameless type"));
1065 std::string name
= std::string (TYPE_NAME (type
)) + "::" + method
;
1067 block
= get_selected_block (0);
1068 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1069 if (sym
.symbol
== NULL
)
1070 error (_("Could not find function named '%s'"), name
.c_str ());
1072 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1073 if (TYPE_NFIELDS (fn_type
) == 0)
1074 error (_("Function '%s' takes no arguments"), name
.c_str ());
1076 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1077 args
[0] = value_addr (args
[0]);
1079 function
= address_of_variable (sym
.symbol
, block
);
1081 for (i
= 0; i
< num_args
; ++i
)
1082 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1084 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1085 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1087 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1091 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1093 static struct value
*
1094 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1096 enum range_type kind
;
1097 struct value
*low
= NULL
, *high
= NULL
;
1098 struct value
*addrval
, *result
;
1100 struct type
*range_type
;
1101 struct type
*index_type
;
1102 struct type
*temp_type
;
1105 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1108 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1109 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1110 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1111 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1112 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1113 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1114 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1116 if (noside
== EVAL_SKIP
)
1117 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1124 name
= "std::ops::RangeFull";
1128 index_type
= value_type (high
);
1130 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1137 index_type
= value_type (low
);
1138 name
= "std::ops::RangeFrom";
1142 if (!types_equal (value_type (low
), value_type (high
)))
1143 error (_("Range expression with different types"));
1144 index_type
= value_type (low
);
1145 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1149 /* If we don't have an index type, just allocate this on the
1150 arch. Here any type will do. */
1151 temp_type
= (index_type
== NULL
1152 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1154 /* It would be nicer to cache the range type. */
1155 range_type
= rust_composite_type (temp_type
, name
,
1156 low
== NULL
? NULL
: "start", index_type
,
1157 high
== NULL
? NULL
: "end", index_type
);
1159 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1160 return value_zero (range_type
, lval_memory
);
1162 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1163 addr
= value_as_long (addrval
);
1164 result
= value_at_lazy (range_type
, addr
);
1168 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1171 value_assign (start
, low
);
1176 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1179 value_assign (end
, high
);
1182 result
= value_at_lazy (range_type
, addr
);
1186 /* A helper function to compute the range and kind given a range
1187 value. TYPE is the type of the range value. RANGE is the range
1188 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1189 parameters might be filled in, or might not be, depending on the
1190 kind of range this is. KIND will always be set to the appropriate
1191 value describing the kind of range, and this can be used to
1192 determine whether LOW or HIGH are valid. */
1195 rust_compute_range (struct type
*type
, struct value
*range
,
1196 LONGEST
*low
, LONGEST
*high
,
1197 enum range_type
*kind
)
1203 *kind
= BOTH_BOUND_DEFAULT
;
1205 if (TYPE_NFIELDS (type
) == 0)
1209 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1211 *kind
= HIGH_BOUND_DEFAULT
;
1212 *low
= value_as_long (value_field (range
, 0));
1215 if (TYPE_NFIELDS (type
) > i
1216 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1218 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1219 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1220 *high
= value_as_long (value_field (range
, i
));
1222 if (rust_inclusive_range_type_p (type
))
1227 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1229 static struct value
*
1230 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1233 struct value
*lhs
, *rhs
, *result
;
1234 struct type
*rhstype
;
1235 LONGEST low
, high_bound
;
1236 /* Initialized to appease the compiler. */
1237 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1242 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1243 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1245 if (noside
== EVAL_SKIP
)
1248 rhstype
= check_typedef (value_type (rhs
));
1249 if (rust_range_type_p (rhstype
))
1252 error (_("Can't take slice of array without '&'"));
1253 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1257 low
= value_as_long (rhs
);
1259 struct type
*type
= check_typedef (value_type (lhs
));
1260 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1262 struct type
*base_type
= nullptr;
1263 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1264 base_type
= TYPE_TARGET_TYPE (type
);
1265 else if (rust_slice_type_p (type
))
1267 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1269 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1271 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1275 if (base_type
== nullptr)
1276 error (_("Could not find 'data_ptr' in slice type"));
1278 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1279 base_type
= TYPE_TARGET_TYPE (type
);
1281 error (_("Cannot subscript non-array type"));
1283 struct type
*new_type
;
1286 if (rust_slice_type_p (type
))
1291 = language_lookup_primitive_type (exp
->language_defn
,
1294 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1298 new_type
= base_type
;
1300 return value_zero (new_type
, VALUE_LVAL (lhs
));
1307 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1310 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1311 error (_("Can't compute array bounds"));
1313 error (_("Found array with non-zero lower bound"));
1316 else if (rust_slice_type_p (type
))
1320 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1321 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1323 high_bound
= value_as_long (len
);
1325 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1329 high_bound
= LONGEST_MAX
;
1332 error (_("Cannot subscript non-array type"));
1335 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1338 error (_("Index less than zero"));
1339 if (low
> high_bound
)
1340 error (_("Index greater than length"));
1342 result
= value_subscript (base
, low
);
1349 struct type
*usize
, *slice
;
1351 struct value
*addrval
, *tem
;
1353 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1356 error (_("High index less than zero"));
1358 error (_("Low index greater than high index"));
1359 if (high
> high_bound
)
1360 error (_("High index greater than length"));
1362 usize
= language_lookup_primitive_type (exp
->language_defn
,
1365 const char *new_name
= ((type
!= nullptr
1366 && rust_slice_type_p (type
))
1367 ? TYPE_NAME (type
) : "&[*gdb*]");
1369 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1371 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1372 addr
= value_as_long (addrval
);
1373 tem
= value_at_lazy (slice
, addr
);
1375 value_assign (value_field (tem
, 0), value_addr (result
));
1376 value_assign (value_field (tem
, 1),
1377 value_from_longest (usize
, high
- low
));
1379 result
= value_at_lazy (slice
, addr
);
1382 result
= value_addr (result
);
1388 /* evaluate_exp implementation for Rust. */
1390 static struct value
*
1391 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1392 int *pos
, enum noside noside
)
1394 struct value
*result
;
1396 switch (exp
->elts
[*pos
].opcode
)
1400 if (noside
!= EVAL_NORMAL
)
1401 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1405 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1408 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1409 if (trait_ptr
!= NULL
)
1412 result
= value_ind (value
);
1417 case UNOP_COMPLEMENT
:
1419 struct value
*value
;
1422 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1423 if (noside
== EVAL_SKIP
)
1425 /* Preserving the type is enough. */
1428 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1429 result
= value_from_longest (value_type (value
),
1430 value_logical_not (value
));
1432 result
= value_complement (value
);
1436 case BINOP_SUBSCRIPT
:
1437 result
= rust_subscript (exp
, pos
, noside
, 0);
1441 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1447 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1448 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1451 struct value
*addrval
= NULL
;
1455 if (noside
== EVAL_NORMAL
)
1457 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1458 addr
= value_as_long (addrval
);
1459 result
= value_at_lazy (type
, addr
);
1462 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1467 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1468 if (noside
== EVAL_NORMAL
)
1470 /* This isn't quite right but will do for the time
1471 being, seeing that we can't implement the Copy
1473 value_assign (result
, init
);
1479 gdb_assert (arglen
% 2 == 0);
1480 for (i
= 0; i
< arglen
; i
+= 2)
1483 const char *fieldname
;
1484 struct value
*value
, *field
;
1486 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1488 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1490 fieldname
= &exp
->elts
[*pos
].string
;
1491 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1493 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1494 if (noside
== EVAL_NORMAL
)
1496 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1498 value_assign (field
, value
);
1502 if (noside
== EVAL_SKIP
)
1503 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1505 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1506 result
= allocate_value (type
);
1508 result
= value_at_lazy (type
, addr
);
1517 struct value
*ncopies
;
1519 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1520 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1521 copies
= value_as_long (ncopies
);
1523 error (_("Array with negative number of elements"));
1525 if (noside
== EVAL_NORMAL
)
1528 std::vector
<struct value
*> eltvec (copies
);
1530 for (i
= 0; i
< copies
; ++i
)
1532 result
= value_array (0, copies
- 1, eltvec
.data ());
1536 struct type
*arraytype
1537 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1538 result
= allocate_value (arraytype
);
1543 case STRUCTOP_ANONYMOUS
:
1545 /* Anonymous field access, i.e. foo.1. */
1547 int pc
, field_number
, nfields
;
1548 struct type
*type
, *variant_type
;
1551 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1553 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1555 type
= value_type (lhs
);
1557 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1559 struct type
*outer_type
= NULL
;
1561 if (rust_enum_p (type
))
1563 const gdb_byte
*valaddr
= value_contents (lhs
);
1564 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1566 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1569 int fieldno
= (variant_field
1570 - &TYPE_FIELD (value_type (union_value
), 0));
1571 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1572 value_type (union_value
));
1574 type
= value_type (lhs
);
1577 /* Tuples and tuple structs */
1578 nfields
= TYPE_NFIELDS (type
);
1580 if (field_number
>= nfields
|| field_number
< 0)
1582 if (outer_type
!= NULL
)
1583 error(_("Cannot access field %d of variant %s::%s, "
1584 "there are only %d fields"),
1585 field_number
, TYPE_NAME (outer_type
),
1586 rust_last_path_segment (TYPE_NAME (type
)),
1589 error(_("Cannot access field %d of %s, "
1590 "there are only %d fields"),
1591 field_number
, TYPE_NAME (type
), nfields
);
1594 /* Tuples are tuple structs too. */
1595 if (!rust_tuple_struct_type_p (type
))
1597 if (outer_type
!= NULL
)
1598 error(_("Variant %s::%s is not a tuple variant"),
1599 TYPE_NAME (outer_type
),
1600 rust_last_path_segment (TYPE_NAME (type
)));
1602 error(_("Attempting to access anonymous field %d "
1603 "of %s, which is not a tuple, tuple struct, or "
1604 "tuple-like variant"),
1605 field_number
, TYPE_NAME (type
));
1608 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1611 error(_("Anonymous field access is only allowed on tuples, \
1612 tuple structs, and tuple-like enum variants"));
1616 case STRUCTOP_STRUCT
:
1623 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1624 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1625 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1627 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1628 type
= value_type (lhs
);
1629 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1631 const gdb_byte
*valaddr
= value_contents (lhs
);
1632 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1634 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1637 int fieldno
= (variant_field
1638 - &TYPE_FIELD (value_type (union_value
), 0));
1639 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1640 value_type (union_value
));
1642 struct type
*outer_type
= type
;
1643 type
= value_type (lhs
);
1644 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1645 error (_("Attempting to access named field foo of tuple "
1646 "variant %s::%s, which has only anonymous fields"),
1647 TYPE_NAME (outer_type
),
1648 rust_last_path_segment (TYPE_NAME (type
)));
1652 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1655 CATCH (except
, RETURN_MASK_ERROR
)
1657 error (_("Could not find field %s of struct variant %s::%s"),
1658 field_name
, TYPE_NAME (outer_type
),
1659 rust_last_path_segment (TYPE_NAME (type
)));
1664 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1665 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1666 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1671 result
= rust_range (exp
, pos
, noside
);
1675 /* We might have &array[range], in which case we need to make a
1677 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1680 result
= rust_subscript (exp
, pos
, noside
, 1);
1685 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1692 /* operator_length implementation for Rust. */
1695 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1701 switch (exp
->elts
[pc
- 1].opcode
)
1704 /* We handle aggregate as a type and argument count. The first
1705 argument might be OP_OTHERS. After that the arguments
1706 alternate: first an OP_NAME, then an expression. */
1708 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1716 case STRUCTOP_ANONYMOUS
:
1727 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1735 /* op_name implementation for Rust. */
1738 rust_op_name (enum exp_opcode opcode
)
1743 return "OP_AGGREGATE";
1747 return op_name_standard (opcode
);
1751 /* dump_subexp_body implementation for Rust. */
1754 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1757 switch (exp
->elts
[elt
].opcode
)
1761 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1764 fprintf_filtered (stream
, "Type @");
1765 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1766 fprintf_filtered (stream
, " (");
1767 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1768 fprintf_filtered (stream
, "), length %d", length
);
1771 for (i
= 0; i
< length
; ++i
)
1772 elt
= dump_subexp (exp
, stream
, elt
);
1779 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1781 fprintf_filtered (stream
, "%s: %s",
1782 (exp
->elts
[elt
].opcode
== OP_STRING
1783 ? "string" : "name"),
1784 &exp
->elts
[elt
+ 2].string
);
1785 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1790 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1793 case STRUCTOP_ANONYMOUS
:
1797 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1799 fprintf_filtered (stream
, "Field number: %d", field_number
);
1800 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1809 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1816 /* print_subexp implementation for Rust. */
1819 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1820 enum precedence prec
)
1822 switch (exp
->elts
[*pos
].opcode
)
1826 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1829 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1830 fputs_filtered (" { ", stream
);
1833 for (i
= 0; i
< length
; ++i
)
1835 rust_print_subexp (exp
, pos
, stream
, prec
);
1836 fputs_filtered (", ", stream
);
1838 fputs_filtered (" }", stream
);
1844 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1846 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1847 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1853 fputs_filtered ("<<others>> (", stream
);
1855 rust_print_subexp (exp
, pos
, stream
, prec
);
1856 fputs_filtered (")", stream
);
1860 case STRUCTOP_ANONYMOUS
:
1862 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1865 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1866 fprintf_filtered (stream
, ".%d", tem
);
1872 fprintf_filtered (stream
, "[");
1873 rust_print_subexp (exp
, pos
, stream
, prec
);
1874 fprintf_filtered (stream
, "; ");
1875 rust_print_subexp (exp
, pos
, stream
, prec
);
1876 fprintf_filtered (stream
, "]");
1880 print_subexp_standard (exp
, pos
, stream
, prec
);
1885 /* operator_check implementation for Rust. */
1888 rust_operator_check (struct expression
*exp
, int pos
,
1889 int (*objfile_func
) (struct objfile
*objfile
,
1893 switch (exp
->elts
[pos
].opcode
)
1897 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1898 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1900 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1911 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1919 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1921 static struct block_symbol
1922 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1924 const struct block
*block
,
1925 const domain_enum domain
)
1927 struct block_symbol result
= {NULL
, NULL
};
1929 if (symbol_lookup_debug
)
1931 fprintf_unfiltered (gdb_stdlog
,
1932 "rust_lookup_symbol_non_local"
1933 " (%s, %s (scope %s), %s)\n",
1934 name
, host_address_to_string (block
),
1935 block_scope (block
), domain_name (domain
));
1938 /* Look up bare names in the block's scope. */
1939 std::string scopedname
;
1940 if (name
[cp_find_first_component (name
)] == '\0')
1942 const char *scope
= block_scope (block
);
1944 if (scope
[0] != '\0')
1946 scopedname
= std::string (scope
) + "::" + name
;
1947 name
= scopedname
.c_str ();
1955 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1956 if (result
.symbol
== NULL
)
1957 result
= lookup_global_symbol (name
, block
, domain
);
1964 /* la_sniff_from_mangled_name for Rust. */
1967 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
1969 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1970 return *demangled
!= NULL
;
1975 /* la_watch_location_expression for Rust. */
1977 static gdb::unique_xmalloc_ptr
<char>
1978 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
1980 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
1981 std::string name
= type_to_string (type
);
1982 return gdb::unique_xmalloc_ptr
<char>
1983 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
1989 static const struct exp_descriptor exp_descriptor_rust
=
1992 rust_operator_length
,
1993 rust_operator_check
,
1995 rust_dump_subexp_body
,
1996 rust_evaluate_subexp
1999 static const char *rust_extensions
[] =
2004 extern const struct language_defn rust_language_defn
=
2014 &exp_descriptor_rust
,
2017 rust_printchar
, /* Print a character constant */
2018 rust_printstr
, /* Function to print string constant */
2019 rust_emitchar
, /* Print a single char */
2020 rust_print_type
, /* Print a type using appropriate syntax */
2021 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2022 rust_val_print
, /* Print a value using appropriate syntax */
2023 c_value_print
, /* Print a top-level value */
2024 default_read_var_value
, /* la_read_var_value */
2025 NULL
, /* Language specific skip_trampoline */
2026 NULL
, /* name_of_this */
2027 false, /* la_store_sym_names_in_linkage_form_p */
2028 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2029 basic_lookup_transparent_type
,/* lookup_transparent_type */
2030 gdb_demangle
, /* Language specific symbol demangler */
2031 rust_sniff_from_mangled_name
,
2032 NULL
, /* Language specific
2033 class_name_from_physname */
2034 c_op_print_tab
, /* expression operators for printing */
2035 1, /* c-style arrays */
2036 0, /* String lower bound */
2037 default_word_break_characters
,
2038 default_collect_symbol_completion_matches
,
2039 rust_language_arch_info
,
2040 default_print_array_index
,
2041 default_pass_by_reference
,
2043 rust_watch_location_expression
,
2044 NULL
, /* la_get_symbol_name_matcher */
2045 iterate_over_symbols
,
2046 default_search_name_hash
,
2047 &default_varobj_ops
,