1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cp-support.h"
33 #include "rust-lang.h"
39 /* See rust-lang.h. */
42 rust_last_path_segment (const char *path
)
44 const char *result
= strrchr (path
, ':');
51 /* See rust-lang.h. */
54 rust_crate_for_block (const struct block
*block
)
56 const char *scope
= block_scope (block
);
59 return std::string ();
61 return std::string (scope
, cp_find_first_component (scope
));
64 /* Return true if TYPE, which must be a struct type, represents a Rust
68 rust_enum_p (const struct type
*type
)
70 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
71 && TYPE_NFIELDS (type
) == 1
72 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type
, 0)));
75 /* Given an enum type and contents, find which variant is active. */
78 rust_enum_variant (struct type
*type
, const gdb_byte
*contents
)
80 /* In Rust the enum always fills the containing structure. */
81 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
83 struct type
*union_type
= TYPE_FIELD_TYPE (type
, 0);
85 int fieldno
= value_union_variant (union_type
, contents
);
86 return &TYPE_FIELD (union_type
, fieldno
);
89 /* See rust-lang.h. */
92 rust_tuple_type_p (struct type
*type
)
94 /* The current implementation is a bit of a hack, but there's
95 nothing else in the debuginfo to distinguish a tuple from a
97 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
98 && TYPE_TAG_NAME (type
) != NULL
99 && TYPE_TAG_NAME (type
)[0] == '(');
102 /* Return true if all non-static fields of a structlike type are in a
103 sequence like __0, __1, __2. */
106 rust_underscore_fields (struct type
*type
)
112 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
114 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
116 if (!field_is_static (&TYPE_FIELD (type
, i
)))
120 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
121 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
129 /* See rust-lang.h. */
132 rust_tuple_struct_type_p (struct type
*type
)
134 /* This is just an approximation until DWARF can represent Rust more
135 precisely. We exclude zero-length structs because they may not
136 be tuple structs, and there's no way to tell. */
137 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
);
140 /* Return true if TYPE is a slice type, otherwise false. */
143 rust_slice_type_p (struct type
*type
)
145 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
146 && TYPE_TAG_NAME (type
) != NULL
147 && (strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0
148 || strcmp (TYPE_TAG_NAME (type
), "&str") == 0));
151 /* Return true if TYPE is a range type, otherwise false. */
154 rust_range_type_p (struct type
*type
)
158 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
159 || TYPE_NFIELDS (type
) > 2
160 || TYPE_TAG_NAME (type
) == NULL
161 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
164 if (TYPE_NFIELDS (type
) == 0)
168 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
170 if (TYPE_NFIELDS (type
) == 1)
174 else if (TYPE_NFIELDS (type
) == 2)
176 /* First field had to be "start". */
180 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
183 /* Return true if TYPE seems to be the type "u8", otherwise false. */
186 rust_u8_type_p (struct type
*type
)
188 return (TYPE_CODE (type
) == TYPE_CODE_INT
189 && TYPE_UNSIGNED (type
)
190 && TYPE_LENGTH (type
) == 1);
193 /* Return true if TYPE is a Rust character type. */
196 rust_chartype_p (struct type
*type
)
198 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
199 && TYPE_LENGTH (type
) == 4
200 && TYPE_UNSIGNED (type
));
203 /* If VALUE represents a trait object pointer, return the underlying
204 pointer with the correct (i.e., runtime) type. Otherwise, return
207 static struct value
*
208 rust_get_trait_object_pointer (struct value
*value
)
210 struct type
*type
= check_typedef (value_type (value
));
212 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
215 /* Try to be a bit resilient if the ABI changes. */
216 int vtable_field
= 0;
217 for (int i
= 0; i
< 2; ++i
)
219 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
221 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
225 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
226 struct symbol
*symbol
= find_symbol_at_address (vtable
);
227 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
230 struct rust_vtable_symbol
*vtable_sym
231 = static_cast<struct rust_vtable_symbol
*> (symbol
);
232 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
233 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
238 /* la_emitchar implementation for Rust. */
241 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
243 if (!rust_chartype_p (type
))
244 generic_emit_char (c
, type
, stream
, quoter
,
245 target_charset (get_type_arch (type
)));
246 else if (c
== '\\' || c
== quoter
)
247 fprintf_filtered (stream
, "\\%c", c
);
249 fputs_filtered ("\\n", stream
);
251 fputs_filtered ("\\r", stream
);
253 fputs_filtered ("\\t", stream
);
255 fputs_filtered ("\\0", stream
);
256 else if (c
>= 32 && c
<= 127 && isprint (c
))
257 fputc_filtered (c
, stream
);
259 fprintf_filtered (stream
, "\\x%02x", c
);
261 fprintf_filtered (stream
, "\\u{%06x}", c
);
264 /* la_printchar implementation for Rust. */
267 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
269 fputs_filtered ("'", stream
);
270 LA_EMIT_CHAR (c
, type
, stream
, '\'');
271 fputs_filtered ("'", stream
);
274 /* la_printstr implementation for Rust. */
277 rust_printstr (struct ui_file
*stream
, struct type
*type
,
278 const gdb_byte
*string
, unsigned int length
,
279 const char *user_encoding
, int force_ellipses
,
280 const struct value_print_options
*options
)
282 /* Rust always uses UTF-8, but let the caller override this if need
284 const char *encoding
= user_encoding
;
285 if (user_encoding
== NULL
|| !*user_encoding
)
287 /* In Rust strings, characters are "u8". */
288 if (rust_u8_type_p (type
))
292 /* This is probably some C string, so let's let C deal with
294 c_printstr (stream
, type
, string
, length
, user_encoding
,
295 force_ellipses
, options
);
300 /* This is not ideal as it doesn't use our character printer. */
301 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
307 /* Helper function to print a string slice. */
310 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
311 const struct value_print_options
*options
)
313 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
315 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
317 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
318 value_as_address (base
), value_as_long (len
), stream
,
322 /* rust_val_print helper for structs and untagged unions. */
325 val_print_struct (struct type
*type
, int embedded_offset
,
326 CORE_ADDR address
, struct ui_file
*stream
,
327 int recurse
, struct value
*val
,
328 const struct value_print_options
*options
)
333 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
335 rust_val_print_str (stream
, val
, options
);
339 bool is_tuple
= rust_tuple_type_p (type
);
340 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
341 struct value_print_options opts
;
345 if (TYPE_TAG_NAME (type
) != NULL
)
346 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
348 if (TYPE_NFIELDS (type
) == 0)
351 if (TYPE_TAG_NAME (type
) != NULL
)
352 fputs_filtered (" ", stream
);
355 if (is_tuple
|| is_tuple_struct
)
356 fputs_filtered ("(", stream
);
358 fputs_filtered ("{", stream
);
364 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
366 if (field_is_static (&TYPE_FIELD (type
, i
)))
370 fputs_filtered (",", stream
);
372 if (options
->prettyformat
)
374 fputs_filtered ("\n", stream
);
375 print_spaces_filtered (2 + 2 * recurse
, stream
);
377 else if (!first_field
)
378 fputs_filtered (" ", stream
);
382 if (!is_tuple
&& !is_tuple_struct
)
384 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
385 fputs_filtered (": ", stream
);
388 val_print (TYPE_FIELD_TYPE (type
, i
),
389 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
391 stream
, recurse
+ 1, val
, &opts
,
395 if (options
->prettyformat
)
397 fputs_filtered ("\n", stream
);
398 print_spaces_filtered (2 * recurse
, stream
);
401 if (is_tuple
|| is_tuple_struct
)
402 fputs_filtered (")", stream
);
404 fputs_filtered ("}", stream
);
407 /* rust_val_print helper for discriminated unions (Rust enums). */
410 rust_print_enum (struct type
*type
, int embedded_offset
,
411 CORE_ADDR address
, struct ui_file
*stream
,
412 int recurse
, struct value
*val
,
413 const struct value_print_options
*options
)
415 struct value_print_options opts
= *options
;
419 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
420 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
421 embedded_offset
+= FIELD_BITPOS (*variant_field
) / 8;
422 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
424 int nfields
= TYPE_NFIELDS (variant_type
);
426 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
428 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
431 /* In case of a nullary variant like 'None', just output
436 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
438 fprintf_filtered (stream
, "(");
441 /* struct variant. */
442 fprintf_filtered (stream
, "{");
445 bool first_field
= true;
446 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
449 fputs_filtered (", ", stream
);
453 fprintf_filtered (stream
, "%s: ",
454 TYPE_FIELD_NAME (variant_type
, j
));
456 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
458 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
460 stream
, recurse
+ 1, val
, &opts
,
465 fputs_filtered (")", stream
);
467 fputs_filtered ("}", stream
);
470 static const struct generic_val_print_decorations rust_decorations
=
472 /* Complex isn't used in Rust, but we provide C-ish values just in
484 /* la_val_print implementation for Rust. */
487 rust_val_print (struct type
*type
, int embedded_offset
,
488 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
490 const struct value_print_options
*options
)
492 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
494 type
= check_typedef (type
);
495 switch (TYPE_CODE (type
))
499 LONGEST low_bound
, high_bound
;
501 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
502 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
503 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
505 /* We have a pointer to a byte string, so just print
507 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
509 struct gdbarch
*arch
= get_type_arch (type
);
510 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
512 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
513 if (options
->addressprint
)
515 fputs_filtered (paddress (arch
, addr
), stream
);
516 fputs_filtered (" ", stream
);
519 fputs_filtered ("b", stream
);
520 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
521 high_bound
- low_bound
+ 1, stream
,
528 case TYPE_CODE_METHODPTR
:
529 case TYPE_CODE_MEMBERPTR
:
530 c_val_print (type
, embedded_offset
, address
, stream
,
531 recurse
, val
, options
);
535 /* Recognize the unit type. */
536 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
537 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
539 fputs_filtered ("()", stream
);
544 case TYPE_CODE_STRING
:
546 struct gdbarch
*arch
= get_type_arch (type
);
547 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
548 LONGEST low_bound
, high_bound
;
550 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
551 error (_("Could not determine the array bounds"));
553 /* If we see a plain TYPE_CODE_STRING, then we're printing a
554 byte string, hence the choice of "ASCII" as the
556 fputs_filtered ("b", stream
);
557 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
558 valaddr
+ embedded_offset
* unit_size
,
559 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
563 case TYPE_CODE_ARRAY
:
565 LONGEST low_bound
, high_bound
;
567 if (get_array_bounds (type
, &low_bound
, &high_bound
)
568 && high_bound
- low_bound
+ 1 == 0)
569 fputs_filtered ("[]", stream
);
575 case TYPE_CODE_UNION
:
576 /* Untagged unions are printed as if they are structs. Since
577 the field bit positions overlap in the debuginfo, the code
578 for printing a union is same as that for a struct, the only
579 difference is that the input type will have overlapping
581 val_print_struct (type
, embedded_offset
, address
, stream
,
582 recurse
, val
, options
);
585 case TYPE_CODE_STRUCT
:
586 if (rust_enum_p (type
))
587 rust_print_enum (type
, embedded_offset
, address
, stream
,
588 recurse
, val
, options
);
590 val_print_struct (type
, embedded_offset
, address
, stream
,
591 recurse
, val
, options
);
596 /* Nothing special yet. */
597 generic_val_print (type
, embedded_offset
, address
, stream
,
598 recurse
, val
, options
, &rust_decorations
);
605 rust_internal_print_type (struct type
*type
, const char *varstring
,
606 struct ui_file
*stream
, int show
, int level
,
607 const struct type_print_options
*flags
,
610 /* Print a struct or union typedef. */
612 rust_print_struct_def (struct type
*type
, const char *varstring
,
613 struct ui_file
*stream
, int show
, int level
,
614 const struct type_print_options
*flags
,
617 /* Print a tuple type simply. */
618 if (rust_tuple_type_p (type
))
620 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
624 /* If we see a base class, delegate to C. */
625 if (TYPE_N_BASECLASSES (type
) > 0)
626 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
628 /* Compute properties of TYPE here because, in the enum case, the
629 rest of the code ends up looking only at the variant part. */
630 const char *tagname
= TYPE_TAG_NAME (type
);
631 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
632 bool is_tuple
= rust_tuple_type_p (type
);
633 bool is_enum
= rust_enum_p (type
);
635 int enum_discriminant_index
= -1;
639 /* Already printing an outer enum, so nothing to print here. */
643 /* This code path is also used by unions and enums. */
646 fputs_filtered ("enum ", stream
);
647 type
= TYPE_FIELD_TYPE (type
, 0);
649 struct dynamic_prop
*discriminant_prop
650 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
651 struct discriminant_info
*info
652 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
653 enum_discriminant_index
= info
->discriminant_index
;
655 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
656 fputs_filtered ("struct ", stream
);
658 fputs_filtered ("union ", stream
);
661 fputs_filtered (tagname
, stream
);
664 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
667 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
669 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
671 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
674 if (field_is_static (&TYPE_FIELD (type
, i
)))
677 /* We'd like to print "pub" here as needed, but rustc
678 doesn't emit the debuginfo, and our types don't have
679 cplus_struct_type attached. */
681 /* For a tuple struct we print the type but nothing
684 print_spaces_filtered (level
+ 2, stream
);
687 if (i
== enum_discriminant_index
)
689 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
691 else if (!is_tuple_struct
)
692 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
694 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
695 stream
, (is_enum
? show
: show
- 1),
696 level
+ 2, flags
, is_enum
);
698 fputs_filtered (",\n", stream
);
699 else if (i
+ 1 < TYPE_NFIELDS (type
))
700 fputs_filtered (", ", stream
);
704 print_spaces_filtered (level
, stream
);
705 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
708 /* la_print_typedef implementation for Rust. */
711 rust_print_typedef (struct type
*type
,
712 struct symbol
*new_symbol
,
713 struct ui_file
*stream
)
715 type
= check_typedef (type
);
716 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
717 type_print (type
, "", stream
, 0);
718 fprintf_filtered (stream
, ";\n");
721 /* la_print_type implementation for Rust. */
724 rust_internal_print_type (struct type
*type
, const char *varstring
,
725 struct ui_file
*stream
, int show
, int level
,
726 const struct type_print_options
*flags
,
733 && TYPE_NAME (type
) != NULL
)
735 /* Rust calls the unit type "void" in its debuginfo,
736 but we don't want to print it as that. */
737 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
738 fputs_filtered ("()", stream
);
740 fputs_filtered (TYPE_NAME (type
), stream
);
744 type
= check_typedef (type
);
745 switch (TYPE_CODE (type
))
748 /* If we have an enum, we've already printed the type's
749 unqualified name, and there is nothing else to print
752 fputs_filtered ("()", stream
);
756 /* Delegate varargs to the C printer. */
757 if (TYPE_VARARGS (type
))
760 fputs_filtered ("fn ", stream
);
761 if (varstring
!= NULL
)
762 fputs_filtered (varstring
, stream
);
763 fputs_filtered ("(", stream
);
764 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
768 fputs_filtered (", ", stream
);
769 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
770 -1, 0, flags
, false);
772 fputs_filtered (")", stream
);
773 /* If it returns unit, we can omit the return type. */
774 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
776 fputs_filtered (" -> ", stream
);
777 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
778 -1, 0, flags
, false);
782 case TYPE_CODE_ARRAY
:
784 LONGEST low_bound
, high_bound
;
786 fputs_filtered ("[", stream
);
787 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
788 stream
, show
- 1, level
, flags
, false);
790 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
791 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
792 fprintf_filtered (stream
, "; variable length");
793 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
794 fprintf_filtered (stream
, "; %s",
795 plongest (high_bound
- low_bound
+ 1));
796 fputs_filtered ("]", stream
);
800 case TYPE_CODE_UNION
:
801 case TYPE_CODE_STRUCT
:
802 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
810 fputs_filtered ("enum ", stream
);
811 if (TYPE_TAG_NAME (type
) != NULL
)
813 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
814 fputs_filtered (" ", stream
);
815 len
= strlen (TYPE_TAG_NAME (type
));
817 fputs_filtered ("{\n", stream
);
819 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
821 const char *name
= TYPE_FIELD_NAME (type
, i
);
826 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
828 && name
[len
+ 1] == ':')
830 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
833 fputs_filtered ("}", stream
);
839 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
844 rust_print_type (struct type
*type
, const char *varstring
,
845 struct ui_file
*stream
, int show
, int level
,
846 const struct type_print_options
*flags
)
848 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
854 /* Compute the alignment of the type T. */
857 rust_type_alignment (struct type
*t
)
859 t
= check_typedef (t
);
860 switch (TYPE_CODE (t
))
863 error (_("Could not compute alignment of type"));
872 return TYPE_LENGTH (t
);
874 case TYPE_CODE_ARRAY
:
875 case TYPE_CODE_COMPLEX
:
876 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
878 case TYPE_CODE_STRUCT
:
879 case TYPE_CODE_UNION
:
884 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
886 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
895 /* Like arch_composite_type, but uses TYPE to decide how to allocate
896 -- either on an obstack or on a gdbarch. */
899 rust_composite_type (struct type
*original
,
901 const char *field1
, struct type
*type1
,
902 const char *field2
, struct type
*type2
)
904 struct type
*result
= alloc_type_copy (original
);
905 int i
, nfields
, bitpos
;
913 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
914 TYPE_NAME (result
) = name
;
915 TYPE_TAG_NAME (result
) = name
;
917 TYPE_NFIELDS (result
) = nfields
;
919 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
925 struct field
*field
= &TYPE_FIELD (result
, i
);
927 SET_FIELD_BITPOS (*field
, bitpos
);
928 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
930 FIELD_NAME (*field
) = field1
;
931 FIELD_TYPE (*field
) = type1
;
936 struct field
*field
= &TYPE_FIELD (result
, i
);
937 int align
= rust_type_alignment (type2
);
943 align
*= TARGET_CHAR_BIT
;
944 delta
= bitpos
% align
;
946 bitpos
+= align
- delta
;
948 SET_FIELD_BITPOS (*field
, bitpos
);
950 FIELD_NAME (*field
) = field2
;
951 FIELD_TYPE (*field
) = type2
;
957 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
958 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
962 /* See rust-lang.h. */
965 rust_slice_type (const char *name
, struct type
*elt_type
,
966 struct type
*usize_type
)
970 elt_type
= lookup_pointer_type (elt_type
);
971 type
= rust_composite_type (elt_type
, name
,
972 "data_ptr", elt_type
,
973 "length", usize_type
);
978 enum rust_primitive_types
990 rust_primitive_isize
,
991 rust_primitive_usize
,
996 nr_rust_primitive_types
999 /* la_language_arch_info implementation for Rust. */
1002 rust_language_arch_info (struct gdbarch
*gdbarch
,
1003 struct language_arch_info
*lai
)
1005 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1007 struct type
**types
;
1008 unsigned int length
;
1010 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1013 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1014 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1015 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1016 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1017 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1018 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1019 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1020 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1021 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1022 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1024 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1025 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1026 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1028 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1029 floatformats_ieee_single
);
1030 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1031 floatformats_ieee_double
);
1033 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1035 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1036 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1037 types
[rust_primitive_usize
]);
1039 lai
->primitive_type_vector
= types
;
1040 lai
->bool_type_default
= types
[rust_primitive_bool
];
1041 lai
->string_char_type
= types
[rust_primitive_u8
];
1046 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1048 static struct value
*
1049 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1052 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1054 struct value
*function
, *result
, *arg0
;
1055 struct type
*type
, *fn_type
;
1056 const struct block
*block
;
1057 struct block_symbol sym
;
1059 /* For an ordinary function call we can simply defer to the
1060 generic implementation. */
1061 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1062 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1064 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1066 method
= &exp
->elts
[*pos
+ 1].string
;
1067 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1069 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1070 type in order to look up the method. */
1071 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1073 if (noside
== EVAL_SKIP
)
1075 for (i
= 0; i
< num_args
; ++i
)
1076 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1080 std::vector
<struct value
*> args (num_args
+ 1);
1083 /* We don't yet implement real Deref semantics. */
1084 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1085 args
[0] = value_ind (args
[0]);
1087 type
= value_type (args
[0]);
1088 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1089 && TYPE_CODE (type
) != TYPE_CODE_UNION
1090 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1091 || rust_tuple_type_p (type
))
1092 error (_("Method calls only supported on struct or enum types"));
1093 if (TYPE_TAG_NAME (type
) == NULL
)
1094 error (_("Method call on nameless type"));
1096 std::string name
= std::string (TYPE_TAG_NAME (type
)) + "::" + method
;
1098 block
= get_selected_block (0);
1099 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1100 if (sym
.symbol
== NULL
)
1101 error (_("Could not find function named '%s'"), name
.c_str ());
1103 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1104 if (TYPE_NFIELDS (fn_type
) == 0)
1105 error (_("Function '%s' takes no arguments"), name
.c_str ());
1107 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1108 args
[0] = value_addr (args
[0]);
1110 function
= address_of_variable (sym
.symbol
, block
);
1112 for (i
= 0; i
< num_args
; ++i
)
1113 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1115 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1116 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1118 result
= call_function_by_hand (function
, NULL
, num_args
+ 1, args
.data ());
1122 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1124 static struct value
*
1125 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1127 enum range_type kind
;
1128 struct value
*low
= NULL
, *high
= NULL
;
1129 struct value
*addrval
, *result
;
1131 struct type
*range_type
;
1132 struct type
*index_type
;
1133 struct type
*temp_type
;
1136 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1139 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1140 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1141 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1142 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1144 if (noside
== EVAL_SKIP
)
1145 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1152 name
= "std::ops::RangeFull";
1156 index_type
= value_type (high
);
1157 name
= "std::ops::RangeTo";
1164 index_type
= value_type (low
);
1165 name
= "std::ops::RangeFrom";
1169 if (!types_equal (value_type (low
), value_type (high
)))
1170 error (_("Range expression with different types"));
1171 index_type
= value_type (low
);
1172 name
= "std::ops::Range";
1176 /* If we don't have an index type, just allocate this on the
1177 arch. Here any type will do. */
1178 temp_type
= (index_type
== NULL
1179 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1181 /* It would be nicer to cache the range type. */
1182 range_type
= rust_composite_type (temp_type
, name
,
1183 low
== NULL
? NULL
: "start", index_type
,
1184 high
== NULL
? NULL
: "end", index_type
);
1186 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1187 return value_zero (range_type
, lval_memory
);
1189 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1190 addr
= value_as_long (addrval
);
1191 result
= value_at_lazy (range_type
, addr
);
1195 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1198 value_assign (start
, low
);
1203 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1206 value_assign (end
, high
);
1209 result
= value_at_lazy (range_type
, addr
);
1213 /* A helper function to compute the range and kind given a range
1214 value. TYPE is the type of the range value. RANGE is the range
1215 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1216 parameters might be filled in, or might not be, depending on the
1217 kind of range this is. KIND will always be set to the appropriate
1218 value describing the kind of range, and this can be used to
1219 determine whether LOW or HIGH are valid. */
1222 rust_compute_range (struct type
*type
, struct value
*range
,
1223 LONGEST
*low
, LONGEST
*high
,
1224 enum range_type
*kind
)
1230 *kind
= BOTH_BOUND_DEFAULT
;
1232 if (TYPE_NFIELDS (type
) == 0)
1236 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1238 *kind
= HIGH_BOUND_DEFAULT
;
1239 *low
= value_as_long (value_field (range
, 0));
1242 if (TYPE_NFIELDS (type
) > i
1243 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1245 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1246 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1247 *high
= value_as_long (value_field (range
, i
));
1251 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1253 static struct value
*
1254 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1257 struct value
*lhs
, *rhs
, *result
;
1258 struct type
*rhstype
;
1259 LONGEST low
, high_bound
;
1260 /* Initialized to appease the compiler. */
1261 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1266 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1267 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1269 if (noside
== EVAL_SKIP
)
1272 rhstype
= check_typedef (value_type (rhs
));
1273 if (rust_range_type_p (rhstype
))
1276 error (_("Can't take slice of array without '&'"));
1277 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1281 low
= value_as_long (rhs
);
1283 struct type
*type
= check_typedef (value_type (lhs
));
1284 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1286 struct type
*base_type
= nullptr;
1287 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1288 base_type
= TYPE_TARGET_TYPE (type
);
1289 else if (rust_slice_type_p (type
))
1291 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1293 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1295 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1299 if (base_type
== nullptr)
1300 error (_("Could not find 'data_ptr' in slice type"));
1302 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1303 base_type
= TYPE_TARGET_TYPE (type
);
1305 error (_("Cannot subscript non-array type"));
1307 struct type
*new_type
;
1310 if (rust_slice_type_p (type
))
1315 = language_lookup_primitive_type (exp
->language_defn
,
1318 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1322 new_type
= base_type
;
1324 return value_zero (new_type
, VALUE_LVAL (lhs
));
1331 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1334 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1335 error (_("Can't compute array bounds"));
1337 error (_("Found array with non-zero lower bound"));
1340 else if (rust_slice_type_p (type
))
1344 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1345 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1347 high_bound
= value_as_long (len
);
1349 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1353 high_bound
= LONGEST_MAX
;
1356 error (_("Cannot subscript non-array type"));
1359 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1362 error (_("Index less than zero"));
1363 if (low
> high_bound
)
1364 error (_("Index greater than length"));
1366 result
= value_subscript (base
, low
);
1373 struct type
*usize
, *slice
;
1375 struct value
*addrval
, *tem
;
1377 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1380 error (_("High index less than zero"));
1382 error (_("Low index greater than high index"));
1383 if (high
> high_bound
)
1384 error (_("High index greater than length"));
1386 usize
= language_lookup_primitive_type (exp
->language_defn
,
1389 const char *new_name
= ((type
!= nullptr
1390 && rust_slice_type_p (type
))
1391 ? TYPE_NAME (type
) : "&[*gdb*]");
1393 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1395 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1396 addr
= value_as_long (addrval
);
1397 tem
= value_at_lazy (slice
, addr
);
1399 value_assign (value_field (tem
, 0), value_addr (result
));
1400 value_assign (value_field (tem
, 1),
1401 value_from_longest (usize
, high
- low
));
1403 result
= value_at_lazy (slice
, addr
);
1406 result
= value_addr (result
);
1412 /* evaluate_exp implementation for Rust. */
1414 static struct value
*
1415 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1416 int *pos
, enum noside noside
)
1418 struct value
*result
;
1420 switch (exp
->elts
[*pos
].opcode
)
1424 if (noside
!= EVAL_NORMAL
)
1425 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1429 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1432 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1433 if (trait_ptr
!= NULL
)
1436 result
= value_ind (value
);
1441 case UNOP_COMPLEMENT
:
1443 struct value
*value
;
1446 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1447 if (noside
== EVAL_SKIP
)
1449 /* Preserving the type is enough. */
1452 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1453 result
= value_from_longest (value_type (value
),
1454 value_logical_not (value
));
1456 result
= value_complement (value
);
1460 case BINOP_SUBSCRIPT
:
1461 result
= rust_subscript (exp
, pos
, noside
, 0);
1465 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1471 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1472 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1475 struct value
*addrval
= NULL
;
1479 if (noside
== EVAL_NORMAL
)
1481 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1482 addr
= value_as_long (addrval
);
1483 result
= value_at_lazy (type
, addr
);
1486 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1491 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1492 if (noside
== EVAL_NORMAL
)
1494 /* This isn't quite right but will do for the time
1495 being, seeing that we can't implement the Copy
1497 value_assign (result
, init
);
1503 gdb_assert (arglen
% 2 == 0);
1504 for (i
= 0; i
< arglen
; i
+= 2)
1507 const char *fieldname
;
1508 struct value
*value
, *field
;
1510 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1512 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1514 fieldname
= &exp
->elts
[*pos
].string
;
1515 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1517 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1518 if (noside
== EVAL_NORMAL
)
1520 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1522 value_assign (field
, value
);
1526 if (noside
== EVAL_SKIP
)
1527 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1529 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1530 result
= allocate_value (type
);
1532 result
= value_at_lazy (type
, addr
);
1541 struct value
*ncopies
;
1543 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1544 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1545 copies
= value_as_long (ncopies
);
1547 error (_("Array with negative number of elements"));
1549 if (noside
== EVAL_NORMAL
)
1552 std::vector
<struct value
*> eltvec (copies
);
1554 for (i
= 0; i
< copies
; ++i
)
1556 result
= value_array (0, copies
- 1, eltvec
.data ());
1560 struct type
*arraytype
1561 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1562 result
= allocate_value (arraytype
);
1567 case STRUCTOP_ANONYMOUS
:
1569 /* Anonymous field access, i.e. foo.1. */
1571 int pc
, field_number
, nfields
;
1572 struct type
*type
, *variant_type
;
1575 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1577 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1579 type
= value_type (lhs
);
1581 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1583 struct type
*outer_type
= NULL
;
1585 if (rust_enum_p (type
))
1587 const gdb_byte
*valaddr
= value_contents (lhs
);
1588 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1590 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1593 int fieldno
= (variant_field
1594 - &TYPE_FIELD (value_type (union_value
), 0));
1595 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1596 value_type (union_value
));
1598 type
= value_type (lhs
);
1601 /* Tuples and tuple structs */
1602 nfields
= TYPE_NFIELDS (type
);
1604 if (field_number
>= nfields
|| field_number
< 0)
1606 if (outer_type
!= NULL
)
1607 error(_("Cannot access field %d of variant %s::%s, "
1608 "there are only %d fields"),
1609 field_number
, TYPE_TAG_NAME (outer_type
),
1610 rust_last_path_segment (TYPE_TAG_NAME (type
)),
1613 error(_("Cannot access field %d of %s, "
1614 "there are only %d fields"),
1615 field_number
, TYPE_TAG_NAME (type
), nfields
);
1618 /* Tuples are tuple structs too. */
1619 if (!rust_tuple_struct_type_p (type
))
1621 if (outer_type
!= NULL
)
1622 error(_("Variant %s::%s is not a tuple variant"),
1623 TYPE_TAG_NAME (outer_type
),
1624 rust_last_path_segment (TYPE_TAG_NAME (type
)));
1626 error(_("Attempting to access anonymous field %d "
1627 "of %s, which is not a tuple, tuple struct, or "
1628 "tuple-like variant"),
1629 field_number
, TYPE_TAG_NAME (type
));
1632 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1635 error(_("Anonymous field access is only allowed on tuples, \
1636 tuple structs, and tuple-like enum variants"));
1640 case STRUCTOP_STRUCT
:
1647 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1648 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1649 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1651 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1652 type
= value_type (lhs
);
1653 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1655 const gdb_byte
*valaddr
= value_contents (lhs
);
1656 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1658 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1661 int fieldno
= (variant_field
1662 - &TYPE_FIELD (value_type (union_value
), 0));
1663 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1664 value_type (union_value
));
1666 struct type
*outer_type
= type
;
1667 type
= value_type (lhs
);
1668 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1669 error (_("Attempting to access named field foo of tuple "
1670 "variant %s::%s, which has only anonymous fields"),
1671 TYPE_TAG_NAME (outer_type
),
1672 rust_last_path_segment (TYPE_NAME (type
)));
1676 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1679 CATCH (except
, RETURN_MASK_ERROR
)
1681 error (_("Could not find field %s of struct variant %s::%s"),
1682 field_name
, TYPE_TAG_NAME (outer_type
),
1683 rust_last_path_segment (TYPE_NAME (type
)));
1688 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1689 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1690 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1695 result
= rust_range (exp
, pos
, noside
);
1699 /* We might have &array[range], in which case we need to make a
1701 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1704 result
= rust_subscript (exp
, pos
, noside
, 1);
1709 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1716 /* operator_length implementation for Rust. */
1719 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1725 switch (exp
->elts
[pc
- 1].opcode
)
1728 /* We handle aggregate as a type and argument count. The first
1729 argument might be OP_OTHERS. After that the arguments
1730 alternate: first an OP_NAME, then an expression. */
1732 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1740 case STRUCTOP_ANONYMOUS
:
1751 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1759 /* op_name implementation for Rust. */
1762 rust_op_name (enum exp_opcode opcode
)
1767 return "OP_AGGREGATE";
1771 return op_name_standard (opcode
);
1775 /* dump_subexp_body implementation for Rust. */
1778 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1781 switch (exp
->elts
[elt
].opcode
)
1785 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1788 fprintf_filtered (stream
, "Type @");
1789 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1790 fprintf_filtered (stream
, " (");
1791 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1792 fprintf_filtered (stream
, "), length %d", length
);
1795 for (i
= 0; i
< length
; ++i
)
1796 elt
= dump_subexp (exp
, stream
, elt
);
1803 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1805 fprintf_filtered (stream
, "%s: %s",
1806 (exp
->elts
[elt
].opcode
== OP_STRING
1807 ? "string" : "name"),
1808 &exp
->elts
[elt
+ 2].string
);
1809 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1814 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1817 case STRUCTOP_ANONYMOUS
:
1821 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1823 fprintf_filtered (stream
, "Field number: %d", field_number
);
1824 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1833 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1840 /* print_subexp implementation for Rust. */
1843 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1844 enum precedence prec
)
1846 switch (exp
->elts
[*pos
].opcode
)
1850 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1853 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1854 fputs_filtered (" { ", stream
);
1857 for (i
= 0; i
< length
; ++i
)
1859 rust_print_subexp (exp
, pos
, stream
, prec
);
1860 fputs_filtered (", ", stream
);
1862 fputs_filtered (" }", stream
);
1868 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1870 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1871 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1877 fputs_filtered ("<<others>> (", stream
);
1879 rust_print_subexp (exp
, pos
, stream
, prec
);
1880 fputs_filtered (")", stream
);
1884 case STRUCTOP_ANONYMOUS
:
1886 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1889 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1890 fprintf_filtered (stream
, ".%d", tem
);
1896 fprintf_filtered (stream
, "[");
1897 rust_print_subexp (exp
, pos
, stream
, prec
);
1898 fprintf_filtered (stream
, "; ");
1899 rust_print_subexp (exp
, pos
, stream
, prec
);
1900 fprintf_filtered (stream
, "]");
1904 print_subexp_standard (exp
, pos
, stream
, prec
);
1909 /* operator_check implementation for Rust. */
1912 rust_operator_check (struct expression
*exp
, int pos
,
1913 int (*objfile_func
) (struct objfile
*objfile
,
1917 switch (exp
->elts
[pos
].opcode
)
1921 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1922 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1924 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1935 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1943 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1945 static struct block_symbol
1946 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1948 const struct block
*block
,
1949 const domain_enum domain
)
1951 struct block_symbol result
= {NULL
, NULL
};
1953 if (symbol_lookup_debug
)
1955 fprintf_unfiltered (gdb_stdlog
,
1956 "rust_lookup_symbol_non_local"
1957 " (%s, %s (scope %s), %s)\n",
1958 name
, host_address_to_string (block
),
1959 block_scope (block
), domain_name (domain
));
1962 /* Look up bare names in the block's scope. */
1963 std::string scopedname
;
1964 if (name
[cp_find_first_component (name
)] == '\0')
1966 const char *scope
= block_scope (block
);
1968 if (scope
[0] != '\0')
1970 scopedname
= std::string (scope
) + "::" + name
;
1971 name
= scopedname
.c_str ();
1979 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1980 if (result
.symbol
== NULL
)
1981 result
= lookup_global_symbol (name
, block
, domain
);
1988 /* la_sniff_from_mangled_name for Rust. */
1991 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
1993 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1994 return *demangled
!= NULL
;
1999 /* la_watch_location_expression for Rust. */
2001 static gdb::unique_xmalloc_ptr
<char>
2002 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2004 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2005 std::string name
= type_to_string (type
);
2006 return gdb::unique_xmalloc_ptr
<char>
2007 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2013 static const struct exp_descriptor exp_descriptor_rust
=
2016 rust_operator_length
,
2017 rust_operator_check
,
2019 rust_dump_subexp_body
,
2020 rust_evaluate_subexp
2023 static const char *rust_extensions
[] =
2028 extern const struct language_defn rust_language_defn
=
2038 &exp_descriptor_rust
,
2042 rust_printchar
, /* Print a character constant */
2043 rust_printstr
, /* Function to print string constant */
2044 rust_emitchar
, /* Print a single char */
2045 rust_print_type
, /* Print a type using appropriate syntax */
2046 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2047 rust_val_print
, /* Print a value using appropriate syntax */
2048 c_value_print
, /* Print a top-level value */
2049 default_read_var_value
, /* la_read_var_value */
2050 NULL
, /* Language specific skip_trampoline */
2051 NULL
, /* name_of_this */
2052 false, /* la_store_sym_names_in_linkage_form_p */
2053 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2054 basic_lookup_transparent_type
,/* lookup_transparent_type */
2055 gdb_demangle
, /* Language specific symbol demangler */
2056 rust_sniff_from_mangled_name
,
2057 NULL
, /* Language specific
2058 class_name_from_physname */
2059 c_op_print_tab
, /* expression operators for printing */
2060 1, /* c-style arrays */
2061 0, /* String lower bound */
2062 default_word_break_characters
,
2063 default_collect_symbol_completion_matches
,
2064 rust_language_arch_info
,
2065 default_print_array_index
,
2066 default_pass_by_reference
,
2068 rust_watch_location_expression
,
2069 NULL
, /* la_get_symbol_name_matcher */
2070 iterate_over_symbols
,
2071 default_search_name_hash
,
2072 &default_varobj_ops
,