1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2020 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"
34 #include "typeprint.h"
40 #include "cli/cli-style.h"
42 /* See rust-lang.h. */
45 rust_last_path_segment (const char *path
)
47 const char *result
= strrchr (path
, ':');
54 /* See rust-lang.h. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
62 return std::string ();
64 return std::string (scope
, cp_find_first_component (scope
));
67 /* Return true if TYPE, which must be a struct type, represents a Rust
71 rust_enum_p (const struct type
*type
)
73 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
74 && TYPE_NFIELDS (type
) == 1
75 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type
, 0)));
78 /* Return true if TYPE, which must be an enum type, has no
82 rust_empty_enum_p (const struct type
*type
)
84 gdb_assert (rust_enum_p (type
));
85 /* In Rust the enum always fills the containing structure. */
86 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
88 return TYPE_NFIELDS (TYPE_FIELD_TYPE (type
, 0)) == 0;
91 /* Given an enum type and contents, find which variant is active. */
94 rust_enum_variant (struct type
*type
, const gdb_byte
*contents
)
96 /* In Rust the enum always fills the containing structure. */
97 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
99 struct type
*union_type
= TYPE_FIELD_TYPE (type
, 0);
101 int fieldno
= value_union_variant (union_type
, contents
);
102 return &TYPE_FIELD (union_type
, fieldno
);
105 /* See rust-lang.h. */
108 rust_tuple_type_p (struct type
*type
)
110 /* The current implementation is a bit of a hack, but there's
111 nothing else in the debuginfo to distinguish a tuple from a
113 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
114 && TYPE_NAME (type
) != NULL
115 && TYPE_NAME (type
)[0] == '(');
118 /* Return true if all non-static fields of a structlike type are in a
119 sequence like __0, __1, __2. */
122 rust_underscore_fields (struct type
*type
)
128 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
130 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
132 if (!field_is_static (&TYPE_FIELD (type
, i
)))
136 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
137 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
145 /* See rust-lang.h. */
148 rust_tuple_struct_type_p (struct type
*type
)
150 /* This is just an approximation until DWARF can represent Rust more
151 precisely. We exclude zero-length structs because they may not
152 be tuple structs, and there's no way to tell. */
153 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
);
156 /* Return true if TYPE is a slice type, otherwise false. */
159 rust_slice_type_p (struct type
*type
)
161 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
162 && TYPE_NAME (type
) != NULL
163 && (strncmp (TYPE_NAME (type
), "&[", 2) == 0
164 || strcmp (TYPE_NAME (type
), "&str") == 0));
167 /* Return true if TYPE is a range type, otherwise false. */
170 rust_range_type_p (struct type
*type
)
174 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
175 || TYPE_NFIELDS (type
) > 2
176 || TYPE_NAME (type
) == NULL
177 || strstr (TYPE_NAME (type
), "::Range") == NULL
)
180 if (TYPE_NFIELDS (type
) == 0)
184 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
186 if (TYPE_NFIELDS (type
) == 1)
190 else if (TYPE_NFIELDS (type
) == 2)
192 /* First field had to be "start". */
196 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
199 /* Return true if TYPE is an inclusive range type, otherwise false.
200 This is only valid for types which are already known to be range
204 rust_inclusive_range_type_p (struct type
*type
)
206 return (strstr (TYPE_NAME (type
), "::RangeInclusive") != NULL
207 || strstr (TYPE_NAME (type
), "::RangeToInclusive") != NULL
);
210 /* Return true if TYPE seems to be the type "u8", otherwise false. */
213 rust_u8_type_p (struct type
*type
)
215 return (TYPE_CODE (type
) == TYPE_CODE_INT
216 && TYPE_UNSIGNED (type
)
217 && TYPE_LENGTH (type
) == 1);
220 /* Return true if TYPE is a Rust character type. */
223 rust_chartype_p (struct type
*type
)
225 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
226 && TYPE_LENGTH (type
) == 4
227 && TYPE_UNSIGNED (type
));
230 /* Return true if TYPE is a string type. */
233 rust_is_string_type_p (struct type
*type
)
235 LONGEST low_bound
, high_bound
;
237 type
= check_typedef (type
);
238 return ((TYPE_CODE (type
) == TYPE_CODE_STRING
)
239 || (TYPE_CODE (type
) == TYPE_CODE_PTR
240 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
241 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
242 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
244 || (TYPE_CODE (type
) == TYPE_CODE_STRUCT
245 && !rust_enum_p (type
)
246 && rust_slice_type_p (type
)
247 && strcmp (TYPE_NAME (type
), "&str") == 0));
250 /* If VALUE represents a trait object pointer, return the underlying
251 pointer with the correct (i.e., runtime) type. Otherwise, return
254 static struct value
*
255 rust_get_trait_object_pointer (struct value
*value
)
257 struct type
*type
= check_typedef (value_type (value
));
259 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
262 /* Try to be a bit resilient if the ABI changes. */
263 int vtable_field
= 0;
264 for (int i
= 0; i
< 2; ++i
)
266 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
268 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
272 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
273 struct symbol
*symbol
= find_symbol_at_address (vtable
);
274 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
277 struct rust_vtable_symbol
*vtable_sym
278 = static_cast<struct rust_vtable_symbol
*> (symbol
);
279 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
280 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
285 /* la_emitchar implementation for Rust. */
288 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
290 if (!rust_chartype_p (type
))
291 generic_emit_char (c
, type
, stream
, quoter
,
292 target_charset (get_type_arch (type
)));
293 else if (c
== '\\' || c
== quoter
)
294 fprintf_filtered (stream
, "\\%c", c
);
296 fputs_filtered ("\\n", stream
);
298 fputs_filtered ("\\r", stream
);
300 fputs_filtered ("\\t", stream
);
302 fputs_filtered ("\\0", stream
);
303 else if (c
>= 32 && c
<= 127 && isprint (c
))
304 fputc_filtered (c
, stream
);
306 fprintf_filtered (stream
, "\\x%02x", c
);
308 fprintf_filtered (stream
, "\\u{%06x}", c
);
311 /* la_printchar implementation for Rust. */
314 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
316 fputs_filtered ("'", stream
);
317 LA_EMIT_CHAR (c
, type
, stream
, '\'');
318 fputs_filtered ("'", stream
);
321 /* la_printstr implementation for Rust. */
324 rust_printstr (struct ui_file
*stream
, struct type
*type
,
325 const gdb_byte
*string
, unsigned int length
,
326 const char *user_encoding
, int force_ellipses
,
327 const struct value_print_options
*options
)
329 /* Rust always uses UTF-8, but let the caller override this if need
331 const char *encoding
= user_encoding
;
332 if (user_encoding
== NULL
|| !*user_encoding
)
334 /* In Rust strings, characters are "u8". */
335 if (rust_u8_type_p (type
))
339 /* This is probably some C string, so let's let C deal with
341 c_printstr (stream
, type
, string
, length
, user_encoding
,
342 force_ellipses
, options
);
347 /* This is not ideal as it doesn't use our character printer. */
348 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
354 /* Helper function to print a string slice. */
357 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
358 const struct value_print_options
*options
)
360 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
362 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
364 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
365 value_as_address (base
), value_as_long (len
), stream
,
369 /* rust_val_print helper for structs and untagged unions. */
372 val_print_struct (struct type
*type
, int embedded_offset
,
373 CORE_ADDR address
, struct ui_file
*stream
,
374 int recurse
, struct value
*val
,
375 const struct value_print_options
*options
)
380 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
382 /* If what we are printing here is actually a string within a
383 structure then VAL will be the original parent value, while TYPE
384 will be the type of the structure representing the string we want
386 However, RUST_VAL_PRINT_STR looks up the fields of the string
387 inside VAL, assuming that VAL is the string.
388 So, recreate VAL as a value representing just the string. */
389 val
= value_at_lazy (type
, value_address (val
) + embedded_offset
);
390 rust_val_print_str (stream
, val
, options
);
394 bool is_tuple
= rust_tuple_type_p (type
);
395 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
396 struct value_print_options opts
;
400 if (TYPE_NAME (type
) != NULL
)
401 fprintf_filtered (stream
, "%s", TYPE_NAME (type
));
403 if (TYPE_NFIELDS (type
) == 0)
406 if (TYPE_NAME (type
) != NULL
)
407 fputs_filtered (" ", stream
);
410 if (is_tuple
|| is_tuple_struct
)
411 fputs_filtered ("(", stream
);
413 fputs_filtered ("{", stream
);
419 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
421 if (field_is_static (&TYPE_FIELD (type
, i
)))
425 fputs_filtered (",", stream
);
427 if (options
->prettyformat
)
429 fputs_filtered ("\n", stream
);
430 print_spaces_filtered (2 + 2 * recurse
, stream
);
432 else if (!first_field
)
433 fputs_filtered (" ", stream
);
437 if (!is_tuple
&& !is_tuple_struct
)
439 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
440 fputs_filtered (": ", stream
);
443 val_print (TYPE_FIELD_TYPE (type
, i
),
444 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
446 stream
, recurse
+ 1, val
, &opts
,
450 if (options
->prettyformat
)
452 fputs_filtered ("\n", stream
);
453 print_spaces_filtered (2 * recurse
, stream
);
456 if (is_tuple
|| is_tuple_struct
)
457 fputs_filtered (")", stream
);
459 fputs_filtered ("}", stream
);
462 /* rust_val_print helper for discriminated unions (Rust enums). */
465 rust_print_enum (struct type
*type
, int embedded_offset
,
466 CORE_ADDR address
, struct ui_file
*stream
,
467 int recurse
, struct value
*val
,
468 const struct value_print_options
*options
)
470 struct value_print_options opts
= *options
;
474 if (rust_empty_enum_p (type
))
476 /* Print the enum type name here to be more clear. */
477 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
479 metadata_style
.style ().ptr (), nullptr);
483 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
484 struct field
*variant_field
= rust_enum_variant (type
,
485 valaddr
+ embedded_offset
);
486 embedded_offset
+= FIELD_BITPOS (*variant_field
) / 8;
487 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
489 int nfields
= TYPE_NFIELDS (variant_type
);
491 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
493 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
496 /* In case of a nullary variant like 'None', just output
501 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
503 fprintf_filtered (stream
, "(");
506 /* struct variant. */
507 fprintf_filtered (stream
, "{");
510 bool first_field
= true;
511 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
514 fputs_filtered (", ", stream
);
518 fprintf_filtered (stream
, "%s: ",
519 TYPE_FIELD_NAME (variant_type
, j
));
521 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
523 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
525 stream
, recurse
+ 1, val
, &opts
,
530 fputs_filtered (")", stream
);
532 fputs_filtered ("}", stream
);
535 static const struct generic_val_print_decorations rust_decorations
=
537 /* Complex isn't used in Rust, but we provide C-ish values just in
549 /* la_val_print implementation for Rust. */
552 rust_val_print (struct type
*type
, int embedded_offset
,
553 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
555 const struct value_print_options
*options
)
557 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
559 type
= check_typedef (type
);
560 switch (TYPE_CODE (type
))
564 LONGEST low_bound
, high_bound
;
566 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
567 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
568 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
570 /* We have a pointer to a byte string, so just print
572 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
574 struct gdbarch
*arch
= get_type_arch (type
);
575 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
577 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
578 if (options
->addressprint
)
580 fputs_filtered (paddress (arch
, addr
), stream
);
581 fputs_filtered (" ", stream
);
584 fputs_filtered ("b", stream
);
585 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
586 high_bound
- low_bound
+ 1, stream
,
593 case TYPE_CODE_METHODPTR
:
594 case TYPE_CODE_MEMBERPTR
:
595 c_val_print (type
, embedded_offset
, address
, stream
,
596 recurse
, val
, options
);
600 /* Recognize the unit type. */
601 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
602 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
604 fputs_filtered ("()", stream
);
609 case TYPE_CODE_STRING
:
611 struct gdbarch
*arch
= get_type_arch (type
);
612 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
613 LONGEST low_bound
, high_bound
;
615 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
616 error (_("Could not determine the array bounds"));
618 /* If we see a plain TYPE_CODE_STRING, then we're printing a
619 byte string, hence the choice of "ASCII" as the
621 fputs_filtered ("b", stream
);
622 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
623 valaddr
+ embedded_offset
* unit_size
,
624 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
628 case TYPE_CODE_ARRAY
:
630 LONGEST low_bound
, high_bound
;
632 if (get_array_bounds (type
, &low_bound
, &high_bound
)
633 && high_bound
- low_bound
+ 1 == 0)
634 fputs_filtered ("[]", stream
);
640 case TYPE_CODE_UNION
:
641 /* Untagged unions are printed as if they are structs. Since
642 the field bit positions overlap in the debuginfo, the code
643 for printing a union is same as that for a struct, the only
644 difference is that the input type will have overlapping
646 val_print_struct (type
, embedded_offset
, address
, stream
,
647 recurse
, val
, options
);
650 case TYPE_CODE_STRUCT
:
651 if (rust_enum_p (type
))
652 rust_print_enum (type
, embedded_offset
, address
, stream
,
653 recurse
, val
, options
);
655 val_print_struct (type
, embedded_offset
, address
, stream
,
656 recurse
, val
, options
);
661 /* Nothing special yet. */
662 generic_val_print (type
, embedded_offset
, address
, stream
,
663 recurse
, val
, options
, &rust_decorations
);
670 rust_internal_print_type (struct type
*type
, const char *varstring
,
671 struct ui_file
*stream
, int show
, int level
,
672 const struct type_print_options
*flags
,
673 bool for_rust_enum
, print_offset_data
*podata
);
675 /* Print a struct or union typedef. */
677 rust_print_struct_def (struct type
*type
, const char *varstring
,
678 struct ui_file
*stream
, int show
, int level
,
679 const struct type_print_options
*flags
,
680 bool for_rust_enum
, print_offset_data
*podata
)
682 /* Print a tuple type simply. */
683 if (rust_tuple_type_p (type
))
685 fputs_filtered (TYPE_NAME (type
), stream
);
689 /* If we see a base class, delegate to C. */
690 if (TYPE_N_BASECLASSES (type
) > 0)
691 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
693 if (flags
->print_offsets
)
695 /* Temporarily bump the level so that the output lines up
700 /* Compute properties of TYPE here because, in the enum case, the
701 rest of the code ends up looking only at the variant part. */
702 const char *tagname
= TYPE_NAME (type
);
703 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
704 bool is_tuple
= rust_tuple_type_p (type
);
705 bool is_enum
= rust_enum_p (type
);
707 int enum_discriminant_index
= -1;
711 /* Already printing an outer enum, so nothing to print here. */
715 /* This code path is also used by unions and enums. */
718 fputs_filtered ("enum ", stream
);
720 if (rust_empty_enum_p (type
))
724 fputs_filtered (tagname
, stream
);
725 fputs_filtered (" ", stream
);
727 fputs_filtered ("{}", stream
);
731 type
= TYPE_FIELD_TYPE (type
, 0);
733 struct dynamic_prop
*discriminant_prop
734 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
735 struct discriminant_info
*info
736 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
737 enum_discriminant_index
= info
->discriminant_index
;
739 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
740 fputs_filtered ("struct ", stream
);
742 fputs_filtered ("union ", stream
);
745 fputs_filtered (tagname
, stream
);
748 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
750 if (for_rust_enum
&& !flags
->print_offsets
)
751 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
753 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
755 /* When printing offsets, we rearrange the fields into storage
756 order. This lets us show holes more clearly. We work using
757 field indices here because it simplifies calls to
758 print_offset_data::update below. */
759 std::vector
<int> fields
;
760 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
762 if (field_is_static (&TYPE_FIELD (type
, i
)))
764 if (is_enum
&& i
== enum_discriminant_index
)
766 fields
.push_back (i
);
768 if (flags
->print_offsets
)
769 std::sort (fields
.begin (), fields
.end (),
772 return (TYPE_FIELD_BITPOS (type
, a
)
773 < TYPE_FIELD_BITPOS (type
, b
));
780 gdb_assert (!field_is_static (&TYPE_FIELD (type
, i
)));
781 gdb_assert (! (is_enum
&& i
== enum_discriminant_index
));
783 if (flags
->print_offsets
)
784 podata
->update (type
, i
, stream
);
786 /* We'd like to print "pub" here as needed, but rustc
787 doesn't emit the debuginfo, and our types don't have
788 cplus_struct_type attached. */
790 /* For a tuple struct we print the type but nothing
792 if (!for_rust_enum
|| flags
->print_offsets
)
793 print_spaces_filtered (level
+ 2, stream
);
795 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
796 else if (!is_tuple_struct
)
797 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
799 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
800 stream
, (is_enum
? show
: show
- 1),
801 level
+ 2, flags
, is_enum
, podata
);
802 if (!for_rust_enum
|| flags
->print_offsets
)
803 fputs_filtered (",\n", stream
);
804 /* Note that this check of "I" is ok because we only sorted the
805 fields by offset when print_offsets was set, so we won't take
806 this branch in that case. */
807 else if (i
+ 1 < TYPE_NFIELDS (type
))
808 fputs_filtered (", ", stream
);
811 if (flags
->print_offsets
)
813 /* Undo the temporary level increase we did above. */
815 podata
->finish (type
, level
, stream
);
816 print_spaces_filtered (print_offset_data::indentation
, stream
);
818 print_spaces_filtered (2, stream
);
820 if (!for_rust_enum
|| flags
->print_offsets
)
821 print_spaces_filtered (level
, stream
);
822 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
825 /* la_print_typedef implementation for Rust. */
828 rust_print_typedef (struct type
*type
,
829 struct symbol
*new_symbol
,
830 struct ui_file
*stream
)
832 type
= check_typedef (type
);
833 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
834 type_print (type
, "", stream
, 0);
835 fprintf_filtered (stream
, ";");
838 /* la_print_type implementation for Rust. */
841 rust_internal_print_type (struct type
*type
, const char *varstring
,
842 struct ui_file
*stream
, int show
, int level
,
843 const struct type_print_options
*flags
,
844 bool for_rust_enum
, print_offset_data
*podata
)
848 && TYPE_NAME (type
) != NULL
)
850 /* Rust calls the unit type "void" in its debuginfo,
851 but we don't want to print it as that. */
852 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
853 fputs_filtered ("()", stream
);
855 fputs_filtered (TYPE_NAME (type
), stream
);
859 type
= check_typedef (type
);
860 switch (TYPE_CODE (type
))
863 /* If we have an enum, we've already printed the type's
864 unqualified name, and there is nothing else to print
867 fputs_filtered ("()", stream
);
871 /* Delegate varargs to the C printer. */
872 if (TYPE_VARARGS (type
))
875 fputs_filtered ("fn ", stream
);
876 if (varstring
!= NULL
)
877 fputs_filtered (varstring
, stream
);
878 fputs_filtered ("(", stream
);
879 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
883 fputs_filtered (", ", stream
);
884 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
885 -1, 0, flags
, false, podata
);
887 fputs_filtered (")", stream
);
888 /* If it returns unit, we can omit the return type. */
889 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
891 fputs_filtered (" -> ", stream
);
892 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
893 -1, 0, flags
, false, podata
);
897 case TYPE_CODE_ARRAY
:
899 LONGEST low_bound
, high_bound
;
901 fputs_filtered ("[", stream
);
902 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
903 stream
, show
- 1, level
, flags
, false,
906 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
907 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
908 fprintf_filtered (stream
, "; variable length");
909 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
910 fprintf_filtered (stream
, "; %s",
911 plongest (high_bound
- low_bound
+ 1));
912 fputs_filtered ("]", stream
);
916 case TYPE_CODE_UNION
:
917 case TYPE_CODE_STRUCT
:
918 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
919 for_rust_enum
, podata
);
926 fputs_filtered ("enum ", stream
);
927 if (TYPE_NAME (type
) != NULL
)
929 fputs_filtered (TYPE_NAME (type
), stream
);
930 fputs_filtered (" ", stream
);
931 len
= strlen (TYPE_NAME (type
));
933 fputs_filtered ("{\n", stream
);
935 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
937 const char *name
= TYPE_FIELD_NAME (type
, i
);
942 && strncmp (name
, TYPE_NAME (type
), len
) == 0
944 && name
[len
+ 1] == ':')
946 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
949 fputs_filtered ("}", stream
);
955 if (TYPE_NAME (type
) != nullptr)
956 fputs_filtered (TYPE_NAME (type
), stream
);
959 /* We currently can't distinguish between pointers and
961 fputs_filtered ("*mut ", stream
);
962 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
969 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
974 rust_print_type (struct type
*type
, const char *varstring
,
975 struct ui_file
*stream
, int show
, int level
,
976 const struct type_print_options
*flags
)
978 print_offset_data podata
;
979 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
980 flags
, false, &podata
);
985 /* Like arch_composite_type, but uses TYPE to decide how to allocate
986 -- either on an obstack or on a gdbarch. */
989 rust_composite_type (struct type
*original
,
991 const char *field1
, struct type
*type1
,
992 const char *field2
, struct type
*type2
)
994 struct type
*result
= alloc_type_copy (original
);
995 int i
, nfields
, bitpos
;
1003 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1004 TYPE_NAME (result
) = name
;
1006 TYPE_NFIELDS (result
) = nfields
;
1007 TYPE_FIELDS (result
)
1008 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1014 struct field
*field
= &TYPE_FIELD (result
, i
);
1016 SET_FIELD_BITPOS (*field
, bitpos
);
1017 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1019 FIELD_NAME (*field
) = field1
;
1020 FIELD_TYPE (*field
) = type1
;
1025 struct field
*field
= &TYPE_FIELD (result
, i
);
1026 unsigned align
= type_align (type2
);
1032 align
*= TARGET_CHAR_BIT
;
1033 delta
= bitpos
% align
;
1035 bitpos
+= align
- delta
;
1037 SET_FIELD_BITPOS (*field
, bitpos
);
1039 FIELD_NAME (*field
) = field2
;
1040 FIELD_TYPE (*field
) = type2
;
1045 TYPE_LENGTH (result
)
1046 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1047 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1051 /* See rust-lang.h. */
1054 rust_slice_type (const char *name
, struct type
*elt_type
,
1055 struct type
*usize_type
)
1059 elt_type
= lookup_pointer_type (elt_type
);
1060 type
= rust_composite_type (elt_type
, name
,
1061 "data_ptr", elt_type
,
1062 "length", usize_type
);
1067 enum rust_primitive_types
1069 rust_primitive_bool
,
1070 rust_primitive_char
,
1079 rust_primitive_isize
,
1080 rust_primitive_usize
,
1083 rust_primitive_unit
,
1085 nr_rust_primitive_types
1088 /* la_language_arch_info implementation for Rust. */
1091 rust_language_arch_info (struct gdbarch
*gdbarch
,
1092 struct language_arch_info
*lai
)
1094 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1096 struct type
**types
;
1097 unsigned int length
;
1099 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1102 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1103 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1104 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1105 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1106 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1107 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1108 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1109 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1110 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1111 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1113 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1114 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1115 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1117 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1118 floatformats_ieee_single
);
1119 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1120 floatformats_ieee_double
);
1122 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1124 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1125 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1126 types
[rust_primitive_usize
]);
1128 lai
->primitive_type_vector
= types
;
1129 lai
->bool_type_default
= types
[rust_primitive_bool
];
1130 lai
->string_char_type
= types
[rust_primitive_u8
];
1135 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1137 static struct value
*
1138 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1141 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1143 struct value
*function
, *result
, *arg0
;
1144 struct type
*type
, *fn_type
;
1145 const struct block
*block
;
1146 struct block_symbol sym
;
1148 /* For an ordinary function call we can simply defer to the
1149 generic implementation. */
1150 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1151 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1153 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1155 method
= &exp
->elts
[*pos
+ 1].string
;
1156 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1158 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1159 type in order to look up the method. */
1160 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1162 if (noside
== EVAL_SKIP
)
1164 for (i
= 0; i
< num_args
; ++i
)
1165 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1169 std::vector
<struct value
*> args (num_args
+ 1);
1172 /* We don't yet implement real Deref semantics. */
1173 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1174 args
[0] = value_ind (args
[0]);
1176 type
= value_type (args
[0]);
1177 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1178 && TYPE_CODE (type
) != TYPE_CODE_UNION
1179 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1180 || rust_tuple_type_p (type
))
1181 error (_("Method calls only supported on struct or enum types"));
1182 if (TYPE_NAME (type
) == NULL
)
1183 error (_("Method call on nameless type"));
1185 std::string name
= std::string (TYPE_NAME (type
)) + "::" + method
;
1187 block
= get_selected_block (0);
1188 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1189 if (sym
.symbol
== NULL
)
1190 error (_("Could not find function named '%s'"), name
.c_str ());
1192 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1193 if (TYPE_NFIELDS (fn_type
) == 0)
1194 error (_("Function '%s' takes no arguments"), name
.c_str ());
1196 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1197 args
[0] = value_addr (args
[0]);
1199 function
= address_of_variable (sym
.symbol
, block
);
1201 for (i
= 0; i
< num_args
; ++i
)
1202 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1204 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1205 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1207 result
= call_function_by_hand (function
, NULL
, args
);
1211 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1213 static struct value
*
1214 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1216 enum range_type kind
;
1217 struct value
*low
= NULL
, *high
= NULL
;
1218 struct value
*addrval
, *result
;
1220 struct type
*range_type
;
1221 struct type
*index_type
;
1222 struct type
*temp_type
;
1225 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1228 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1229 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1230 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1231 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1232 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1233 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1234 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1236 if (noside
== EVAL_SKIP
)
1237 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1244 name
= "std::ops::RangeFull";
1248 index_type
= value_type (high
);
1250 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1257 index_type
= value_type (low
);
1258 name
= "std::ops::RangeFrom";
1262 if (!types_equal (value_type (low
), value_type (high
)))
1263 error (_("Range expression with different types"));
1264 index_type
= value_type (low
);
1265 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1269 /* If we don't have an index type, just allocate this on the
1270 arch. Here any type will do. */
1271 temp_type
= (index_type
== NULL
1272 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1274 /* It would be nicer to cache the range type. */
1275 range_type
= rust_composite_type (temp_type
, name
,
1276 low
== NULL
? NULL
: "start", index_type
,
1277 high
== NULL
? NULL
: "end", index_type
);
1279 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1280 return value_zero (range_type
, lval_memory
);
1282 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1283 addr
= value_as_long (addrval
);
1284 result
= value_at_lazy (range_type
, addr
);
1288 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1291 value_assign (start
, low
);
1296 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1299 value_assign (end
, high
);
1302 result
= value_at_lazy (range_type
, addr
);
1306 /* A helper function to compute the range and kind given a range
1307 value. TYPE is the type of the range value. RANGE is the range
1308 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1309 parameters might be filled in, or might not be, depending on the
1310 kind of range this is. KIND will always be set to the appropriate
1311 value describing the kind of range, and this can be used to
1312 determine whether LOW or HIGH are valid. */
1315 rust_compute_range (struct type
*type
, struct value
*range
,
1316 LONGEST
*low
, LONGEST
*high
,
1317 enum range_type
*kind
)
1323 *kind
= BOTH_BOUND_DEFAULT
;
1325 if (TYPE_NFIELDS (type
) == 0)
1329 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1331 *kind
= HIGH_BOUND_DEFAULT
;
1332 *low
= value_as_long (value_field (range
, 0));
1335 if (TYPE_NFIELDS (type
) > i
1336 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1338 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1339 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1340 *high
= value_as_long (value_field (range
, i
));
1342 if (rust_inclusive_range_type_p (type
))
1347 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1349 static struct value
*
1350 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1353 struct value
*lhs
, *rhs
, *result
;
1354 struct type
*rhstype
;
1355 LONGEST low
, high_bound
;
1356 /* Initialized to appease the compiler. */
1357 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1362 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1363 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1365 if (noside
== EVAL_SKIP
)
1368 rhstype
= check_typedef (value_type (rhs
));
1369 if (rust_range_type_p (rhstype
))
1372 error (_("Can't take slice of array without '&'"));
1373 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1377 low
= value_as_long (rhs
);
1379 struct type
*type
= check_typedef (value_type (lhs
));
1380 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1382 struct type
*base_type
= nullptr;
1383 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1384 base_type
= TYPE_TARGET_TYPE (type
);
1385 else if (rust_slice_type_p (type
))
1387 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1389 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1391 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1395 if (base_type
== nullptr)
1396 error (_("Could not find 'data_ptr' in slice type"));
1398 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1399 base_type
= TYPE_TARGET_TYPE (type
);
1401 error (_("Cannot subscript non-array type"));
1403 struct type
*new_type
;
1406 if (rust_slice_type_p (type
))
1411 = language_lookup_primitive_type (exp
->language_defn
,
1414 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1418 new_type
= base_type
;
1420 return value_zero (new_type
, VALUE_LVAL (lhs
));
1427 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1430 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1431 error (_("Can't compute array bounds"));
1433 error (_("Found array with non-zero lower bound"));
1436 else if (rust_slice_type_p (type
))
1440 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1441 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1443 high_bound
= value_as_long (len
);
1445 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1449 high_bound
= LONGEST_MAX
;
1452 error (_("Cannot subscript non-array type"));
1455 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1458 error (_("Index less than zero"));
1459 if (low
> high_bound
)
1460 error (_("Index greater than length"));
1462 result
= value_subscript (base
, low
);
1469 struct type
*usize
, *slice
;
1471 struct value
*addrval
, *tem
;
1473 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1476 error (_("High index less than zero"));
1478 error (_("Low index greater than high index"));
1479 if (high
> high_bound
)
1480 error (_("High index greater than length"));
1482 usize
= language_lookup_primitive_type (exp
->language_defn
,
1485 const char *new_name
= ((type
!= nullptr
1486 && rust_slice_type_p (type
))
1487 ? TYPE_NAME (type
) : "&[*gdb*]");
1489 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1491 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1492 addr
= value_as_long (addrval
);
1493 tem
= value_at_lazy (slice
, addr
);
1495 value_assign (value_field (tem
, 0), value_addr (result
));
1496 value_assign (value_field (tem
, 1),
1497 value_from_longest (usize
, high
- low
));
1499 result
= value_at_lazy (slice
, addr
);
1502 result
= value_addr (result
);
1508 /* evaluate_exp implementation for Rust. */
1510 static struct value
*
1511 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1512 int *pos
, enum noside noside
)
1514 struct value
*result
;
1516 switch (exp
->elts
[*pos
].opcode
)
1520 if (noside
!= EVAL_NORMAL
)
1521 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1525 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1528 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1529 if (trait_ptr
!= NULL
)
1532 result
= value_ind (value
);
1537 case UNOP_COMPLEMENT
:
1539 struct value
*value
;
1542 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1543 if (noside
== EVAL_SKIP
)
1545 /* Preserving the type is enough. */
1548 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1549 result
= value_from_longest (value_type (value
),
1550 value_logical_not (value
));
1552 result
= value_complement (value
);
1556 case BINOP_SUBSCRIPT
:
1557 result
= rust_subscript (exp
, pos
, noside
, 0);
1561 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1567 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1568 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1571 struct value
*addrval
= NULL
;
1575 if (noside
== EVAL_NORMAL
)
1577 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1578 addr
= value_as_long (addrval
);
1579 result
= value_at_lazy (type
, addr
);
1582 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1587 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1588 if (noside
== EVAL_NORMAL
)
1590 /* This isn't quite right but will do for the time
1591 being, seeing that we can't implement the Copy
1593 value_assign (result
, init
);
1599 gdb_assert (arglen
% 2 == 0);
1600 for (i
= 0; i
< arglen
; i
+= 2)
1603 const char *fieldname
;
1604 struct value
*value
, *field
;
1606 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1608 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1610 fieldname
= &exp
->elts
[*pos
].string
;
1611 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1613 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1614 if (noside
== EVAL_NORMAL
)
1616 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1618 value_assign (field
, value
);
1622 if (noside
== EVAL_SKIP
)
1623 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1625 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1626 result
= allocate_value (type
);
1628 result
= value_at_lazy (type
, addr
);
1637 struct value
*ncopies
;
1639 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1640 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1641 copies
= value_as_long (ncopies
);
1643 error (_("Array with negative number of elements"));
1645 if (noside
== EVAL_NORMAL
)
1648 std::vector
<struct value
*> eltvec (copies
);
1650 for (i
= 0; i
< copies
; ++i
)
1652 result
= value_array (0, copies
- 1, eltvec
.data ());
1656 struct type
*arraytype
1657 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1658 result
= allocate_value (arraytype
);
1663 case STRUCTOP_ANONYMOUS
:
1665 /* Anonymous field access, i.e. foo.1. */
1667 int pc
, field_number
, nfields
;
1671 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1673 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1675 type
= value_type (lhs
);
1677 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1679 struct type
*outer_type
= NULL
;
1681 if (rust_enum_p (type
))
1683 if (rust_empty_enum_p (type
))
1684 error (_("Cannot access field %d of empty enum %s"),
1685 field_number
, TYPE_NAME (type
));
1687 const gdb_byte
*valaddr
= value_contents (lhs
);
1688 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1690 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1693 int fieldno
= (variant_field
1694 - &TYPE_FIELD (value_type (union_value
), 0));
1695 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1696 value_type (union_value
));
1698 type
= value_type (lhs
);
1701 /* Tuples and tuple structs */
1702 nfields
= TYPE_NFIELDS (type
);
1704 if (field_number
>= nfields
|| field_number
< 0)
1706 if (outer_type
!= NULL
)
1707 error(_("Cannot access field %d of variant %s::%s, "
1708 "there are only %d fields"),
1709 field_number
, TYPE_NAME (outer_type
),
1710 rust_last_path_segment (TYPE_NAME (type
)),
1713 error(_("Cannot access field %d of %s, "
1714 "there are only %d fields"),
1715 field_number
, TYPE_NAME (type
), nfields
);
1718 /* Tuples are tuple structs too. */
1719 if (!rust_tuple_struct_type_p (type
))
1721 if (outer_type
!= NULL
)
1722 error(_("Variant %s::%s is not a tuple variant"),
1723 TYPE_NAME (outer_type
),
1724 rust_last_path_segment (TYPE_NAME (type
)));
1726 error(_("Attempting to access anonymous field %d "
1727 "of %s, which is not a tuple, tuple struct, or "
1728 "tuple-like variant"),
1729 field_number
, TYPE_NAME (type
));
1732 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1735 error(_("Anonymous field access is only allowed on tuples, \
1736 tuple structs, and tuple-like enum variants"));
1740 case STRUCTOP_STRUCT
:
1747 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1748 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1749 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1751 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1752 type
= value_type (lhs
);
1753 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1755 if (rust_empty_enum_p (type
))
1756 error (_("Cannot access field %s of empty enum %s"),
1757 field_name
, TYPE_NAME (type
));
1759 const gdb_byte
*valaddr
= value_contents (lhs
);
1760 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1762 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1765 int fieldno
= (variant_field
1766 - &TYPE_FIELD (value_type (union_value
), 0));
1767 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1768 value_type (union_value
));
1770 struct type
*outer_type
= type
;
1771 type
= value_type (lhs
);
1772 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1773 error (_("Attempting to access named field %s of tuple "
1774 "variant %s::%s, which has only anonymous fields"),
1775 field_name
, TYPE_NAME (outer_type
),
1776 rust_last_path_segment (TYPE_NAME (type
)));
1780 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1783 catch (const gdb_exception_error
&except
)
1785 error (_("Could not find field %s of struct variant %s::%s"),
1786 field_name
, TYPE_NAME (outer_type
),
1787 rust_last_path_segment (TYPE_NAME (type
)));
1791 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1792 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1793 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1798 result
= rust_range (exp
, pos
, noside
);
1802 /* We might have &array[range], in which case we need to make a
1804 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1807 result
= rust_subscript (exp
, pos
, noside
, 1);
1812 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1819 /* operator_length implementation for Rust. */
1822 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1828 switch (exp
->elts
[pc
- 1].opcode
)
1831 /* We handle aggregate as a type and argument count. The first
1832 argument might be OP_OTHERS. After that the arguments
1833 alternate: first an OP_NAME, then an expression. */
1835 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1843 case STRUCTOP_ANONYMOUS
:
1854 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1862 /* op_name implementation for Rust. */
1865 rust_op_name (enum exp_opcode opcode
)
1870 return "OP_AGGREGATE";
1874 return op_name_standard (opcode
);
1878 /* dump_subexp_body implementation for Rust. */
1881 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1884 switch (exp
->elts
[elt
].opcode
)
1888 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1891 fprintf_filtered (stream
, "Type @");
1892 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1893 fprintf_filtered (stream
, " (");
1894 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1895 fprintf_filtered (stream
, "), length %d", length
);
1898 for (i
= 0; i
< length
; ++i
)
1899 elt
= dump_subexp (exp
, stream
, elt
);
1906 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1908 fprintf_filtered (stream
, "%s: %s",
1909 (exp
->elts
[elt
].opcode
== OP_STRING
1910 ? "string" : "name"),
1911 &exp
->elts
[elt
+ 2].string
);
1912 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1917 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1920 case STRUCTOP_ANONYMOUS
:
1924 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1926 fprintf_filtered (stream
, "Field number: %d", field_number
);
1927 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1936 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1943 /* print_subexp implementation for Rust. */
1946 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1947 enum precedence prec
)
1949 switch (exp
->elts
[*pos
].opcode
)
1953 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1956 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1957 fputs_filtered (" { ", stream
);
1960 for (i
= 0; i
< length
; ++i
)
1962 rust_print_subexp (exp
, pos
, stream
, prec
);
1963 fputs_filtered (", ", stream
);
1965 fputs_filtered (" }", stream
);
1971 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1973 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1974 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1980 fputs_filtered ("<<others>> (", stream
);
1982 rust_print_subexp (exp
, pos
, stream
, prec
);
1983 fputs_filtered (")", stream
);
1987 case STRUCTOP_ANONYMOUS
:
1989 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1992 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1993 fprintf_filtered (stream
, ".%d", tem
);
1999 fprintf_filtered (stream
, "[");
2000 rust_print_subexp (exp
, pos
, stream
, prec
);
2001 fprintf_filtered (stream
, "; ");
2002 rust_print_subexp (exp
, pos
, stream
, prec
);
2003 fprintf_filtered (stream
, "]");
2007 print_subexp_standard (exp
, pos
, stream
, prec
);
2012 /* operator_check implementation for Rust. */
2015 rust_operator_check (struct expression
*exp
, int pos
,
2016 int (*objfile_func
) (struct objfile
*objfile
,
2020 switch (exp
->elts
[pos
].opcode
)
2024 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2025 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2027 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2038 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2046 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2048 static struct block_symbol
2049 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2051 const struct block
*block
,
2052 const domain_enum domain
)
2054 struct block_symbol result
= {};
2056 if (symbol_lookup_debug
)
2058 fprintf_unfiltered (gdb_stdlog
,
2059 "rust_lookup_symbol_non_local"
2060 " (%s, %s (scope %s), %s)\n",
2061 name
, host_address_to_string (block
),
2062 block_scope (block
), domain_name (domain
));
2065 /* Look up bare names in the block's scope. */
2066 std::string scopedname
;
2067 if (name
[cp_find_first_component (name
)] == '\0')
2069 const char *scope
= block_scope (block
);
2071 if (scope
[0] != '\0')
2073 scopedname
= std::string (scope
) + "::" + name
;
2074 name
= scopedname
.c_str ();
2082 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2083 if (result
.symbol
== NULL
)
2084 result
= lookup_global_symbol (name
, block
, domain
);
2091 /* la_sniff_from_mangled_name for Rust. */
2094 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2096 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2097 return *demangled
!= NULL
;
2102 /* la_watch_location_expression for Rust. */
2104 static gdb::unique_xmalloc_ptr
<char>
2105 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2107 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2108 std::string name
= type_to_string (type
);
2109 return gdb::unique_xmalloc_ptr
<char>
2110 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2116 static const struct exp_descriptor exp_descriptor_rust
=
2119 rust_operator_length
,
2120 rust_operator_check
,
2122 rust_dump_subexp_body
,
2123 rust_evaluate_subexp
2126 static const char *rust_extensions
[] =
2131 extern const struct language_defn rust_language_defn
=
2141 &exp_descriptor_rust
,
2144 rust_printchar
, /* Print a character constant */
2145 rust_printstr
, /* Function to print string constant */
2146 rust_emitchar
, /* Print a single char */
2147 rust_print_type
, /* Print a type using appropriate syntax */
2148 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2149 rust_val_print
, /* Print a value using appropriate syntax */
2150 c_value_print
, /* Print a top-level value */
2151 default_read_var_value
, /* la_read_var_value */
2152 NULL
, /* Language specific skip_trampoline */
2153 NULL
, /* name_of_this */
2154 false, /* la_store_sym_names_in_linkage_form_p */
2155 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2156 basic_lookup_transparent_type
,/* lookup_transparent_type */
2157 gdb_demangle
, /* Language specific symbol demangler */
2158 rust_sniff_from_mangled_name
,
2159 NULL
, /* Language specific
2160 class_name_from_physname */
2161 c_op_print_tab
, /* expression operators for printing */
2162 1, /* c-style arrays */
2163 0, /* String lower bound */
2164 default_word_break_characters
,
2165 default_collect_symbol_completion_matches
,
2166 rust_language_arch_info
,
2167 default_print_array_index
,
2168 default_pass_by_reference
,
2169 rust_watch_location_expression
,
2170 NULL
, /* la_get_symbol_name_matcher */
2171 iterate_over_symbols
,
2172 default_search_name_hash
,
2173 &default_varobj_ops
,
2176 rust_is_string_type_p
,
2177 "{...}" /* la_struct_too_deep_ellipsis */