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 (struct type
*type
)
73 /* is_dynamic_type will return true if any field has a dynamic
74 attribute -- but we only want to check the top level. */
75 return TYPE_HAS_VARIANT_PARTS (type
);
78 /* Return true if TYPE, which must be an already-resolved enum type,
82 rust_empty_enum_p (const struct type
*type
)
84 return type
->num_fields () == 0;
87 /* Given an already-resolved enum type and contents, find which
91 rust_enum_variant (struct type
*type
)
93 /* The active variant is simply the first non-artificial field. */
94 for (int i
= 0; i
< type
->num_fields (); ++i
)
95 if (!TYPE_FIELD_ARTIFICIAL (type
, i
))
98 /* Perhaps we could get here by trying to print an Ada variant
99 record in Rust mode. Unlikely, but an error is safer than an
101 error (_("Could not find active enum variant"));
104 /* See rust-lang.h. */
107 rust_tuple_type_p (struct type
*type
)
109 /* The current implementation is a bit of a hack, but there's
110 nothing else in the debuginfo to distinguish a tuple from a
112 return (type
->code () == TYPE_CODE_STRUCT
113 && type
->name () != NULL
114 && type
->name ()[0] == '(');
117 /* Return true if all non-static fields of a structlike type are in a
118 sequence like __0, __1, __2. */
121 rust_underscore_fields (struct type
*type
)
127 if (type
->code () != TYPE_CODE_STRUCT
)
129 for (i
= 0; i
< type
->num_fields (); ++i
)
131 if (!field_is_static (&type
->field (i
)))
135 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
136 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
144 /* See rust-lang.h. */
147 rust_tuple_struct_type_p (struct type
*type
)
149 /* This is just an approximation until DWARF can represent Rust more
150 precisely. We exclude zero-length structs because they may not
151 be tuple structs, and there's no way to tell. */
152 return type
->num_fields () > 0 && rust_underscore_fields (type
);
155 /* Return true if TYPE is a slice type, otherwise false. */
158 rust_slice_type_p (struct type
*type
)
160 return (type
->code () == TYPE_CODE_STRUCT
161 && type
->name () != NULL
162 && (strncmp (type
->name (), "&[", 2) == 0
163 || strcmp (type
->name (), "&str") == 0));
166 /* Return true if TYPE is a range type, otherwise false. */
169 rust_range_type_p (struct type
*type
)
173 if (type
->code () != TYPE_CODE_STRUCT
174 || type
->num_fields () > 2
175 || type
->name () == NULL
176 || strstr (type
->name (), "::Range") == NULL
)
179 if (type
->num_fields () == 0)
183 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
185 if (type
->num_fields () == 1)
189 else if (type
->num_fields () == 2)
191 /* First field had to be "start". */
195 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
198 /* Return true if TYPE is an inclusive range type, otherwise false.
199 This is only valid for types which are already known to be range
203 rust_inclusive_range_type_p (struct type
*type
)
205 return (strstr (type
->name (), "::RangeInclusive") != NULL
206 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
209 /* Return true if TYPE seems to be the type "u8", otherwise false. */
212 rust_u8_type_p (struct type
*type
)
214 return (type
->code () == TYPE_CODE_INT
215 && TYPE_UNSIGNED (type
)
216 && TYPE_LENGTH (type
) == 1);
219 /* Return true if TYPE is a Rust character type. */
222 rust_chartype_p (struct type
*type
)
224 return (type
->code () == TYPE_CODE_CHAR
225 && TYPE_LENGTH (type
) == 4
226 && TYPE_UNSIGNED (type
));
229 /* Return true if TYPE is a string type. */
232 rust_is_string_type_p (struct type
*type
)
234 LONGEST low_bound
, high_bound
;
236 type
= check_typedef (type
);
237 return ((type
->code () == TYPE_CODE_STRING
)
238 || (type
->code () == TYPE_CODE_PTR
239 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
240 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
241 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
243 || (type
->code () == TYPE_CODE_STRUCT
244 && !rust_enum_p (type
)
245 && rust_slice_type_p (type
)
246 && strcmp (type
->name (), "&str") == 0));
249 /* If VALUE represents a trait object pointer, return the underlying
250 pointer with the correct (i.e., runtime) type. Otherwise, return
253 static struct value
*
254 rust_get_trait_object_pointer (struct value
*value
)
256 struct type
*type
= check_typedef (value_type (value
));
258 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
261 /* Try to be a bit resilient if the ABI changes. */
262 int vtable_field
= 0;
263 for (int i
= 0; i
< 2; ++i
)
265 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
267 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
271 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
272 struct symbol
*symbol
= find_symbol_at_address (vtable
);
273 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
276 struct rust_vtable_symbol
*vtable_sym
277 = static_cast<struct rust_vtable_symbol
*> (symbol
);
278 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
279 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
284 /* language_defn::printstr implementation for Rust. */
287 rust_printstr (struct ui_file
*stream
, struct type
*type
,
288 const gdb_byte
*string
, unsigned int length
,
289 const char *user_encoding
, int force_ellipses
,
290 const struct value_print_options
*options
)
292 /* Rust always uses UTF-8, but let the caller override this if need
294 const char *encoding
= user_encoding
;
295 if (user_encoding
== NULL
|| !*user_encoding
)
297 /* In Rust strings, characters are "u8". */
298 if (rust_u8_type_p (type
))
302 /* This is probably some C string, so let's let C deal with
304 c_printstr (stream
, type
, string
, length
, user_encoding
,
305 force_ellipses
, options
);
310 /* This is not ideal as it doesn't use our character printer. */
311 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
317 static void rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
319 const struct value_print_options
*options
);
321 /* Helper function to print a string slice. */
324 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
325 const struct value_print_options
*options
)
327 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
329 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
331 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
332 value_as_address (base
), value_as_long (len
), stream
,
336 /* rust_val_print helper for structs and untagged unions. */
339 val_print_struct (struct value
*val
, struct ui_file
*stream
, int recurse
,
340 const struct value_print_options
*options
)
344 struct type
*type
= check_typedef (value_type (val
));
346 if (rust_slice_type_p (type
) && strcmp (type
->name (), "&str") == 0)
348 /* If what we are printing here is actually a string within a
349 structure then VAL will be the original parent value, while TYPE
350 will be the type of the structure representing the string we want
352 However, RUST_VAL_PRINT_STR looks up the fields of the string
353 inside VAL, assuming that VAL is the string.
354 So, recreate VAL as a value representing just the string. */
355 val
= value_at_lazy (type
, value_address (val
));
356 rust_val_print_str (stream
, val
, options
);
360 bool is_tuple
= rust_tuple_type_p (type
);
361 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
362 struct value_print_options opts
;
366 if (type
->name () != NULL
)
367 fprintf_filtered (stream
, "%s", type
->name ());
369 if (type
->num_fields () == 0)
372 if (type
->name () != NULL
)
373 fputs_filtered (" ", stream
);
376 if (is_tuple
|| is_tuple_struct
)
377 fputs_filtered ("(", stream
);
379 fputs_filtered ("{", stream
);
385 for (i
= 0; i
< type
->num_fields (); ++i
)
387 if (field_is_static (&type
->field (i
)))
391 fputs_filtered (",", stream
);
393 if (options
->prettyformat
)
395 fputs_filtered ("\n", stream
);
396 print_spaces_filtered (2 + 2 * recurse
, stream
);
398 else if (!first_field
)
399 fputs_filtered (" ", stream
);
403 if (!is_tuple
&& !is_tuple_struct
)
405 fputs_styled (TYPE_FIELD_NAME (type
, i
),
406 variable_name_style
.style (), stream
);
407 fputs_filtered (": ", stream
);
410 rust_value_print_inner (value_field (val
, i
), stream
, recurse
+ 1,
414 if (options
->prettyformat
)
416 fputs_filtered ("\n", stream
);
417 print_spaces_filtered (2 * recurse
, stream
);
420 if (is_tuple
|| is_tuple_struct
)
421 fputs_filtered (")", stream
);
423 fputs_filtered ("}", stream
);
426 /* rust_val_print helper for discriminated unions (Rust enums). */
429 rust_print_enum (struct value
*val
, struct ui_file
*stream
, int recurse
,
430 const struct value_print_options
*options
)
432 struct value_print_options opts
= *options
;
433 struct type
*type
= check_typedef (value_type (val
));
437 gdb_assert (rust_enum_p (type
));
438 gdb::array_view
<const gdb_byte
> view (value_contents_for_printing (val
),
439 TYPE_LENGTH (value_type (val
)));
440 type
= resolve_dynamic_type (type
, view
, value_address (val
));
442 if (rust_empty_enum_p (type
))
444 /* Print the enum type name here to be more clear. */
445 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
447 metadata_style
.style ().ptr (), nullptr);
451 int variant_fieldno
= rust_enum_variant (type
);
452 val
= value_field (val
, variant_fieldno
);
453 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
455 int nfields
= variant_type
->num_fields ();
457 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
459 fprintf_filtered (stream
, "%s", variant_type
->name ());
462 /* In case of a nullary variant like 'None', just output
467 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
469 fprintf_filtered (stream
, "(");
472 /* struct variant. */
473 fprintf_filtered (stream
, "{");
476 bool first_field
= true;
477 for (int j
= 0; j
< variant_type
->num_fields (); j
++)
480 fputs_filtered (", ", stream
);
484 fprintf_filtered (stream
, "%ps: ",
485 styled_string (variable_name_style
.style (),
486 TYPE_FIELD_NAME (variant_type
, j
)));
488 rust_value_print_inner (value_field (val
, j
), stream
, recurse
+ 1,
493 fputs_filtered (")", stream
);
495 fputs_filtered ("}", stream
);
498 static const struct generic_val_print_decorations rust_decorations
=
500 /* Complex isn't used in Rust, but we provide C-ish values just in
512 /* la_value_print_inner implementation for Rust. */
514 rust_value_print_inner (struct value
*val
, struct ui_file
*stream
,
516 const struct value_print_options
*options
)
518 struct value_print_options opts
= *options
;
521 if (opts
.prettyformat
== Val_prettyformat_default
)
522 opts
.prettyformat
= (opts
.prettyformat_structs
523 ? Val_prettyformat
: Val_no_prettyformat
);
525 struct type
*type
= check_typedef (value_type (val
));
526 switch (type
->code ())
530 LONGEST low_bound
, high_bound
;
532 if (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_ARRAY
533 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
534 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
537 /* We have a pointer to a byte string, so just print
539 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
540 CORE_ADDR addr
= value_as_address (val
);
541 struct gdbarch
*arch
= get_type_arch (type
);
543 if (opts
.addressprint
)
545 fputs_filtered (paddress (arch
, addr
), stream
);
546 fputs_filtered (" ", stream
);
549 fputs_filtered ("b", stream
);
550 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
551 high_bound
- low_bound
+ 1, stream
,
558 case TYPE_CODE_METHODPTR
:
559 case TYPE_CODE_MEMBERPTR
:
560 c_value_print_inner (val
, stream
, recurse
, &opts
);
564 /* Recognize the unit type. */
565 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
566 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
568 fputs_filtered ("()", stream
);
573 case TYPE_CODE_STRING
:
575 LONGEST low_bound
, high_bound
;
577 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
578 error (_("Could not determine the array bounds"));
580 /* If we see a plain TYPE_CODE_STRING, then we're printing a
581 byte string, hence the choice of "ASCII" as the
583 fputs_filtered ("b", stream
);
584 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
585 value_contents_for_printing (val
),
586 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
590 case TYPE_CODE_ARRAY
:
592 LONGEST low_bound
, high_bound
;
594 if (get_array_bounds (type
, &low_bound
, &high_bound
)
595 && high_bound
- low_bound
+ 1 == 0)
596 fputs_filtered ("[]", stream
);
602 case TYPE_CODE_UNION
:
603 /* Untagged unions are printed as if they are structs. Since
604 the field bit positions overlap in the debuginfo, the code
605 for printing a union is same as that for a struct, the only
606 difference is that the input type will have overlapping
608 val_print_struct (val
, stream
, recurse
, &opts
);
611 case TYPE_CODE_STRUCT
:
612 if (rust_enum_p (type
))
613 rust_print_enum (val
, stream
, recurse
, &opts
);
615 val_print_struct (val
, stream
, recurse
, &opts
);
620 /* Nothing special yet. */
621 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
628 rust_internal_print_type (struct type
*type
, const char *varstring
,
629 struct ui_file
*stream
, int show
, int level
,
630 const struct type_print_options
*flags
,
631 bool for_rust_enum
, print_offset_data
*podata
);
633 /* Print a struct or union typedef. */
635 rust_print_struct_def (struct type
*type
, const char *varstring
,
636 struct ui_file
*stream
, int show
, int level
,
637 const struct type_print_options
*flags
,
638 bool for_rust_enum
, print_offset_data
*podata
)
640 /* Print a tuple type simply. */
641 if (rust_tuple_type_p (type
))
643 fputs_filtered (type
->name (), stream
);
647 /* If we see a base class, delegate to C. */
648 if (TYPE_N_BASECLASSES (type
) > 0)
649 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
651 if (flags
->print_offsets
)
653 /* Temporarily bump the level so that the output lines up
658 /* Compute properties of TYPE here because, in the enum case, the
659 rest of the code ends up looking only at the variant part. */
660 const char *tagname
= type
->name ();
661 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
662 bool is_tuple
= rust_tuple_type_p (type
);
663 bool is_enum
= rust_enum_p (type
);
667 /* Already printing an outer enum, so nothing to print here. */
671 /* This code path is also used by unions and enums. */
674 fputs_filtered ("enum ", stream
);
675 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
676 if (prop
!= nullptr && prop
->kind
== PROP_TYPE
)
677 type
= prop
->data
.original_type
;
679 else if (type
->code () == TYPE_CODE_STRUCT
)
680 fputs_filtered ("struct ", stream
);
682 fputs_filtered ("union ", stream
);
685 fputs_filtered (tagname
, stream
);
688 if (type
->num_fields () == 0 && !is_tuple
)
690 if (for_rust_enum
&& !flags
->print_offsets
)
691 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
693 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
695 /* When printing offsets, we rearrange the fields into storage
696 order. This lets us show holes more clearly. We work using
697 field indices here because it simplifies calls to
698 print_offset_data::update below. */
699 std::vector
<int> fields
;
700 for (int i
= 0; i
< type
->num_fields (); ++i
)
702 if (field_is_static (&type
->field (i
)))
704 if (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
706 fields
.push_back (i
);
708 if (flags
->print_offsets
)
709 std::sort (fields
.begin (), fields
.end (),
712 return (TYPE_FIELD_BITPOS (type
, a
)
713 < TYPE_FIELD_BITPOS (type
, b
));
720 gdb_assert (!field_is_static (&type
->field (i
)));
721 gdb_assert (! (is_enum
&& TYPE_FIELD_ARTIFICIAL (type
, i
)));
723 if (flags
->print_offsets
)
724 podata
->update (type
, i
, stream
);
726 /* We'd like to print "pub" here as needed, but rustc
727 doesn't emit the debuginfo, and our types don't have
728 cplus_struct_type attached. */
730 /* For a tuple struct we print the type but nothing
732 if (!for_rust_enum
|| flags
->print_offsets
)
733 print_spaces_filtered (level
+ 2, stream
);
735 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
737 else if (!is_tuple_struct
)
738 fprintf_filtered (stream
, "%ps: ",
739 styled_string (variable_name_style
.style (),
740 TYPE_FIELD_NAME (type
, i
)));
742 rust_internal_print_type (type
->field (i
).type (), NULL
,
743 stream
, (is_enum
? show
: show
- 1),
744 level
+ 2, flags
, is_enum
, podata
);
745 if (!for_rust_enum
|| flags
->print_offsets
)
746 fputs_filtered (",\n", stream
);
747 /* Note that this check of "I" is ok because we only sorted the
748 fields by offset when print_offsets was set, so we won't take
749 this branch in that case. */
750 else if (i
+ 1 < type
->num_fields ())
751 fputs_filtered (", ", stream
);
754 if (flags
->print_offsets
)
756 /* Undo the temporary level increase we did above. */
758 podata
->finish (type
, level
, stream
);
759 print_spaces_filtered (print_offset_data::indentation
, stream
);
761 print_spaces_filtered (2, stream
);
763 if (!for_rust_enum
|| flags
->print_offsets
)
764 print_spaces_filtered (level
, stream
);
765 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
768 /* la_print_type implementation for Rust. */
771 rust_internal_print_type (struct type
*type
, const char *varstring
,
772 struct ui_file
*stream
, int show
, int level
,
773 const struct type_print_options
*flags
,
774 bool for_rust_enum
, print_offset_data
*podata
)
778 && type
->name () != NULL
)
780 /* Rust calls the unit type "void" in its debuginfo,
781 but we don't want to print it as that. */
782 if (type
->code () == TYPE_CODE_VOID
)
783 fputs_filtered ("()", stream
);
785 fputs_filtered (type
->name (), stream
);
789 type
= check_typedef (type
);
790 switch (type
->code ())
793 /* If we have an enum, we've already printed the type's
794 unqualified name, and there is nothing else to print
797 fputs_filtered ("()", stream
);
801 /* Delegate varargs to the C printer. */
802 if (TYPE_VARARGS (type
))
805 fputs_filtered ("fn ", stream
);
806 if (varstring
!= NULL
)
807 fputs_filtered (varstring
, stream
);
808 fputs_filtered ("(", stream
);
809 for (int i
= 0; i
< type
->num_fields (); ++i
)
813 fputs_filtered (", ", stream
);
814 rust_internal_print_type (type
->field (i
).type (), "", stream
,
815 -1, 0, flags
, false, podata
);
817 fputs_filtered (")", stream
);
818 /* If it returns unit, we can omit the return type. */
819 if (TYPE_TARGET_TYPE (type
)->code () != TYPE_CODE_VOID
)
821 fputs_filtered (" -> ", stream
);
822 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
823 -1, 0, flags
, false, podata
);
827 case TYPE_CODE_ARRAY
:
829 LONGEST low_bound
, high_bound
;
831 fputs_filtered ("[", stream
);
832 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
833 stream
, show
- 1, level
, flags
, false,
836 if (TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCEXPR
837 || TYPE_HIGH_BOUND_KIND (type
->index_type ()) == PROP_LOCLIST
)
838 fprintf_filtered (stream
, "; variable length");
839 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
840 fprintf_filtered (stream
, "; %s",
841 plongest (high_bound
- low_bound
+ 1));
842 fputs_filtered ("]", stream
);
846 case TYPE_CODE_UNION
:
847 case TYPE_CODE_STRUCT
:
848 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
849 for_rust_enum
, podata
);
856 fputs_filtered ("enum ", stream
);
857 if (type
->name () != NULL
)
859 fputs_filtered (type
->name (), stream
);
860 fputs_filtered (" ", stream
);
861 len
= strlen (type
->name ());
863 fputs_filtered ("{\n", stream
);
865 for (int i
= 0; i
< type
->num_fields (); ++i
)
867 const char *name
= TYPE_FIELD_NAME (type
, i
);
872 && strncmp (name
, type
->name (), len
) == 0
874 && name
[len
+ 1] == ':')
876 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
877 styled_string (variable_name_style
.style (),
881 fputs_filtered ("}", stream
);
887 if (type
->name () != nullptr)
888 fputs_filtered (type
->name (), stream
);
891 /* We currently can't distinguish between pointers and
893 fputs_filtered ("*mut ", stream
);
894 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
901 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
907 /* Like arch_composite_type, but uses TYPE to decide how to allocate
908 -- either on an obstack or on a gdbarch. */
911 rust_composite_type (struct type
*original
,
913 const char *field1
, struct type
*type1
,
914 const char *field2
, struct type
*type2
)
916 struct type
*result
= alloc_type_copy (original
);
917 int i
, nfields
, bitpos
;
925 result
->set_code (TYPE_CODE_STRUCT
);
926 result
->set_name (name
);
928 result
->set_num_fields (nfields
);
930 ((struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
)));
936 struct field
*field
= &result
->field (i
);
938 SET_FIELD_BITPOS (*field
, bitpos
);
939 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
941 FIELD_NAME (*field
) = field1
;
942 field
->set_type (type1
);
947 struct field
*field
= &result
->field (i
);
948 unsigned align
= type_align (type2
);
954 align
*= TARGET_CHAR_BIT
;
955 delta
= bitpos
% align
;
957 bitpos
+= align
- delta
;
959 SET_FIELD_BITPOS (*field
, bitpos
);
961 FIELD_NAME (*field
) = field2
;
962 field
->set_type (type2
);
968 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
969 TYPE_LENGTH (result
->field (i
- 1).type ()));
973 /* See rust-lang.h. */
976 rust_slice_type (const char *name
, struct type
*elt_type
,
977 struct type
*usize_type
)
981 elt_type
= lookup_pointer_type (elt_type
);
982 type
= rust_composite_type (elt_type
, name
,
983 "data_ptr", elt_type
,
984 "length", usize_type
);
989 enum rust_primitive_types
1001 rust_primitive_isize
,
1002 rust_primitive_usize
,
1005 rust_primitive_unit
,
1007 nr_rust_primitive_types
1012 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1014 static struct value
*
1015 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1018 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1020 struct value
*function
, *result
, *arg0
;
1021 struct type
*type
, *fn_type
;
1022 const struct block
*block
;
1023 struct block_symbol sym
;
1025 /* For an ordinary function call we can simply defer to the
1026 generic implementation. */
1027 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1028 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1030 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1032 method
= &exp
->elts
[*pos
+ 1].string
;
1033 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1035 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1036 type in order to look up the method. */
1037 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1039 if (noside
== EVAL_SKIP
)
1041 for (i
= 0; i
< num_args
; ++i
)
1042 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1046 std::vector
<struct value
*> args (num_args
+ 1);
1049 /* We don't yet implement real Deref semantics. */
1050 while (value_type (args
[0])->code () == TYPE_CODE_PTR
)
1051 args
[0] = value_ind (args
[0]);
1053 type
= value_type (args
[0]);
1054 if ((type
->code () != TYPE_CODE_STRUCT
1055 && type
->code () != TYPE_CODE_UNION
1056 && type
->code () != TYPE_CODE_ENUM
)
1057 || rust_tuple_type_p (type
))
1058 error (_("Method calls only supported on struct or enum types"));
1059 if (type
->name () == NULL
)
1060 error (_("Method call on nameless type"));
1062 std::string name
= std::string (type
->name ()) + "::" + method
;
1064 block
= get_selected_block (0);
1065 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1066 if (sym
.symbol
== NULL
)
1067 error (_("Could not find function named '%s'"), name
.c_str ());
1069 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1070 if (fn_type
->num_fields () == 0)
1071 error (_("Function '%s' takes no arguments"), name
.c_str ());
1073 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1074 args
[0] = value_addr (args
[0]);
1076 function
= address_of_variable (sym
.symbol
, block
);
1078 for (i
= 0; i
< num_args
; ++i
)
1079 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1081 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1082 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1084 result
= call_function_by_hand (function
, NULL
, args
);
1088 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1090 static struct value
*
1091 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1093 enum range_type kind
;
1094 struct value
*low
= NULL
, *high
= NULL
;
1095 struct value
*addrval
, *result
;
1097 struct type
*range_type
;
1098 struct type
*index_type
;
1099 struct type
*temp_type
;
1102 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1105 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1106 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1107 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1108 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1109 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1110 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1111 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1113 if (noside
== EVAL_SKIP
)
1114 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1121 name
= "std::ops::RangeFull";
1125 index_type
= value_type (high
);
1127 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1134 index_type
= value_type (low
);
1135 name
= "std::ops::RangeFrom";
1139 if (!types_equal (value_type (low
), value_type (high
)))
1140 error (_("Range expression with different types"));
1141 index_type
= value_type (low
);
1142 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1146 /* If we don't have an index type, just allocate this on the
1147 arch. Here any type will do. */
1148 temp_type
= (index_type
== NULL
1149 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1151 /* It would be nicer to cache the range type. */
1152 range_type
= rust_composite_type (temp_type
, name
,
1153 low
== NULL
? NULL
: "start", index_type
,
1154 high
== NULL
? NULL
: "end", index_type
);
1156 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1157 return value_zero (range_type
, lval_memory
);
1159 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1160 addr
= value_as_long (addrval
);
1161 result
= value_at_lazy (range_type
, addr
);
1165 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1168 value_assign (start
, low
);
1173 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1176 value_assign (end
, high
);
1179 result
= value_at_lazy (range_type
, addr
);
1183 /* A helper function to compute the range and kind given a range
1184 value. TYPE is the type of the range value. RANGE is the range
1185 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1186 parameters might be filled in, or might not be, depending on the
1187 kind of range this is. KIND will always be set to the appropriate
1188 value describing the kind of range, and this can be used to
1189 determine whether LOW or HIGH are valid. */
1192 rust_compute_range (struct type
*type
, struct value
*range
,
1193 LONGEST
*low
, LONGEST
*high
,
1194 enum range_type
*kind
)
1200 *kind
= BOTH_BOUND_DEFAULT
;
1202 if (type
->num_fields () == 0)
1206 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1208 *kind
= HIGH_BOUND_DEFAULT
;
1209 *low
= value_as_long (value_field (range
, 0));
1212 if (type
->num_fields () > i
1213 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1215 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1216 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1217 *high
= value_as_long (value_field (range
, i
));
1219 if (rust_inclusive_range_type_p (type
))
1224 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1226 static struct value
*
1227 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1230 struct value
*lhs
, *rhs
, *result
;
1231 struct type
*rhstype
;
1232 LONGEST low
, high_bound
;
1233 /* Initialized to appease the compiler. */
1234 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1239 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1240 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1242 if (noside
== EVAL_SKIP
)
1245 rhstype
= check_typedef (value_type (rhs
));
1246 if (rust_range_type_p (rhstype
))
1249 error (_("Can't take slice of array without '&'"));
1250 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1254 low
= value_as_long (rhs
);
1256 struct type
*type
= check_typedef (value_type (lhs
));
1257 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1259 struct type
*base_type
= nullptr;
1260 if (type
->code () == TYPE_CODE_ARRAY
)
1261 base_type
= TYPE_TARGET_TYPE (type
);
1262 else if (rust_slice_type_p (type
))
1264 for (int i
= 0; i
< type
->num_fields (); ++i
)
1266 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1268 base_type
= TYPE_TARGET_TYPE (type
->field (i
).type ());
1272 if (base_type
== nullptr)
1273 error (_("Could not find 'data_ptr' in slice type"));
1275 else if (type
->code () == TYPE_CODE_PTR
)
1276 base_type
= TYPE_TARGET_TYPE (type
);
1278 error (_("Cannot subscript non-array type"));
1280 struct type
*new_type
;
1283 if (rust_slice_type_p (type
))
1288 = language_lookup_primitive_type (exp
->language_defn
,
1291 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1295 new_type
= base_type
;
1297 return value_zero (new_type
, VALUE_LVAL (lhs
));
1304 if (type
->code () == TYPE_CODE_ARRAY
)
1307 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1308 error (_("Can't compute array bounds"));
1310 error (_("Found array with non-zero lower bound"));
1313 else if (rust_slice_type_p (type
))
1317 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1318 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1320 high_bound
= value_as_long (len
);
1322 else if (type
->code () == TYPE_CODE_PTR
)
1326 high_bound
= LONGEST_MAX
;
1329 error (_("Cannot subscript non-array type"));
1332 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1335 error (_("Index less than zero"));
1336 if (low
> high_bound
)
1337 error (_("Index greater than length"));
1339 result
= value_subscript (base
, low
);
1346 struct type
*usize
, *slice
;
1348 struct value
*addrval
, *tem
;
1350 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1353 error (_("High index less than zero"));
1355 error (_("Low index greater than high index"));
1356 if (high
> high_bound
)
1357 error (_("High index greater than length"));
1359 usize
= language_lookup_primitive_type (exp
->language_defn
,
1362 const char *new_name
= ((type
!= nullptr
1363 && rust_slice_type_p (type
))
1364 ? type
->name () : "&[*gdb*]");
1366 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1368 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1369 addr
= value_as_long (addrval
);
1370 tem
= value_at_lazy (slice
, addr
);
1372 value_assign (value_field (tem
, 0), value_addr (result
));
1373 value_assign (value_field (tem
, 1),
1374 value_from_longest (usize
, high
- low
));
1376 result
= value_at_lazy (slice
, addr
);
1379 result
= value_addr (result
);
1385 /* evaluate_exp implementation for Rust. */
1387 static struct value
*
1388 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1389 int *pos
, enum noside noside
)
1391 struct value
*result
;
1393 switch (exp
->elts
[*pos
].opcode
)
1397 if (noside
!= EVAL_NORMAL
)
1398 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1402 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1405 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1406 if (trait_ptr
!= NULL
)
1409 result
= value_ind (value
);
1414 case UNOP_COMPLEMENT
:
1416 struct value
*value
;
1419 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1420 if (noside
== EVAL_SKIP
)
1422 /* Preserving the type is enough. */
1425 if (value_type (value
)->code () == TYPE_CODE_BOOL
)
1426 result
= value_from_longest (value_type (value
),
1427 value_logical_not (value
));
1429 result
= value_complement (value
);
1433 case BINOP_SUBSCRIPT
:
1434 result
= rust_subscript (exp
, pos
, noside
, 0);
1438 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1444 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1445 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1448 struct value
*addrval
= NULL
;
1452 if (noside
== EVAL_NORMAL
)
1454 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1455 addr
= value_as_long (addrval
);
1456 result
= value_at_lazy (type
, addr
);
1459 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1464 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1465 if (noside
== EVAL_NORMAL
)
1467 /* This isn't quite right but will do for the time
1468 being, seeing that we can't implement the Copy
1470 value_assign (result
, init
);
1476 gdb_assert (arglen
% 2 == 0);
1477 for (i
= 0; i
< arglen
; i
+= 2)
1480 const char *fieldname
;
1481 struct value
*value
, *field
;
1483 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1485 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1487 fieldname
= &exp
->elts
[*pos
].string
;
1488 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1490 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1491 if (noside
== EVAL_NORMAL
)
1493 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1495 value_assign (field
, value
);
1499 if (noside
== EVAL_SKIP
)
1500 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1502 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1503 result
= allocate_value (type
);
1505 result
= value_at_lazy (type
, addr
);
1514 struct value
*ncopies
;
1516 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1517 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1518 copies
= value_as_long (ncopies
);
1520 error (_("Array with negative number of elements"));
1522 if (noside
== EVAL_NORMAL
)
1525 std::vector
<struct value
*> eltvec (copies
);
1527 for (i
= 0; i
< copies
; ++i
)
1529 result
= value_array (0, copies
- 1, eltvec
.data ());
1533 struct type
*arraytype
1534 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1535 result
= allocate_value (arraytype
);
1540 case STRUCTOP_ANONYMOUS
:
1542 /* Anonymous field access, i.e. foo.1. */
1544 int pc
, field_number
, nfields
;
1548 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1550 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1552 type
= value_type (lhs
);
1554 if (type
->code () == TYPE_CODE_STRUCT
)
1556 struct type
*outer_type
= NULL
;
1558 if (rust_enum_p (type
))
1560 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1561 TYPE_LENGTH (type
));
1562 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1564 if (rust_empty_enum_p (type
))
1565 error (_("Cannot access field %d of empty enum %s"),
1566 field_number
, type
->name ());
1568 int fieldno
= rust_enum_variant (type
);
1569 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1571 type
= value_type (lhs
);
1574 /* Tuples and tuple structs */
1575 nfields
= type
->num_fields ();
1577 if (field_number
>= nfields
|| field_number
< 0)
1579 if (outer_type
!= NULL
)
1580 error(_("Cannot access field %d of variant %s::%s, "
1581 "there are only %d fields"),
1582 field_number
, outer_type
->name (),
1583 rust_last_path_segment (type
->name ()),
1586 error(_("Cannot access field %d of %s, "
1587 "there are only %d fields"),
1588 field_number
, type
->name (), nfields
);
1591 /* Tuples are tuple structs too. */
1592 if (!rust_tuple_struct_type_p (type
))
1594 if (outer_type
!= NULL
)
1595 error(_("Variant %s::%s is not a tuple variant"),
1596 outer_type
->name (),
1597 rust_last_path_segment (type
->name ()));
1599 error(_("Attempting to access anonymous field %d "
1600 "of %s, which is not a tuple, tuple struct, or "
1601 "tuple-like variant"),
1602 field_number
, type
->name ());
1605 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1608 error(_("Anonymous field access is only allowed on tuples, \
1609 tuple structs, and tuple-like enum variants"));
1613 case STRUCTOP_STRUCT
:
1620 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1621 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1622 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1624 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1625 type
= value_type (lhs
);
1626 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1628 gdb::array_view
<const gdb_byte
> view (value_contents (lhs
),
1629 TYPE_LENGTH (type
));
1630 type
= resolve_dynamic_type (type
, view
, value_address (lhs
));
1632 if (rust_empty_enum_p (type
))
1633 error (_("Cannot access field %s of empty enum %s"),
1634 field_name
, type
->name ());
1636 int fieldno
= rust_enum_variant (type
);
1637 lhs
= value_primitive_field (lhs
, 0, fieldno
, type
);
1639 struct type
*outer_type
= type
;
1640 type
= value_type (lhs
);
1641 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1642 error (_("Attempting to access named field %s of tuple "
1643 "variant %s::%s, which has only anonymous fields"),
1644 field_name
, outer_type
->name (),
1645 rust_last_path_segment (type
->name ()));
1649 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1652 catch (const gdb_exception_error
&except
)
1654 error (_("Could not find field %s of struct variant %s::%s"),
1655 field_name
, outer_type
->name (),
1656 rust_last_path_segment (type
->name ()));
1660 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1661 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1662 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1667 result
= rust_range (exp
, pos
, noside
);
1671 /* We might have &array[range], in which case we need to make a
1673 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1676 result
= rust_subscript (exp
, pos
, noside
, 1);
1681 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1688 /* operator_length implementation for Rust. */
1691 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1697 switch (exp
->elts
[pc
- 1].opcode
)
1700 /* We handle aggregate as a type and argument count. The first
1701 argument might be OP_OTHERS. After that the arguments
1702 alternate: first an OP_NAME, then an expression. */
1704 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1712 case STRUCTOP_ANONYMOUS
:
1723 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1731 /* op_name implementation for Rust. */
1734 rust_op_name (enum exp_opcode opcode
)
1739 return "OP_AGGREGATE";
1743 return op_name_standard (opcode
);
1747 /* dump_subexp_body implementation for Rust. */
1750 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1753 switch (exp
->elts
[elt
].opcode
)
1757 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1760 fprintf_filtered (stream
, "Type @");
1761 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1762 fprintf_filtered (stream
, " (");
1763 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1764 fprintf_filtered (stream
, "), length %d", length
);
1767 for (i
= 0; i
< length
; ++i
)
1768 elt
= dump_subexp (exp
, stream
, elt
);
1775 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1777 fprintf_filtered (stream
, "%s: %s",
1778 (exp
->elts
[elt
].opcode
== OP_STRING
1779 ? "string" : "name"),
1780 &exp
->elts
[elt
+ 2].string
);
1781 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1786 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1789 case STRUCTOP_ANONYMOUS
:
1793 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1795 fprintf_filtered (stream
, "Field number: %d", field_number
);
1796 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1805 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1812 /* print_subexp implementation for Rust. */
1815 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1816 enum precedence prec
)
1818 switch (exp
->elts
[*pos
].opcode
)
1822 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1825 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1826 fputs_filtered (" { ", stream
);
1829 for (i
= 0; i
< length
; ++i
)
1831 rust_print_subexp (exp
, pos
, stream
, prec
);
1832 fputs_filtered (", ", stream
);
1834 fputs_filtered (" }", stream
);
1840 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1842 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1843 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1849 fputs_filtered ("<<others>> (", stream
);
1851 rust_print_subexp (exp
, pos
, stream
, prec
);
1852 fputs_filtered (")", stream
);
1856 case STRUCTOP_ANONYMOUS
:
1858 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1861 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1862 fprintf_filtered (stream
, ".%d", tem
);
1868 fprintf_filtered (stream
, "[");
1869 rust_print_subexp (exp
, pos
, stream
, prec
);
1870 fprintf_filtered (stream
, "; ");
1871 rust_print_subexp (exp
, pos
, stream
, prec
);
1872 fprintf_filtered (stream
, "]");
1876 print_subexp_standard (exp
, pos
, stream
, prec
);
1881 /* operator_check implementation for Rust. */
1884 rust_operator_check (struct expression
*exp
, int pos
,
1885 int (*objfile_func
) (struct objfile
*objfile
,
1889 switch (exp
->elts
[pos
].opcode
)
1893 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1894 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1896 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1907 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1915 static const struct exp_descriptor exp_descriptor_rust
=
1918 rust_operator_length
,
1919 rust_operator_check
,
1921 rust_dump_subexp_body
,
1922 rust_evaluate_subexp
1925 static const char *rust_extensions
[] =
1930 /* Constant data representing the Rust language. */
1932 extern const struct language_data rust_language_data
=
1942 &exp_descriptor_rust
,
1943 NULL
, /* name_of_this */
1944 false, /* la_store_sym_names_in_linkage_form_p */
1945 c_op_print_tab
, /* expression operators for printing */
1946 1, /* c-style arrays */
1947 0, /* String lower bound */
1948 &default_varobj_ops
,
1949 rust_is_string_type_p
,
1950 "{...}" /* la_struct_too_deep_ellipsis */
1953 /* Class representing the Rust language. */
1955 class rust_language
: public language_defn
1959 : language_defn (language_rust
, rust_language_data
)
1962 /* See language.h. */
1963 void language_arch_info (struct gdbarch
*gdbarch
,
1964 struct language_arch_info
*lai
) const override
1966 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1969 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1972 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1973 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1974 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1975 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1976 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1977 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1978 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1979 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1980 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1981 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1983 unsigned int length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1984 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1985 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1987 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1988 floatformats_ieee_single
);
1989 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1990 floatformats_ieee_double
);
1992 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1994 struct type
*tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1995 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1996 types
[rust_primitive_usize
]);
1998 lai
->primitive_type_vector
= types
;
1999 lai
->bool_type_default
= types
[rust_primitive_bool
];
2000 lai
->string_char_type
= types
[rust_primitive_u8
];
2003 /* See language.h. */
2004 bool sniff_from_mangled_name (const char *mangled
,
2005 char **demangled
) const override
2007 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2008 return *demangled
!= NULL
;
2011 /* See language.h. */
2013 char *demangle (const char *mangled
, int options
) const override
2015 return gdb_demangle (mangled
, options
);
2018 /* See language.h. */
2020 void print_type (struct type
*type
, const char *varstring
,
2021 struct ui_file
*stream
, int show
, int level
,
2022 const struct type_print_options
*flags
) const override
2024 print_offset_data podata
;
2025 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
2026 flags
, false, &podata
);
2029 /* See language.h. */
2031 gdb::unique_xmalloc_ptr
<char> watch_location_expression
2032 (struct type
*type
, CORE_ADDR addr
) const override
2034 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2035 std::string name
= type_to_string (type
);
2036 return gdb::unique_xmalloc_ptr
<char>
2037 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2041 /* See language.h. */
2043 void value_print_inner
2044 (struct value
*val
, struct ui_file
*stream
, int recurse
,
2045 const struct value_print_options
*options
) const override
2047 return rust_value_print_inner (val
, stream
, recurse
, options
);
2050 /* See language.h. */
2052 struct block_symbol lookup_symbol_nonlocal
2053 (const char *name
, const struct block
*block
,
2054 const domain_enum domain
) const override
2056 struct block_symbol result
= {};
2058 if (symbol_lookup_debug
)
2060 fprintf_unfiltered (gdb_stdlog
,
2061 "rust_lookup_symbol_non_local"
2062 " (%s, %s (scope %s), %s)\n",
2063 name
, host_address_to_string (block
),
2064 block_scope (block
), domain_name (domain
));
2067 /* Look up bare names in the block's scope. */
2068 std::string scopedname
;
2069 if (name
[cp_find_first_component (name
)] == '\0')
2071 const char *scope
= block_scope (block
);
2073 if (scope
[0] != '\0')
2075 scopedname
= std::string (scope
) + "::" + name
;
2076 name
= scopedname
.c_str ();
2084 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2085 if (result
.symbol
== NULL
)
2086 result
= lookup_global_symbol (name
, block
, domain
);
2091 /* See language.h. */
2093 int parser (struct parser_state
*ps
) const override
2095 return rust_parse (ps
);
2098 /* See language.h. */
2100 void emitchar (int ch
, struct type
*chtype
,
2101 struct ui_file
*stream
, int quoter
) const override
2103 if (!rust_chartype_p (chtype
))
2104 generic_emit_char (ch
, chtype
, stream
, quoter
,
2105 target_charset (get_type_arch (chtype
)));
2106 else if (ch
== '\\' || ch
== quoter
)
2107 fprintf_filtered (stream
, "\\%c", ch
);
2108 else if (ch
== '\n')
2109 fputs_filtered ("\\n", stream
);
2110 else if (ch
== '\r')
2111 fputs_filtered ("\\r", stream
);
2112 else if (ch
== '\t')
2113 fputs_filtered ("\\t", stream
);
2114 else if (ch
== '\0')
2115 fputs_filtered ("\\0", stream
);
2116 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
2117 fputc_filtered (ch
, stream
);
2119 fprintf_filtered (stream
, "\\x%02x", ch
);
2121 fprintf_filtered (stream
, "\\u{%06x}", ch
);
2124 /* See language.h. */
2126 void printchar (int ch
, struct type
*chtype
,
2127 struct ui_file
*stream
) const override
2129 fputs_filtered ("'", stream
);
2130 LA_EMIT_CHAR (ch
, chtype
, stream
, '\'');
2131 fputs_filtered ("'", stream
);
2134 /* See language.h. */
2136 void printstr (struct ui_file
*stream
, struct type
*elttype
,
2137 const gdb_byte
*string
, unsigned int length
,
2138 const char *encoding
, int force_ellipses
,
2139 const struct value_print_options
*options
) const override
2141 rust_printstr (stream
, elttype
, string
, length
, encoding
,
2142 force_ellipses
, options
);
2145 /* See language.h. */
2147 void print_typedef (struct type
*type
, struct symbol
*new_symbol
,
2148 struct ui_file
*stream
) const override
2150 type
= check_typedef (type
);
2151 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
2152 type_print (type
, "", stream
, 0);
2153 fprintf_filtered (stream
, ";");
2157 /* Single instance of the Rust language class. */
2159 static rust_language rust_language_defn
;