1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2021 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"
41 #include "parser-defs.h"
43 /* See rust-lang.h. */
46 rust_last_path_segment (const char *path
)
48 const char *result
= strrchr (path
, ':');
55 /* See rust-lang.h. */
58 rust_crate_for_block (const struct block
*block
)
60 const char *scope
= block_scope (block
);
63 return std::string ();
65 return std::string (scope
, cp_find_first_component (scope
));
68 /* Return true if TYPE, which must be a struct type, represents a Rust
72 rust_enum_p (struct type
*type
)
74 /* is_dynamic_type will return true if any field has a dynamic
75 attribute -- but we only want to check the top level. */
76 return TYPE_HAS_VARIANT_PARTS (type
);
79 /* Return true if TYPE, which must be an already-resolved enum type,
83 rust_empty_enum_p (const struct type
*type
)
85 return type
->num_fields () == 0;
88 /* Given an already-resolved enum type and contents, find which
92 rust_enum_variant (struct type
*type
)
94 /* The active variant is simply the first non-artificial field. */
95 for (int i
= 0; i
< type
->num_fields (); ++i
)
96 if (!TYPE_FIELD_ARTIFICIAL (type
, i
))
99 /* Perhaps we could get here by trying to print an Ada variant
100 record in Rust mode. Unlikely, but an error is safer than an
102 error (_("Could not find active enum variant"));
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_CODE_STRUCT
114 && type
->name () != NULL
115 && type
->name ()[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_CODE_STRUCT
)
130 for (i
= 0; i
< type
->num_fields (); ++i
)
132 if (!field_is_static (&type
->field (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
->num_fields () > 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_CODE_STRUCT
162 && type
->name () != NULL
163 && (strncmp (type
->name (), "&[", 2) == 0
164 || strcmp (type
->name (), "&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_CODE_STRUCT
175 || type
->num_fields () > 2
176 || type
->name () == NULL
177 || strstr (type
->name (), "::Range") == NULL
)
180 if (type
->num_fields () == 0)
184 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
186 if (type
->num_fields () == 1)
190 else if (type
->num_fields () == 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 (), "::RangeInclusive") != NULL
207 || strstr (type
->name (), "::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_CODE_INT
216 && type
->is_unsigned ()
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_CODE_CHAR
226 && TYPE_LENGTH (type
) == 4
227 && type
->is_unsigned ());
230 /* If VALUE represents a trait object pointer, return the underlying
231 pointer with the correct (i.e., runtime) type. Otherwise, return
234 static struct value
*
235 rust_get_trait_object_pointer (struct value
*value
)
237 struct type
*type
= check_typedef (value_type (value
));
239 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
242 /* Try to be a bit resilient if the ABI changes. */
243 int vtable_field
= 0;
244 for (int i
= 0; i
< 2; ++i
)
246 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
248 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
252 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
253 struct symbol
*symbol
= find_symbol_at_address (vtable
);
254 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
257 struct rust_vtable_symbol
*vtable_sym
258 = static_cast<struct rust_vtable_symbol
*> (symbol
);
259 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
260 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
265 /* See language.h. */
268 rust_language::printstr (struct ui_file
*stream
, struct type
*type
,
269 const gdb_byte
*string
, unsigned int length
,
270 const char *user_encoding
, int force_ellipses
,
271 const struct value_print_options
*options
) const
273 /* Rust always uses UTF-8, but let the caller override this if need
275 const char *encoding
= user_encoding
;
276 if (user_encoding
== NULL
|| !*user_encoding
)
278 /* In Rust strings, characters are "u8". */
279 if (rust_u8_type_p (type
))
283 /* This is probably some C string, so let's let C deal with
285 c_printstr (stream
, type
, string
, length
, user_encoding
,
286 force_ellipses
, options
);
291 /* This is not ideal as it doesn't use our character printer. */
292 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
298 /* Helper function to print a string slice. */
301 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
302 const struct value_print_options
*options
)
304 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
306 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
308 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
309 value_as_address (base
), value_as_long (len
), stream
,
313 /* See rust-lang.h. */
316 rust_language::val_print_struct
317 (struct value
*val
, struct ui_file
*stream
, int recurse
,
318 const struct value_print_options
*options
) const
322 struct type
*type
= check_typedef (value_type (val
));
324 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
326 /* If what we are printing here is actually a string within a
327 structure then VAL will be the original parent value, while TYPE
328 will be the type of the structure representing the string we want
330 However, RUST_VAL_PRINT_STR looks up the fields of the string
331 inside VAL, assuming that VAL is the string.
332 So, recreate VAL as a value representing just the string. */
333 val
= value_at_lazy (type
, value_address (val
));
334 rust_val_print_str (stream
, val
, options
);
338 bool is_tuple
= rust_tuple_type_p (type
);
339 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
340 struct value_print_options opts
;
344 if (type
->name () != NULL
)
345 fprintf_filtered (stream
, "%s", type
->name ());
347 if (type
->num_fields () == 0)
350 if (type
->name () != NULL
)
351 fputs_filtered (" ", stream
);
354 if (is_tuple
|| is_tuple_struct
)
355 fputs_filtered ("(", stream
);
357 fputs_filtered ("{", stream
);
363 for (i
= 0; i
< type
->num_fields (); ++i
)
365 if (field_is_static (&type
->field (i
)))
369 fputs_filtered (",", stream
);
371 if (options
->prettyformat
)
373 fputs_filtered ("\n", stream
);
374 print_spaces_filtered (2 + 2 * recurse
, stream
);
376 else if (!first_field
)
377 fputs_filtered (" ", stream
);
381 if (!is_tuple
&& !is_tuple_struct
)
383 fputs_styled (TYPE_FIELD_NAME (type
, i
),
384 variable_name_style
.style (), stream
);
385 fputs_filtered (": ", stream
);
388 value_print_inner (value_field (val
, i
), stream
, recurse
+ 1, &opts
);
391 if (options
->prettyformat
)
393 fputs_filtered ("\n", stream
);
394 print_spaces_filtered (2 * recurse
, stream
);
397 if (is_tuple
|| is_tuple_struct
)
398 fputs_filtered (")", stream
);
400 fputs_filtered ("}", stream
);
403 /* See rust-lang.h. */
406 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
408 const struct value_print_options
*options
) const
410 struct value_print_options opts
= *options
;
411 struct type
*type
= check_typedef (value_type (val
));
415 gdb_assert (rust_enum_p (type
));
416 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
417 TYPE_LENGTH (value_type (val
)));
418 type
= resolve_dynamic_type (type
, view
, value_address (val
));
420 if (rust_empty_enum_p (type
))
422 /* Print the enum type name here to be more clear. */
423 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
425 metadata_style
.style ().ptr (), nullptr);
429 int variant_fieldno
= rust_enum_variant (type
);
430 val
= value_field (val
, variant_fieldno
);
431 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
433 int nfields
= variant_type
->num_fields ();
435 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
437 fprintf_filtered (stream
, "%s", variant_type
->name ());
440 /* In case of a nullary variant like 'None', just output
445 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
447 fprintf_filtered (stream
, "(");
450 /* struct variant. */
451 fprintf_filtered (stream
, "{");
454 bool first_field
= true;
455 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
458 fputs_filtered (", ", stream
);
462 fprintf_filtered (stream
, "%ps: ",
463 styled_string (variable_name_style
.style (),
464 TYPE_FIELD_NAME (variant_type
, j
)));
466 value_print_inner (value_field (val
, j
), stream
, recurse
+ 1, &opts
);
470 fputs_filtered (")", stream
);
472 fputs_filtered ("}", stream
);
475 static const struct generic_val_print_decorations rust_decorations
=
477 /* Complex isn't used in Rust, but we provide C-ish values just in
489 /* See language.h. */
492 rust_language::value_print_inner
493 (struct value
*val
, struct ui_file
*stream
, int recurse
,
494 const struct value_print_options
*options
) const
496 struct value_print_options opts
= *options
;
499 if (opts
.prettyformat
== Val_prettyformat_default
)
500 opts
.prettyformat
= (opts
.prettyformat_structs
501 ? Val_prettyformat
: Val_no_prettyformat
);
503 struct type
*type
= check_typedef (value_type (val
));
504 switch (type
->code ())
508 LONGEST low_bound
, high_bound
;
510 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
511 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
512 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
515 /* We have a pointer to a byte string, so just print
517 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
518 CORE_ADDR addr
= value_as_address (val
);
519 struct gdbarch
*arch
= type
->arch ();
521 if (opts
.addressprint
)
523 fputs_filtered (paddress (arch
, addr
), stream
);
524 fputs_filtered (" ", stream
);
527 fputs_filtered ("b", stream
);
528 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
529 high_bound
- low_bound
+ 1, stream
,
537 /* Recognize the unit type. */
538 if (type
->is_unsigned () && TYPE_LENGTH (type
) == 0
539 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
541 fputs_filtered ("()", stream
);
546 case TYPE_CODE_STRING
:
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 printstr (stream
, TYPE_TARGET_TYPE (type
),
558 value_contents_for_printing (val
),
559 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
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 (val
, stream
, recurse
, &opts
);
584 case TYPE_CODE_STRUCT
:
585 if (rust_enum_p (type
))
586 print_enum (val
, stream
, recurse
, &opts
);
588 val_print_struct (val
, stream
, recurse
, &opts
);
593 /* Nothing special yet. */
594 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
601 rust_internal_print_type (struct type
*type
, const char *varstring
,
602 struct ui_file
*stream
, int show
, int level
,
603 const struct type_print_options
*flags
,
604 bool for_rust_enum
, print_offset_data
*podata
);
606 /* Print a struct or union typedef. */
608 rust_print_struct_def (struct type
*type
, const char *varstring
,
609 struct ui_file
*stream
, int show
, int level
,
610 const struct type_print_options
*flags
,
611 bool for_rust_enum
, print_offset_data
*podata
)
613 /* Print a tuple type simply. */
614 if (rust_tuple_type_p (type
))
616 fputs_filtered (type
->name (), stream
);
620 /* If we see a base class, delegate to C. */
621 if (TYPE_N_BASECLASSES (type
) > 0)
622 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
624 if (flags
->print_offsets
)
626 /* Temporarily bump the level so that the output lines up
631 /* Compute properties of TYPE here because, in the enum case, the
632 rest of the code ends up looking only at the variant part. */
633 const char *tagname
= type
->name ();
634 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
635 bool is_tuple
= rust_tuple_type_p (type
);
636 bool is_enum
= rust_enum_p (type
);
640 /* Already printing an outer enum, so nothing to print here. */
644 /* This code path is also used by unions and enums. */
647 fputs_filtered ("enum ", stream
);
648 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
649 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
650 type
= prop
->original_type ();
652 else if (type
->code () == TYPE_CODE_STRUCT
)
653 fputs_filtered ("struct ", stream
);
655 fputs_filtered ("union ", stream
);
658 fputs_filtered (tagname
, stream
);
661 if (type
->num_fields () == 0 && !is_tuple
)
663 if (for_rust_enum
&& !flags
->print_offsets
)
664 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
666 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
668 /* When printing offsets, we rearrange the fields into storage
669 order. This lets us show holes more clearly. We work using
670 field indices here because it simplifies calls to
671 print_offset_data::update below. */
672 std::vector
<int> fields
;
673 for (int i
= 0; i
< type
->num_fields (); ++i
)
675 if (field_is_static (&type
->field (i
)))
677 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
679 fields
.push_back (i
);
681 if (flags
->print_offsets
)
682 std::sort (fields
.begin (), fields
.end (),
685 return (TYPE_FIELD_BITPOS (type
, a
)
686 < TYPE_FIELD_BITPOS (type
, b
));
693 gdb_assert (!field_is_static (&type
->field (i
)));
694 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
696 if (flags
->print_offsets
)
697 podata
->update (type
, i
, stream
);
699 /* We'd like to print "pub" here as needed, but rustc
700 doesn't emit the debuginfo, and our types don't have
701 cplus_struct_type attached. */
703 /* For a tuple struct we print the type but nothing
705 if (!for_rust_enum
|| flags
->print_offsets
)
706 print_spaces_filtered (level
+ 2, stream
);
708 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
710 else if (!is_tuple_struct
)
711 fprintf_filtered (stream
, "%ps: ",
712 styled_string (variable_name_style
.style (),
713 TYPE_FIELD_NAME (type
, i
)));
715 rust_internal_print_type (type
->field (i
).type (), NULL
,
716 stream
, (is_enum
? show
: show
- 1),
717 level
+ 2, flags
, is_enum
, podata
);
718 if (!for_rust_enum
|| flags
->print_offsets
)
719 fputs_filtered (",\n", stream
);
720 /* Note that this check of "I" is ok because we only sorted the
721 fields by offset when print_offsets was set, so we won't take
722 this branch in that case. */
723 else if (i
+ 1 < type
->num_fields ())
724 fputs_filtered (", ", stream
);
727 if (flags
->print_offsets
)
729 /* Undo the temporary level increase we did above. */
731 podata
->finish (type
, level
, stream
);
732 print_spaces_filtered (print_offset_data::indentation
, stream
);
734 print_spaces_filtered (2, stream
);
736 if (!for_rust_enum
|| flags
->print_offsets
)
737 print_spaces_filtered (level
, stream
);
738 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
741 /* la_print_type implementation for Rust. */
744 rust_internal_print_type (struct type
*type
, const char *varstring
,
745 struct ui_file
*stream
, int show
, int level
,
746 const struct type_print_options
*flags
,
747 bool for_rust_enum
, print_offset_data
*podata
)
751 && type
->name () != NULL
)
753 /* Rust calls the unit type "void" in its debuginfo,
754 but we don't want to print it as that. */
755 if (type
->code () == TYPE_CODE_VOID
)
756 fputs_filtered ("()", stream
);
758 fputs_filtered (type
->name (), stream
);
762 type
= check_typedef (type
);
763 switch (type
->code ())
766 /* If we have an enum, we've already printed the type's
767 unqualified name, and there is nothing else to print
770 fputs_filtered ("()", stream
);
774 /* Delegate varargs to the C printer. */
775 if (type
->has_varargs ())
778 fputs_filtered ("fn ", stream
);
779 if (varstring
!= NULL
)
780 fputs_filtered (varstring
, stream
);
781 fputs_filtered ("(", stream
);
782 for (int i
= 0; i
< type
->num_fields (); ++i
)
786 fputs_filtered (", ", stream
);
787 rust_internal_print_type (type
->field (i
).type (), "", stream
,
788 -1, 0, flags
, false, podata
);
790 fputs_filtered (")", stream
);
791 /* If it returns unit, we can omit the return type. */
792 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
794 fputs_filtered (" -> ", stream
);
795 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
796 -1, 0, flags
, false, podata
);
800 case TYPE_CODE_ARRAY
:
802 LONGEST low_bound
, high_bound
;
804 fputs_filtered ("[", stream
);
805 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
806 stream
, show
- 1, level
, flags
, false,
809 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
810 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
811 fprintf_filtered (stream
, "; variable length");
812 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
813 fprintf_filtered (stream
, "; %s",
814 plongest (high_bound
- low_bound
+ 1));
815 fputs_filtered ("]", stream
);
819 case TYPE_CODE_UNION
:
820 case TYPE_CODE_STRUCT
:
821 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
822 for_rust_enum
, podata
);
829 fputs_filtered ("enum ", stream
);
830 if (type
->name () != NULL
)
832 fputs_filtered (type
->name (), stream
);
833 fputs_filtered (" ", stream
);
834 len
= strlen (type
->name ());
836 fputs_filtered ("{\n", stream
);
838 for (int i
= 0; i
< type
->num_fields (); ++i
)
840 const char *name
= TYPE_FIELD_NAME (type
, i
);
845 && strncmp (name
, type
->name (), len
) == 0
847 && name
[len
+ 1] == ':')
849 fprintf_filtered (stream
, "%*s%ps,\n",
851 styled_string (variable_name_style
.style (),
855 fputs_filtered ("}", stream
);
861 if (type
->name () != nullptr)
862 fputs_filtered (type
->name (), stream
);
865 /* We currently can't distinguish between pointers and
867 fputs_filtered ("*mut ", stream
);
868 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
875 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
881 /* Like arch_composite_type, but uses TYPE to decide how to allocate
882 -- either on an obstack or on a gdbarch. */
885 rust_composite_type (struct type
*original
,
887 const char *field1
, struct type
*type1
,
888 const char *field2
, struct type
*type2
)
890 struct type
*result
= alloc_type_copy (original
);
891 int i
, nfields
, bitpos
;
899 result
->set_code (TYPE_CODE_STRUCT
);
900 result
->set_name (name
);
902 result
->set_num_fields (nfields
);
904 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
910 struct field
*field
= &result
->field (i
);
912 SET_FIELD_BITPOS (*field
, bitpos
);
913 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
915 FIELD_NAME (*field
) = field1
;
916 field
->set_type (type1
);
921 struct field
*field
= &result
->field (i
);
922 unsigned align
= type_align (type2
);
928 align
*= TARGET_CHAR_BIT
;
929 delta
= bitpos
% align
;
931 bitpos
+= align
- delta
;
933 SET_FIELD_BITPOS (*field
, bitpos
);
935 FIELD_NAME (*field
) = field2
;
936 field
->set_type (type2
);
942 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
943 TYPE_LENGTH (result
->field (i
- 1).type ()));
947 /* See rust-lang.h. */
950 rust_slice_type (const char *name
, struct type
*elt_type
,
951 struct type
*usize_type
)
955 elt_type
= lookup_pointer_type (elt_type
);
956 type
= rust_composite_type (elt_type
, name
,
957 "data_ptr", elt_type
,
958 "length", usize_type
);
965 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
967 static struct value
*
968 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
971 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
973 struct value
*function
, *result
, *arg0
;
974 struct type
*type
, *fn_type
;
975 const struct block
*block
;
976 struct block_symbol sym
;
978 /* For an ordinary function call we can simply defer to the
979 generic implementation. */
980 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
981 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
983 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
985 method
= &exp
->elts
[*pos
+ 1].string
;
986 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
988 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
989 type in order to look up the method. */
990 arg0
= evaluate_subexp (nullptr, exp
, pos
, noside
);
992 if (noside
== EVAL_SKIP
)
994 for (i
= 0; i
< num_args
; ++i
)
995 evaluate_subexp (nullptr, exp
, pos
, noside
);
999 std::vector
<struct value
*> args (num_args
+ 1);
1002 /* We don't yet implement real Deref semantics. */
1003 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1004 args
[0] = value_ind (args
[0]);
1006 type
= value_type (args
[0]);
1007 if ((type
->code () != TYPE_CODE_STRUCT
1008 && type
->code () != TYPE_CODE_UNION
1009 && type
->code () != TYPE_CODE_ENUM
)
1010 || rust_tuple_type_p (type
))
1011 error (_("Method calls only supported on struct or enum types"));
1012 if (type
->name () == NULL
)
1013 error (_("Method call on nameless type"));
1015 std::string name
= std::string (type
->name ()) + "::" + method
;
1017 block
= get_selected_block (0);
1018 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1019 if (sym
.symbol
== NULL
)
1020 error (_("Could not find function named '%s'"), name
.c_str ());
1022 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1023 if (fn_type
->num_fields () == 0)
1024 error (_("Function '%s' takes no arguments"), name
.c_str ());
1026 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1027 args
[0] = value_addr (args
[0]);
1029 function
= address_of_variable (sym
.symbol
, block
);
1031 for (i
= 0; i
< num_args
; ++i
)
1032 args
[i
+ 1] = evaluate_subexp (nullptr, exp
, pos
, noside
);
1034 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1035 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1037 result
= call_function_by_hand (function
, NULL
, args
);
1041 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1043 static struct value
*
1044 rust_range (struct type
*expect_type
, struct expression
*exp
,
1045 enum noside noside
, enum range_flag kind
,
1046 struct value
*low
, struct value
*high
)
1048 struct value
*addrval
, *result
;
1050 struct type
*range_type
;
1051 struct type
*index_type
;
1052 struct type
*temp_type
;
1055 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1057 if (noside
== EVAL_SKIP
)
1058 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1065 name
= "std::ops::RangeFull";
1069 index_type
= value_type (high
);
1071 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1078 index_type
= value_type (low
);
1079 name
= "std::ops::RangeFrom";
1083 if (!types_equal (value_type (low
), value_type (high
)))
1084 error (_("Range expression with different types"));
1085 index_type
= value_type (low
);
1086 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1090 /* If we don't have an index type, just allocate this on the
1091 arch. Here any type will do. */
1092 temp_type
= (index_type
== NULL
1093 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1095 /* It would be nicer to cache the range type. */
1096 range_type
= rust_composite_type (temp_type
, name
,
1097 low
== NULL
? NULL
: "start", index_type
,
1098 high
== NULL
? NULL
: "end", index_type
);
1100 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1101 return value_zero (range_type
, lval_memory
);
1103 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1104 addr
= value_as_long (addrval
);
1105 result
= value_at_lazy (range_type
, addr
);
1109 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1112 value_assign (start
, low
);
1117 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1120 value_assign (end
, high
);
1123 result
= value_at_lazy (range_type
, addr
);
1127 /* A helper function to compute the range and kind given a range
1128 value. TYPE is the type of the range value. RANGE is the range
1129 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1130 parameters might be filled in, or might not be, depending on the
1131 kind of range this is. KIND will always be set to the appropriate
1132 value describing the kind of range, and this can be used to
1133 determine whether LOW or HIGH are valid. */
1136 rust_compute_range (struct type
*type
, struct value
*range
,
1137 LONGEST
*low
, LONGEST
*high
,
1144 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1146 if (type
->num_fields () == 0)
1150 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1152 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1153 *low
= value_as_long (value_field (range
, 0));
1156 if (type
->num_fields () > i
1157 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1159 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1160 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1161 *high
= value_as_long (value_field (range
, i
));
1163 if (rust_inclusive_range_type_p (type
))
1168 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1170 static struct value
*
1171 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1172 enum noside noside
, bool for_addr
,
1173 struct value
*lhs
, struct value
*rhs
)
1175 struct value
*result
;
1176 struct type
*rhstype
;
1177 LONGEST low
, high_bound
;
1178 /* Initialized to appease the compiler. */
1179 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1183 if (noside
== EVAL_SKIP
)
1186 rhstype
= check_typedef (value_type (rhs
));
1187 if (rust_range_type_p (rhstype
))
1190 error (_("Can't take slice of array without '&'"));
1191 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1195 low
= value_as_long (rhs
);
1197 struct type
*type
= check_typedef (value_type (lhs
));
1198 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1200 struct type
*base_type
= nullptr;
1201 if (type
->code () == TYPE_CODE_ARRAY
)
1202 base_type
= TYPE_TARGET_TYPE (type
);
1203 else if (rust_slice_type_p (type
))
1205 for (int i
= 0; i
< type
->num_fields (); ++i
)
1207 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1209 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1213 if (base_type
== nullptr)
1214 error (_("Could not find 'data_ptr' in slice type"));
1216 else if (type
->code () == TYPE_CODE_PTR
)
1217 base_type
= TYPE_TARGET_TYPE (type
);
1219 error (_("Cannot subscript non-array type"));
1221 struct type
*new_type
;
1224 if (rust_slice_type_p (type
))
1229 = language_lookup_primitive_type (exp
->language_defn
,
1232 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1236 new_type
= base_type
;
1238 return value_zero (new_type
, VALUE_LVAL (lhs
));
1245 if (type
->code () == TYPE_CODE_ARRAY
)
1248 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1249 error (_("Can't compute array bounds"));
1251 error (_("Found array with non-zero lower bound"));
1254 else if (rust_slice_type_p (type
))
1258 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1259 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1261 high_bound
= value_as_long (len
);
1263 else if (type
->code () == TYPE_CODE_PTR
)
1267 high_bound
= LONGEST_MAX
;
1270 error (_("Cannot subscript non-array type"));
1272 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1275 error (_("Index less than zero"));
1276 if (low
> high_bound
)
1277 error (_("Index greater than length"));
1279 result
= value_subscript (base
, low
);
1286 struct type
*usize
, *slice
;
1288 struct value
*addrval
, *tem
;
1290 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1293 error (_("High index less than zero"));
1295 error (_("Low index greater than high index"));
1296 if (high
> high_bound
)
1297 error (_("High index greater than length"));
1299 usize
= language_lookup_primitive_type (exp
->language_defn
,
1302 const char *new_name
= ((type
!= nullptr
1303 && rust_slice_type_p (type
))
1304 ? type
->name () : "&[*gdb*]");
1306 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1308 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1309 addr
= value_as_long (addrval
);
1310 tem
= value_at_lazy (slice
, addr
);
1312 value_assign (value_field (tem
, 0), value_addr (result
));
1313 value_assign (value_field (tem
, 1),
1314 value_from_longest (usize
, high
- low
));
1316 result
= value_at_lazy (slice
, addr
);
1319 result
= value_addr (result
);
1325 /* A helper function for UNOP_IND. */
1327 static struct value
*
1328 eval_op_rust_ind (struct type
*expect_type
, struct expression
*exp
,
1330 struct value
*value
)
1332 gdb_assert (noside
== EVAL_NORMAL
);
1333 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1334 if (trait_ptr
!= NULL
)
1337 return value_ind (value
);
1340 /* evaluate_exp implementation for Rust. */
1342 static struct value
*
1343 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1344 int *pos
, enum noside noside
)
1346 struct value
*result
;
1348 switch (exp
->elts
[*pos
].opcode
)
1352 if (noside
!= EVAL_NORMAL
)
1353 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1357 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1359 result
= eval_op_rust_ind (expect_type
, exp
, noside
, value
);
1364 case UNOP_COMPLEMENT
:
1366 struct value
*value
;
1369 value
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1370 if (noside
== EVAL_SKIP
)
1372 /* Preserving the type is enough. */
1375 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1376 result
= value_from_longest (value_type (value
),
1377 value_logical_not (value
));
1379 result
= value_complement (value
);
1383 case BINOP_SUBSCRIPT
:
1386 struct value
*lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1387 struct value
*rhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1388 result
= rust_subscript (expect_type
, exp
, noside
, false, lhs
, rhs
);
1393 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1399 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1400 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1403 struct value
*addrval
= NULL
;
1407 if (noside
== EVAL_NORMAL
)
1409 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1410 addr
= value_as_long (addrval
);
1411 result
= value_at_lazy (type
, addr
);
1414 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1419 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1420 if (noside
== EVAL_NORMAL
)
1422 /* This isn't quite right but will do for the time
1423 being, seeing that we can't implement the Copy
1425 value_assign (result
, init
);
1431 gdb_assert (arglen
% 2 == 0);
1432 for (i
= 0; i
< arglen
; i
+= 2)
1435 const char *fieldname
;
1436 struct value
*value
, *field
;
1438 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1440 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1442 fieldname
= &exp
->elts
[*pos
].string
;
1443 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1445 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1446 if (noside
== EVAL_NORMAL
)
1448 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1450 value_assign (field
, value
);
1454 if (noside
== EVAL_SKIP
)
1455 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1457 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1458 result
= allocate_value (type
);
1460 result
= value_at_lazy (type
, addr
);
1469 struct value
*ncopies
;
1471 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1472 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1473 copies
= value_as_long (ncopies
);
1475 error (_("Array with negative number of elements"));
1477 if (noside
== EVAL_NORMAL
)
1480 std::vector
<struct value
*> eltvec (copies
);
1482 for (i
= 0; i
< copies
; ++i
)
1484 result
= value_array (0, copies
- 1, eltvec
.data ());
1488 struct type
*arraytype
1489 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1490 result
= allocate_value (arraytype
);
1495 case STRUCTOP_ANONYMOUS
:
1497 /* Anonymous field access, i.e. foo.1. */
1499 int pc
, field_number
, nfields
;
1503 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1505 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1507 type
= value_type (lhs
);
1509 if (type
->code () == TYPE_CODE_STRUCT
)
1511 struct type
*outer_type
= NULL
;
1513 if (rust_enum_p (type
))
1515 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1516 TYPE_LENGTH (type
));
1517 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1519 if (rust_empty_enum_p (type
))
1520 error (_("Cannot access field %d of empty enum %s"),
1521 field_number
, type
->name ());
1523 int fieldno
= rust_enum_variant (type
);
1524 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1526 type
= value_type (lhs
);
1529 /* Tuples and tuple structs */
1530 nfields
= type
->num_fields ();
1532 if (field_number
>= nfields
|| field_number
< 0)
1534 if (outer_type
!= NULL
)
1535 error(_("Cannot access field %d of variant %s::%s, "
1536 "there are only %d fields"),
1537 field_number
, outer_type
->name (),
1538 rust_last_path_segment (type
->name ()),
1541 error(_("Cannot access field %d of %s, "
1542 "there are only %d fields"),
1543 field_number
, type
->name (), nfields
);
1546 /* Tuples are tuple structs too. */
1547 if (!rust_tuple_struct_type_p (type
))
1549 if (outer_type
!= NULL
)
1550 error(_("Variant %s::%s is not a tuple variant"),
1551 outer_type
->name (),
1552 rust_last_path_segment (type
->name ()));
1554 error(_("Attempting to access anonymous field %d "
1555 "of %s, which is not a tuple, tuple struct, or "
1556 "tuple-like variant"),
1557 field_number
, type
->name ());
1560 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1563 error(_("Anonymous field access is only allowed on tuples, \
1564 tuple structs, and tuple-like enum variants"));
1568 case STRUCTOP_STRUCT
:
1575 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1576 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1577 lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1579 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1580 type
= value_type (lhs
);
1581 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1583 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1584 TYPE_LENGTH (type
));
1585 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1587 if (rust_empty_enum_p (type
))
1588 error (_("Cannot access field %s of empty enum %s"),
1589 field_name
, type
->name ());
1591 int fieldno
= rust_enum_variant (type
);
1592 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1594 struct type
*outer_type
= type
;
1595 type
= value_type (lhs
);
1596 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1597 error (_("Attempting to access named field %s of tuple "
1598 "variant %s::%s, which has only anonymous fields"),
1599 field_name
, outer_type
->name (),
1600 rust_last_path_segment (type
->name ()));
1604 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1607 catch (const gdb_exception_error
&except
)
1609 error (_("Could not find field %s of struct variant %s::%s"),
1610 field_name
, outer_type
->name (),
1611 rust_last_path_segment (type
->name ()));
1615 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1616 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1617 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1623 struct value
*low
= NULL
, *high
= NULL
;
1625 = (enum range_flag
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1628 if (!(kind
& RANGE_LOW_BOUND_DEFAULT
))
1629 low
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1630 if (!(kind
& RANGE_HIGH_BOUND_DEFAULT
))
1631 high
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1633 result
= rust_range (expect_type
, exp
, noside
, kind
, low
, high
);
1638 /* We might have &array[range], in which case we need to make a
1640 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1644 struct value
*lhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1645 struct value
*rhs
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1647 result
= rust_subscript (expect_type
, exp
, noside
, true, lhs
, rhs
);
1652 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1659 /* operator_length implementation for Rust. */
1662 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1668 switch (exp
->elts
[pc
- 1].opcode
)
1671 /* We handle aggregate as a type and argument count. The first
1672 argument might be OP_OTHERS. After that the arguments
1673 alternate: first an OP_NAME, then an expression. */
1675 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1683 case STRUCTOP_ANONYMOUS
:
1694 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1702 /* dump_subexp_body implementation for Rust. */
1705 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1708 switch (exp
->elts
[elt
].opcode
)
1712 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1715 fprintf_filtered (stream
, "Type @");
1716 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1717 fprintf_filtered (stream
, " (");
1718 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1719 fprintf_filtered (stream
, "), length %d", length
);
1722 for (i
= 0; i
< length
; ++i
)
1723 elt
= dump_subexp (exp
, stream
, elt
);
1730 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1732 fprintf_filtered (stream
, "%s: %s",
1733 (exp
->elts
[elt
].opcode
== OP_STRING
1734 ? "string" : "name"),
1735 &exp
->elts
[elt
+ 2].string
);
1736 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1741 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1744 case STRUCTOP_ANONYMOUS
:
1748 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1750 fprintf_filtered (stream
, "Field number: %d", field_number
);
1751 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1760 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1767 /* print_subexp implementation for Rust. */
1770 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1771 enum precedence prec
)
1773 switch (exp
->elts
[*pos
].opcode
)
1777 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1780 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1781 fputs_filtered (" { ", stream
);
1784 for (i
= 0; i
< length
; ++i
)
1786 rust_print_subexp (exp
, pos
, stream
, prec
);
1787 fputs_filtered (", ", stream
);
1789 fputs_filtered (" }", stream
);
1795 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1797 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1798 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1804 fputs_filtered ("<<others>> (", stream
);
1806 rust_print_subexp (exp
, pos
, stream
, prec
);
1807 fputs_filtered (")", stream
);
1811 case STRUCTOP_ANONYMOUS
:
1813 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1816 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1817 fprintf_filtered (stream
, ".%d", tem
);
1823 fprintf_filtered (stream
, "[");
1824 rust_print_subexp (exp
, pos
, stream
, prec
);
1825 fprintf_filtered (stream
, "; ");
1826 rust_print_subexp (exp
, pos
, stream
, prec
);
1827 fprintf_filtered (stream
, "]");
1831 print_subexp_standard (exp
, pos
, stream
, prec
);
1836 /* operator_check implementation for Rust. */
1839 rust_operator_check (struct expression
*exp
, int pos
,
1840 int (*objfile_func
) (struct objfile
*objfile
,
1844 switch (exp
->elts
[pos
].opcode
)
1848 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1849 struct objfile
*objfile
= type
->objfile_owner ();
1851 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1862 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1870 const struct exp_descriptor
rust_language::exp_descriptor_tab
=
1873 rust_operator_length
,
1874 rust_operator_check
,
1875 rust_dump_subexp_body
,
1876 rust_evaluate_subexp
1879 /* See language.h. */
1882 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1883 struct language_arch_info
*lai
) const
1885 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1887 /* Helper function to allow shorter lines below. */
1888 auto add
= [&] (struct type
* t
) -> struct type
*
1890 lai
->add_primitive_type (t
);
1894 struct type
*bool_type
1895 = add (arch_boolean_type (gdbarch
, 8, 1, "bool"));
1896 add (arch_character_type (gdbarch
, 32, 1, "char"));
1897 add (arch_integer_type (gdbarch
, 8, 0, "i8"));
1898 struct type
*u8_type
1899 = add (arch_integer_type (gdbarch
, 8, 1, "u8"));
1900 add (arch_integer_type (gdbarch
, 16, 0, "i16"));
1901 add (arch_integer_type (gdbarch
, 16, 1, "u16"));
1902 add (arch_integer_type (gdbarch
, 32, 0, "i32"));
1903 add (arch_integer_type (gdbarch
, 32, 1, "u32"));
1904 add (arch_integer_type (gdbarch
, 64, 0, "i64"));
1905 add (arch_integer_type (gdbarch
, 64, 1, "u64"));
1907 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1908 add (arch_integer_type (gdbarch
, length
, 0, "isize"));
1909 struct type
*usize_type
1910 = add (arch_integer_type (gdbarch
, length
, 1, "usize"));
1912 add (arch_float_type (gdbarch
, 32, "f32", floatformats_ieee_single
));
1913 add (arch_float_type (gdbarch
, 64, "f64", floatformats_ieee_double
));
1914 add (arch_integer_type (gdbarch
, 0, 1, "()"));
1916 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1917 add (rust_slice_type ("&str", tem
, usize_type
));
1919 lai
->set_bool_type (bool_type
);
1920 lai
->set_string_char_type (u8_type
);
1923 /* See language.h. */
1926 rust_language::print_type (struct type
*type
, const char *varstring
,
1927 struct ui_file
*stream
, int show
, int level
,
1928 const struct type_print_options
*flags
) const
1930 print_offset_data podata
;
1931 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1932 flags
, false, &podata
);
1935 /* See language.h. */
1938 rust_language::emitchar (int ch
, struct type
*chtype
,
1939 struct ui_file
*stream
, int quoter
) const
1941 if (!rust_chartype_p (chtype
))
1942 generic_emit_char (ch
, chtype
, stream
, quoter
,
1943 target_charset (chtype
->arch ()));
1944 else if (ch
== '\\' || ch
== quoter
)
1945 fprintf_filtered (stream
, "\\%c", ch
);
1946 else if (ch
== '\n')
1947 fputs_filtered ("\\n", stream
);
1948 else if (ch
== '\r')
1949 fputs_filtered ("\\r", stream
);
1950 else if (ch
== '\t')
1951 fputs_filtered ("\\t", stream
);
1952 else if (ch
== '\0')
1953 fputs_filtered ("\\0", stream
);
1954 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1955 fputc_filtered (ch
, stream
);
1957 fprintf_filtered (stream
, "\\x%02x", ch
);
1959 fprintf_filtered (stream
, "\\u{%06x}", ch
);
1962 /* See language.h. */
1965 rust_language::is_string_type_p (struct type
*type
) const
1967 LONGEST low_bound
, high_bound
;
1969 type
= check_typedef (type
);
1970 return ((type
->code () == TYPE_CODE_STRING
)
1971 || (type
->code () == TYPE_CODE_PTR
1972 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
1973 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
1974 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
1976 || (type
->code () == TYPE_CODE_STRUCT
1977 && !rust_enum_p (type
)
1978 && rust_slice_type_p (type
)
1979 && strcmp (type
->name (), "&str") == 0));
1982 /* Single instance of the Rust language class. */
1984 static rust_language rust_language_defn
;