1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2024 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/>. */
26 #include "cp-support.h"
28 #include "event-top.h"
32 #include "rust-lang.h"
33 #include "typeprint.h"
39 #include "cli/cli-style.h"
40 #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 ();
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 (i
).is_artificial ())
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 (!type
->field (i
).is_static ())
136 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
137 if (strcmp (buf
, type
->field (i
).name ()) != 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 "slice-like"; false otherwise. */
159 rust_slice_type_p (const struct type
*type
)
161 if (type
->code () == TYPE_CODE_STRUCT
162 && type
->name () != NULL
163 && type
->num_fields () == 2)
165 /* The order of fields doesn't matter. While it would be nice
166 to check for artificiality here, the Rust compiler doesn't
167 emit this information. */
168 const char *n1
= type
->field (0).name ();
169 const char *n2
= type
->field (1).name ();
170 return ((streq (n1
, "data_ptr") && streq (n2
, "length"))
171 || (streq (n2
, "data_ptr") && streq (n1
, "length")));
176 /* Return true if TYPE is a range type, otherwise false. */
179 rust_range_type_p (struct type
*type
)
183 if (type
->code () != TYPE_CODE_STRUCT
184 || type
->num_fields () > 2
185 || type
->name () == NULL
186 || strstr (type
->name (), "::Range") == NULL
)
189 if (type
->num_fields () == 0)
193 if (strcmp (type
->field (0).name (), "start") == 0)
195 if (type
->num_fields () == 1)
199 else if (type
->num_fields () == 2)
201 /* First field had to be "start". */
205 return strcmp (type
->field (i
).name (), "end") == 0;
208 /* Return true if TYPE is an inclusive range type, otherwise false.
209 This is only valid for types which are already known to be range
213 rust_inclusive_range_type_p (struct type
*type
)
215 return (strstr (type
->name (), "::RangeInclusive") != NULL
216 || strstr (type
->name (), "::RangeToInclusive") != NULL
);
219 /* Return true if TYPE seems to be the type "u8", otherwise false. */
222 rust_u8_type_p (struct type
*type
)
224 return (type
->code () == TYPE_CODE_INT
225 && type
->is_unsigned ()
226 && type
->length () == 1);
229 /* Return true if TYPE is a Rust character type. */
232 rust_chartype_p (struct type
*type
)
234 return (type
->code () == TYPE_CODE_CHAR
235 && type
->length () == 4
236 && type
->is_unsigned ());
239 /* If VALUE represents a trait object pointer, return the underlying
240 pointer with the correct (i.e., runtime) type. Otherwise, return
243 static struct value
*
244 rust_get_trait_object_pointer (struct value
*value
)
246 struct type
*type
= check_typedef (value
->type ());
248 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
251 /* Try to be a bit resilient if the ABI changes. */
252 int vtable_field
= 0;
253 for (int i
= 0; i
< 2; ++i
)
255 if (strcmp (type
->field (i
).name (), "vtable") == 0)
257 else if (strcmp (type
->field (i
).name (), "pointer") != 0)
261 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
262 struct symbol
*symbol
= find_symbol_at_address (vtable
);
263 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
266 struct rust_vtable_symbol
*vtable_sym
267 = static_cast<struct rust_vtable_symbol
*> (symbol
);
268 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
269 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
272 /* Find and possibly rewrite the unsized part of a slice-like type.
274 This function has two modes. If the out parameters are both NULL,
275 it will return true if an unsized member of IN_TYPE is found.
277 If the out parameters are both non-NULL, it will do the same, but
278 will also rewrite the unsized member's type to be an array of the
279 appropriate type. BOUND is the upper bound of the new array.
281 See convert_slice to understand the different kinds of unsized type
282 and how they are represented.
285 rewrite_slice_type (struct type
*in_type
, struct type
**new_type
,
286 LONGEST bound
, ULONGEST
*additional_length
)
288 if (in_type
->code () != TYPE_CODE_STRUCT
)
291 unsigned nfields
= in_type
->num_fields ();
295 struct type
*rewritten
;
296 const field
&field
= in_type
->field (nfields
- 1);
297 struct type
*field_type
= field
.type ();
298 if (field
.loc_kind () == FIELD_LOC_KIND_BITPOS
299 && field
.loc_bitpos () == 8 * in_type
->length ())
301 if (additional_length
== nullptr)
303 rewritten
= lookup_array_range_type (field_type
, 0, bound
);
304 *additional_length
= rewritten
->length ();
308 if (!rewrite_slice_type (field_type
, &rewritten
, bound
,
311 if (additional_length
== nullptr)
315 struct type
*result
= copy_type (in_type
);
316 result
->copy_fields (in_type
);
317 result
->field (nfields
- 1).set_type (rewritten
);
318 result
->set_length (result
->length () + *additional_length
);
324 /* Convert a Rust slice to its "true" representation.
326 The Rust compiler emits slices as "fat" pointers like:
328 struct { payload *data_ptr; usize length }
330 Any sort of unsized type is emitted this way.
332 If 'payload' is a struct type, then it must be searched to see if
333 the trailing field is unsized. This has to be done recursively (as
334 in, if the final field in the struct type itself has struct type,
335 then that type must be searched). In this scenario, the unsized
336 field can be recognized because it does not contribute to the
339 If 'payload' does not have a trailing unsized type, or if it is not
340 of struct type, then this slice is "array-like". In this case
341 rewriting will return an array.
343 static struct value
*
344 convert_slice (struct value
*val
)
346 struct type
*type
= check_typedef (val
->type ());
347 /* This must have been checked by the caller. */
348 gdb_assert (rust_slice_type_p (type
));
350 struct value
*len
= value_struct_elt (&val
, {}, "length", nullptr,
352 LONGEST llen
= value_as_long (len
);
354 struct value
*ptr
= value_struct_elt (&val
, {}, "data_ptr", nullptr,
356 struct type
*original_type
= ptr
->type ()->target_type ();
357 ULONGEST new_length_storage
= 0;
358 struct type
*new_type
= nullptr;
359 if (!rewrite_slice_type (original_type
, &new_type
, llen
- 1,
360 &new_length_storage
))
361 new_type
= lookup_array_range_type (original_type
, 0, llen
- 1);
363 struct value
*result
= value::allocate_lazy (new_type
);
364 result
->set_lval (lval_memory
);
365 result
->set_address (value_as_address (ptr
));
366 result
->fetch_lazy ();
371 /* If TYPE is an array-like slice, return the element type; otherwise
374 rust_array_like_element_type (struct type
*type
)
376 /* Caller must check this. */
377 gdb_assert (rust_slice_type_p (type
));
378 for (int i
= 0; i
< type
->num_fields (); ++i
)
380 if (strcmp (type
->field (i
).name (), "data_ptr") == 0)
382 struct type
*base_type
= type
->field (i
).type ()->target_type ();
383 if (rewrite_slice_type (base_type
, nullptr, 0, nullptr))
393 /* See language.h. */
396 rust_language::printstr (struct ui_file
*stream
, struct type
*type
,
397 const gdb_byte
*string
, unsigned int length
,
398 const char *user_encoding
, int force_ellipses
,
399 const struct value_print_options
*options
) const
401 /* Rust always uses UTF-8, but let the caller override this if need
403 const char *encoding
= user_encoding
;
404 if (user_encoding
== NULL
|| !*user_encoding
)
406 /* In Rust strings, characters are "u8". */
407 if (rust_u8_type_p (type
))
411 /* This is probably some C string, so let's let C deal with
413 language_defn::printstr (stream
, type
, string
, length
,
414 user_encoding
, force_ellipses
,
420 /* This is not ideal as it doesn't use our character printer. */
421 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
427 static const struct generic_val_print_decorations rust_decorations
=
429 /* Complex isn't used in Rust, but we provide C-ish values just in
441 /* See rust-lang.h. */
444 rust_slice_to_array (struct value
*val
)
446 val
= convert_slice (val
);
447 if (val
->type ()->code () != TYPE_CODE_ARRAY
)
452 /* Helper function to print a slice. */
455 rust_language::val_print_slice
456 (struct value
*val
, struct ui_file
*stream
, int recurse
,
457 const struct value_print_options
*options
) const
459 struct type
*orig_type
= check_typedef (val
->type ());
461 val
= convert_slice (val
);
462 struct type
*type
= check_typedef (val
->type ());
464 /* &str is handled here; but for all other slice types it is fine to
465 simply print the contents. */
466 if (orig_type
->name () != nullptr
467 && strcmp (orig_type
->name (), "&str") == 0)
469 LONGEST low_bound
, high_bound
;
470 if (get_array_bounds (type
, &low_bound
, &high_bound
))
472 val_print_string (type
->target_type (), "UTF-8",
473 val
->address (), high_bound
- low_bound
+ 1,
479 /* Print the slice type here. This was gdb's historical behavior
480 (from before unsized types were generically handled) and helps
481 make it clear that the user is seeing a slice, not an array.
482 Only arrays must be handled as the other cases are handled by
483 value_print_inner. */
484 if (type
->code () == TYPE_CODE_ARRAY
)
486 type_print (orig_type
, "", stream
, -1);
487 gdb_printf (stream
, " ");
490 value_print_inner (val
, stream
, recurse
, options
);
493 /* See rust-lang.h. */
496 rust_language::val_print_struct
497 (struct value
*val
, struct ui_file
*stream
, int recurse
,
498 const struct value_print_options
*options
) const
502 struct type
*type
= check_typedef (val
->type ());
504 if (rust_slice_type_p (type
))
506 val_print_slice (val
, stream
, recurse
, options
);
510 bool is_tuple
= rust_tuple_type_p (type
);
511 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
512 struct value_print_options opts
;
516 if (type
->name () != NULL
)
517 gdb_printf (stream
, "%s", type
->name ());
519 if (type
->num_fields () == 0)
522 if (type
->name () != NULL
)
523 gdb_puts (" ", stream
);
526 if (is_tuple
|| is_tuple_struct
)
527 gdb_puts ("(", stream
);
529 gdb_puts ("{", stream
);
532 opts
.deref_ref
= false;
535 for (i
= 0; i
< type
->num_fields (); ++i
)
537 if (type
->field (i
).is_static ())
541 gdb_puts (",", stream
);
543 if (options
->prettyformat
)
545 gdb_puts ("\n", stream
);
546 print_spaces (2 + 2 * recurse
, stream
);
548 else if (!first_field
)
549 gdb_puts (" ", stream
);
553 if (!is_tuple
&& !is_tuple_struct
)
555 fputs_styled (type
->field (i
).name (),
556 variable_name_style
.style (), stream
);
557 gdb_puts (": ", stream
);
560 common_val_print (value_field (val
, i
), stream
, recurse
+ 1, &opts
,
564 if (options
->prettyformat
)
566 gdb_puts ("\n", stream
);
567 print_spaces (2 * recurse
, stream
);
570 if (is_tuple
|| is_tuple_struct
)
571 gdb_puts (")", stream
);
573 gdb_puts ("}", stream
);
576 /* See rust-lang.h. */
579 rust_language::print_enum (struct value
*val
, struct ui_file
*stream
,
581 const struct value_print_options
*options
) const
583 struct value_print_options opts
= *options
;
584 struct type
*type
= check_typedef (val
->type ());
586 opts
.deref_ref
= false;
588 gdb_assert (rust_enum_p (type
));
589 gdb::array_view
<const gdb_byte
> view
590 (val
->contents_for_printing ().data (),
591 val
->type ()->length ());
592 type
= resolve_dynamic_type (type
, view
, val
->address ());
594 if (rust_empty_enum_p (type
))
596 /* Print the enum type name here to be more clear. */
597 gdb_printf (stream
, _("%s {%p[<No data fields>%p]}"),
599 metadata_style
.style ().ptr (), nullptr);
603 int variant_fieldno
= rust_enum_variant (type
);
604 val
= val
->primitive_field (0, variant_fieldno
, type
);
605 struct type
*variant_type
= type
->field (variant_fieldno
).type ();
607 int nfields
= variant_type
->num_fields ();
609 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
611 gdb_printf (stream
, "%s", variant_type
->name ());
614 /* In case of a nullary variant like 'None', just output
619 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
621 gdb_printf (stream
, "(");
624 /* struct variant. */
625 gdb_printf (stream
, "{");
628 bool first_field
= true;
629 for (int j
= 0; j
< nfields
; j
++)
632 gdb_puts (", ", stream
);
636 gdb_printf (stream
, "%ps: ",
637 styled_string (variable_name_style
.style (),
638 variant_type
->field (j
).name ()));
640 common_val_print (value_field (val
, j
), stream
, recurse
+ 1, &opts
,
645 gdb_puts (")", stream
);
647 gdb_puts ("}", stream
);
650 /* See language.h. */
653 rust_language::value_print_inner
654 (struct value
*val
, struct ui_file
*stream
, int recurse
,
655 const struct value_print_options
*options
) const
657 struct value_print_options opts
= *options
;
658 opts
.deref_ref
= true;
660 if (opts
.prettyformat
== Val_prettyformat_default
)
661 opts
.prettyformat
= (opts
.prettyformat_structs
662 ? Val_prettyformat
: Val_no_prettyformat
);
664 struct type
*type
= check_typedef (val
->type ());
665 switch (type
->code ())
669 LONGEST low_bound
, high_bound
;
671 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
672 && rust_u8_type_p (type
->target_type ()->target_type ())
673 && get_array_bounds (type
->target_type (), &low_bound
,
676 /* We have a pointer to a byte string, so just print
678 struct type
*elttype
= check_typedef (type
->target_type ());
679 CORE_ADDR addr
= value_as_address (val
);
680 struct gdbarch
*arch
= type
->arch ();
682 if (opts
.addressprint
)
684 gdb_puts (paddress (arch
, addr
), stream
);
685 gdb_puts (" ", stream
);
688 gdb_puts ("b", stream
);
689 val_print_string (elttype
->target_type (), "ASCII", addr
,
690 high_bound
- low_bound
+ 1, stream
,
698 /* Recognize the unit type. */
699 if (type
->is_unsigned () && type
->length () == 0
700 && type
->name () != NULL
&& strcmp (type
->name (), "()") == 0)
702 gdb_puts ("()", stream
);
707 case TYPE_CODE_STRING
:
709 LONGEST low_bound
, high_bound
;
711 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
712 error (_("Could not determine the array bounds"));
714 /* If we see a plain TYPE_CODE_STRING, then we're printing a
715 byte string, hence the choice of "ASCII" as the
717 gdb_puts ("b", stream
);
718 printstr (stream
, type
->target_type (),
719 val
->contents_for_printing ().data (),
720 high_bound
- low_bound
+ 1, "ASCII", 0, &opts
);
724 case TYPE_CODE_ARRAY
:
726 LONGEST low_bound
, high_bound
;
728 if (get_array_bounds (type
, &low_bound
, &high_bound
)
729 && high_bound
- low_bound
+ 1 == 0)
730 gdb_puts ("[]", stream
);
736 case TYPE_CODE_UNION
:
737 /* Untagged unions are printed as if they are structs. Since
738 the field bit positions overlap in the debuginfo, the code
739 for printing a union is same as that for a struct, the only
740 difference is that the input type will have overlapping
742 val_print_struct (val
, stream
, recurse
, &opts
);
745 case TYPE_CODE_STRUCT
:
746 if (rust_enum_p (type
))
747 print_enum (val
, stream
, recurse
, &opts
);
749 val_print_struct (val
, stream
, recurse
, &opts
);
754 /* Nothing special yet. */
755 generic_value_print (val
, stream
, recurse
, &opts
, &rust_decorations
);
759 /* See language.h. */
762 rust_language::value_print
763 (struct value
*val
, struct ui_file
*stream
,
764 const struct value_print_options
*options
) const
766 value_print_options opts
= *options
;
767 opts
.deref_ref
= true;
769 struct type
*type
= check_typedef (val
->type ());
770 if (type
->is_pointer_or_reference ())
772 gdb_printf (stream
, "(");
773 type_print (val
->type (), "", stream
, -1);
774 gdb_printf (stream
, ") ");
777 return common_val_print (val
, stream
, 0, &opts
, this);
783 rust_internal_print_type (struct type
*type
, const char *varstring
,
784 struct ui_file
*stream
, int show
, int level
,
785 const struct type_print_options
*flags
,
786 bool for_rust_enum
, print_offset_data
*podata
);
788 /* Print a struct or union typedef. */
790 rust_print_struct_def (struct type
*type
, const char *varstring
,
791 struct ui_file
*stream
, int show
, int level
,
792 const struct type_print_options
*flags
,
793 bool for_rust_enum
, print_offset_data
*podata
)
795 /* Print a tuple type simply. */
796 if (rust_tuple_type_p (type
))
798 gdb_puts (type
->name (), stream
);
802 /* If we see a base class, delegate to C. */
803 if (TYPE_N_BASECLASSES (type
) > 0)
804 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
, flags
);
806 if (flags
->print_offsets
)
808 /* Temporarily bump the level so that the output lines up
813 /* Compute properties of TYPE here because, in the enum case, the
814 rest of the code ends up looking only at the variant part. */
815 const char *tagname
= type
->name ();
816 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
817 bool is_tuple
= rust_tuple_type_p (type
);
818 bool is_enum
= rust_enum_p (type
);
822 /* Already printing an outer enum, so nothing to print here. */
826 /* This code path is also used by unions and enums. */
829 gdb_puts ("enum ", stream
);
830 dynamic_prop
*prop
= type
->dyn_prop (DYN_PROP_VARIANT_PARTS
);
831 if (prop
!= nullptr && prop
->kind () == PROP_TYPE
)
832 type
= prop
->original_type ();
834 else if (type
->code () == TYPE_CODE_STRUCT
)
835 gdb_puts ("struct ", stream
);
837 gdb_puts ("union ", stream
);
840 gdb_puts (tagname
, stream
);
843 if (type
->num_fields () == 0 && !is_tuple
)
845 if (for_rust_enum
&& !flags
->print_offsets
)
846 gdb_puts (is_tuple_struct
? "(" : "{", stream
);
848 gdb_puts (is_tuple_struct
? " (\n" : " {\n", stream
);
850 /* When printing offsets, we rearrange the fields into storage
851 order. This lets us show holes more clearly. We work using
852 field indices here because it simplifies calls to
853 print_offset_data::update below. */
854 std::vector
<int> fields
;
855 for (int i
= 0; i
< type
->num_fields (); ++i
)
857 if (type
->field (i
).is_static ())
859 if (is_enum
&& type
->field (i
).is_artificial ())
861 fields
.push_back (i
);
863 if (flags
->print_offsets
)
864 std::sort (fields
.begin (), fields
.end (),
867 return (type
->field (a
).loc_bitpos ()
868 < type
->field (b
).loc_bitpos ());
875 gdb_assert (!type
->field (i
).is_static ());
876 gdb_assert (! (is_enum
&& type
->field (i
).is_artificial ()));
878 if (flags
->print_offsets
)
879 podata
->update (type
, i
, stream
);
881 /* We'd like to print "pub" here as needed, but rustc
882 doesn't emit the debuginfo, and our types don't have
883 cplus_struct_type attached. */
885 /* For a tuple struct we print the type but nothing
887 if (!for_rust_enum
|| flags
->print_offsets
)
888 print_spaces (level
+ 2, stream
);
890 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
892 else if (!is_tuple_struct
)
893 gdb_printf (stream
, "%ps: ",
894 styled_string (variable_name_style
.style (),
895 type
->field (i
).name ()));
897 rust_internal_print_type (type
->field (i
).type (), NULL
,
898 stream
, (is_enum
? show
: show
- 1),
899 level
+ 2, flags
, is_enum
, podata
);
900 if (!for_rust_enum
|| flags
->print_offsets
)
901 gdb_puts (",\n", stream
);
902 /* Note that this check of "I" is ok because we only sorted the
903 fields by offset when print_offsets was set, so we won't take
904 this branch in that case. */
905 else if (i
+ 1 < type
->num_fields ())
906 gdb_puts (", ", stream
);
909 if (flags
->print_offsets
)
911 /* Undo the temporary level increase we did above. */
913 podata
->finish (type
, level
, stream
);
914 print_spaces (print_offset_data::indentation
, stream
);
916 print_spaces (2, stream
);
918 if (!for_rust_enum
|| flags
->print_offsets
)
919 print_spaces (level
, stream
);
920 gdb_puts (is_tuple_struct
? ")" : "}", stream
);
923 /* la_print_type implementation for Rust. */
926 rust_internal_print_type (struct type
*type
, const char *varstring
,
927 struct ui_file
*stream
, int show
, int level
,
928 const struct type_print_options
*flags
,
929 bool for_rust_enum
, print_offset_data
*podata
)
933 && type
->name () != NULL
)
935 /* Rust calls the unit type "void" in its debuginfo,
936 but we don't want to print it as that. */
937 if (type
->code () == TYPE_CODE_VOID
)
938 gdb_puts ("()", stream
);
940 gdb_puts (type
->name (), stream
);
944 type
= check_typedef (type
);
945 switch (type
->code ())
948 /* If we have an enum, we've already printed the type's
949 unqualified name, and there is nothing else to print
952 gdb_puts ("()", stream
);
956 /* Delegate varargs to the C printer. */
957 if (type
->has_varargs ())
960 gdb_puts ("fn ", stream
);
961 if (varstring
!= NULL
)
962 gdb_puts (varstring
, stream
);
963 gdb_puts ("(", stream
);
964 for (int i
= 0; i
< type
->num_fields (); ++i
)
968 gdb_puts (", ", stream
);
969 rust_internal_print_type (type
->field (i
).type (), "", stream
,
970 -1, 0, flags
, false, podata
);
972 gdb_puts (")", stream
);
973 /* If it returns unit, we can omit the return type. */
974 if (type
->target_type ()->code () != TYPE_CODE_VOID
)
976 gdb_puts (" -> ", stream
);
977 rust_internal_print_type (type
->target_type (), "", stream
,
978 -1, 0, flags
, false, podata
);
982 case TYPE_CODE_ARRAY
:
984 LONGEST low_bound
, high_bound
;
986 gdb_puts ("[", stream
);
987 rust_internal_print_type (type
->target_type (), NULL
,
988 stream
, show
- 1, level
, flags
, false,
991 if (type
->bounds ()->high
.kind () == PROP_LOCEXPR
992 || type
->bounds ()->high
.kind () == PROP_LOCLIST
)
993 gdb_printf (stream
, "; variable length");
994 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
995 gdb_printf (stream
, "; %s",
996 plongest (high_bound
- low_bound
+ 1));
997 gdb_puts ("]", stream
);
1001 case TYPE_CODE_UNION
:
1002 case TYPE_CODE_STRUCT
:
1003 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
1004 for_rust_enum
, podata
);
1007 case TYPE_CODE_ENUM
:
1011 gdb_puts ("enum ", stream
);
1012 if (type
->name () != NULL
)
1014 gdb_puts (type
->name (), stream
);
1015 gdb_puts (" ", stream
);
1016 len
= strlen (type
->name ());
1018 gdb_puts ("{\n", stream
);
1020 for (int i
= 0; i
< type
->num_fields (); ++i
)
1022 const char *name
= type
->field (i
).name ();
1027 && strncmp (name
, type
->name (), len
) == 0
1029 && name
[len
+ 1] == ':')
1031 gdb_printf (stream
, "%*s%ps,\n",
1033 styled_string (variable_name_style
.style (),
1037 gdb_puts ("}", stream
);
1043 if (type
->name () != nullptr)
1044 gdb_puts (type
->name (), stream
);
1047 /* We currently can't distinguish between pointers and
1049 gdb_puts ("*mut ", stream
);
1050 type_print (type
->target_type (), "", stream
, 0);
1057 c_print_type (type
, varstring
, stream
, show
, level
, language_rust
,
1064 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1065 -- either on an obstack or on a gdbarch. */
1067 static struct type
*
1068 rust_composite_type (struct type
*original
,
1070 const char *field1
, struct type
*type1
,
1071 const char *field2
, struct type
*type2
)
1073 struct type
*result
= type_allocator (original
).new_type ();
1074 int i
, nfields
, bitpos
;
1082 result
->set_code (TYPE_CODE_STRUCT
);
1083 result
->set_name (name
);
1085 result
->alloc_fields (nfields
);
1091 struct field
*field
= &result
->field (i
);
1093 field
->set_loc_bitpos (bitpos
);
1094 bitpos
+= type1
->length () * TARGET_CHAR_BIT
;
1096 field
->set_name (field1
);
1097 field
->set_type (type1
);
1102 struct field
*field
= &result
->field (i
);
1103 unsigned align
= type_align (type2
);
1109 align
*= TARGET_CHAR_BIT
;
1110 delta
= bitpos
% align
;
1112 bitpos
+= align
- delta
;
1114 field
->set_loc_bitpos (bitpos
);
1116 field
->set_name (field2
);
1117 field
->set_type (type2
);
1122 result
->set_length (result
->field (i
- 1).loc_bitpos () / TARGET_CHAR_BIT
1123 + result
->field (i
- 1).type ()->length ());
1127 /* See rust-lang.h. */
1130 rust_slice_type (const char *name
, struct type
*elt_type
,
1131 struct type
*usize_type
)
1135 elt_type
= lookup_pointer_type (elt_type
);
1136 type
= rust_composite_type (elt_type
, name
,
1137 "data_ptr", elt_type
,
1138 "length", usize_type
);
1145 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1148 rust_range (struct type
*expect_type
, struct expression
*exp
,
1149 enum noside noside
, enum range_flag kind
,
1150 struct value
*low
, struct value
*high
)
1152 struct value
*addrval
, *result
;
1154 struct type
*range_type
;
1155 struct type
*index_type
;
1156 struct type
*temp_type
;
1159 bool inclusive
= !(kind
& RANGE_HIGH_BOUND_EXCLUSIVE
);
1166 name
= "std::ops::RangeFull";
1170 index_type
= high
->type ();
1172 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1179 index_type
= low
->type ();
1180 name
= "std::ops::RangeFrom";
1184 if (!types_equal (low
->type (), high
->type ()))
1185 error (_("Range expression with different types"));
1186 index_type
= low
->type ();
1187 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1191 /* If we don't have an index type, just allocate this on the
1192 arch. Here any type will do. */
1193 temp_type
= (index_type
== NULL
1194 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1196 /* It would be nicer to cache the range type. */
1197 range_type
= rust_composite_type (temp_type
, name
,
1198 low
== NULL
? NULL
: "start", index_type
,
1199 high
== NULL
? NULL
: "end", index_type
);
1201 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1202 return value::zero (range_type
, lval_memory
);
1204 addrval
= value_allocate_space_in_inferior (range_type
->length ());
1205 addr
= value_as_long (addrval
);
1206 result
= value_at_lazy (range_type
, addr
);
1210 struct value
*start
= value_struct_elt (&result
, {}, "start", NULL
,
1213 value_assign (start
, low
);
1218 struct value
*end
= value_struct_elt (&result
, {}, "end", NULL
,
1221 value_assign (end
, high
);
1224 result
= value_at_lazy (range_type
, addr
);
1228 /* A helper function to compute the range and kind given a range
1229 value. TYPE is the type of the range value. RANGE is the range
1230 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1231 parameters might be filled in, or might not be, depending on the
1232 kind of range this is. KIND will always be set to the appropriate
1233 value describing the kind of range, and this can be used to
1234 determine whether LOW or HIGH are valid. */
1237 rust_compute_range (struct type
*type
, struct value
*range
,
1238 LONGEST
*low
, LONGEST
*high
,
1245 *kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1247 if (type
->num_fields () == 0)
1251 if (strcmp (type
->field (0).name (), "start") == 0)
1253 *kind
= RANGE_HIGH_BOUND_DEFAULT
;
1254 *low
= value_as_long (value_field (range
, 0));
1257 if (type
->num_fields () > i
1258 && strcmp (type
->field (i
).name (), "end") == 0)
1260 *kind
= (*kind
== (RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
)
1261 ? RANGE_LOW_BOUND_DEFAULT
: RANGE_STANDARD
);
1262 *high
= value_as_long (value_field (range
, i
));
1264 if (rust_inclusive_range_type_p (type
))
1269 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1272 rust_subscript (struct type
*expect_type
, struct expression
*exp
,
1273 enum noside noside
, bool for_addr
,
1274 struct value
*lhs
, struct value
*rhs
)
1276 struct value
*result
;
1277 struct type
*rhstype
;
1278 LONGEST low
, high_bound
;
1279 /* Initialized to appease the compiler. */
1280 range_flags kind
= RANGE_LOW_BOUND_DEFAULT
| RANGE_HIGH_BOUND_DEFAULT
;
1284 rhstype
= check_typedef (rhs
->type ());
1285 if (rust_range_type_p (rhstype
))
1288 error (_("Can't take slice of array without '&'"));
1289 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1293 low
= value_as_long (rhs
);
1295 struct type
*type
= check_typedef (lhs
->type ());
1296 struct type
*orig_type
= type
;
1297 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1299 struct type
*base_type
= nullptr;
1300 if (type
->code () == TYPE_CODE_ARRAY
)
1301 base_type
= type
->target_type ();
1302 else if (rust_slice_type_p (type
))
1304 base_type
= rust_array_like_element_type (type
);
1305 if (base_type
== nullptr)
1306 error (_("Cannot subscript non-array-like slice"));
1308 else if (type
->code () == TYPE_CODE_PTR
)
1309 base_type
= type
->target_type ();
1311 error (_("Cannot subscript non-array type"));
1313 struct type
*new_type
;
1316 if (rust_slice_type_p (type
))
1321 = language_lookup_primitive_type (exp
->language_defn
,
1324 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1328 new_type
= base_type
;
1330 return value::zero (new_type
, lhs
->lval ());
1337 if (rust_slice_type_p (type
))
1339 lhs
= convert_slice (lhs
);
1340 type
= check_typedef (lhs
->type ());
1343 if (type
->code () == TYPE_CODE_ARRAY
)
1346 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1347 error (_("Can't compute array bounds"));
1349 error (_("Found array with non-zero lower bound"));
1352 else if (type
->code () == TYPE_CODE_PTR
)
1356 high_bound
= LONGEST_MAX
;
1359 error (_("Cannot subscript non-array type"));
1361 if (want_slice
&& (kind
& RANGE_LOW_BOUND_DEFAULT
))
1364 error (_("Index less than zero"));
1365 if (low
> high_bound
)
1366 error (_("Index greater than length"));
1368 result
= value_subscript (base
, low
);
1375 struct type
*usize
, *slice
;
1377 struct value
*addrval
, *tem
;
1379 if (kind
& RANGE_HIGH_BOUND_DEFAULT
)
1382 error (_("High index less than zero"));
1384 error (_("Low index greater than high index"));
1385 if (high
> high_bound
)
1386 error (_("High index greater than length"));
1388 usize
= language_lookup_primitive_type (exp
->language_defn
,
1391 /* Preserve the name for slice-of-slice; this lets
1392 string-printing work a bit more nicely. */
1393 const char *new_name
= ((orig_type
!= nullptr
1394 && rust_slice_type_p (orig_type
))
1395 ? orig_type
->name () : "&[*gdb*]");
1397 slice
= rust_slice_type (new_name
, result
->type (), usize
);
1399 addrval
= value_allocate_space_in_inferior (slice
->length ());
1400 addr
= value_as_long (addrval
);
1401 tem
= value_at_lazy (slice
, addr
);
1403 value_assign (value_field (tem
, 0), value_addr (result
));
1404 value_assign (value_field (tem
, 1),
1405 value_from_longest (usize
, high
- low
));
1407 result
= value_at_lazy (slice
, addr
);
1410 result
= value_addr (result
);
1420 rust_unop_ind_operation::evaluate (struct type
*expect_type
,
1421 struct expression
*exp
,
1424 if (noside
!= EVAL_NORMAL
)
1425 return unop_ind_operation::evaluate (expect_type
, exp
, noside
);
1427 struct value
*value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1429 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1430 if (trait_ptr
!= NULL
)
1433 return value_ind (value
);
1436 } /* namespace expr */
1438 /* A helper function for UNOP_COMPLEMENT. */
1441 eval_op_rust_complement (struct type
*expect_type
, struct expression
*exp
,
1443 enum exp_opcode opcode
,
1444 struct value
*value
)
1446 if (value
->type ()->code () == TYPE_CODE_BOOL
)
1447 return value_from_longest (value
->type (), value_logical_not (value
));
1448 return value_complement (value
);
1451 /* A helper function for OP_ARRAY. */
1454 eval_op_rust_array (struct type
*expect_type
, struct expression
*exp
,
1456 enum exp_opcode opcode
,
1457 struct value
*elt
, struct value
*ncopies
)
1459 int copies
= value_as_long (ncopies
);
1461 error (_("Array with negative number of elements"));
1463 if (noside
== EVAL_NORMAL
)
1464 return value_array (0, std::vector
<value
*> (copies
, elt
));
1467 struct type
*arraytype
1468 = lookup_array_range_type (elt
->type (), 0, copies
- 1);
1469 return value::allocate (arraytype
);
1477 rust_struct_anon::evaluate (struct type
*expect_type
,
1478 struct expression
*exp
,
1481 value
*lhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
1482 int field_number
= std::get
<0> (m_storage
);
1484 struct type
*type
= lhs
->type ();
1486 if (type
->code () == TYPE_CODE_STRUCT
)
1488 struct type
*outer_type
= NULL
;
1490 if (rust_enum_p (type
))
1492 type
= resolve_dynamic_type (type
, lhs
->contents (),
1495 if (rust_empty_enum_p (type
))
1496 error (_("Cannot access field %d of empty enum %s"),
1497 field_number
, type
->name ());
1499 int fieldno
= rust_enum_variant (type
);
1500 lhs
= lhs
->primitive_field (0, fieldno
, type
);
1502 type
= lhs
->type ();
1505 /* Tuples and tuple structs */
1506 int nfields
= type
->num_fields ();
1508 if (field_number
>= nfields
|| field_number
< 0)
1510 if (outer_type
!= NULL
)
1511 error(_("Cannot access field %d of variant %s::%s, "
1512 "there are only %d fields"),
1513 field_number
, outer_type
->name (),
1514 rust_last_path_segment (type
->name ()),
1517 error(_("Cannot access field %d of %s, "
1518 "there are only %d fields"),
1519 field_number
, type
->name (), nfields
);
1522 /* Tuples are tuple structs too. */
1523 if (!rust_tuple_struct_type_p (type
))
1525 if (outer_type
!= NULL
)
1526 error(_("Variant %s::%s is not a tuple variant"),
1527 outer_type
->name (),
1528 rust_last_path_segment (type
->name ()));
1530 error(_("Attempting to access anonymous field %d "
1531 "of %s, which is not a tuple, tuple struct, or "
1532 "tuple-like variant"),
1533 field_number
, type
->name ());
1536 return lhs
->primitive_field (0, field_number
, type
);
1539 error(_("Anonymous field access is only allowed on tuples, \
1540 tuple structs, and tuple-like enum variants"));
1544 rust_structop::evaluate (struct type
*expect_type
,
1545 struct expression
*exp
,
1548 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1549 const char *field_name
= std::get
<1> (m_storage
).c_str ();
1551 struct value
*result
;
1552 struct type
*type
= lhs
->type ();
1553 if (type
->code () == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1555 type
= resolve_dynamic_type (type
, lhs
->contents (),
1558 if (rust_empty_enum_p (type
))
1559 error (_("Cannot access field %s of empty enum %s"),
1560 field_name
, type
->name ());
1562 int fieldno
= rust_enum_variant (type
);
1563 lhs
= lhs
->primitive_field (0, fieldno
, type
);
1565 struct type
*outer_type
= type
;
1566 type
= lhs
->type ();
1567 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1568 error (_("Attempting to access named field %s of tuple "
1569 "variant %s::%s, which has only anonymous fields"),
1570 field_name
, outer_type
->name (),
1571 rust_last_path_segment (type
->name ()));
1575 result
= value_struct_elt (&lhs
, {}, field_name
,
1578 catch (const gdb_exception_error
&except
)
1580 error (_("Could not find field %s of struct variant %s::%s"),
1581 field_name
, outer_type
->name (),
1582 rust_last_path_segment (type
->name ()));
1587 if (rust_slice_type_p (type
))
1588 lhs
= convert_slice (lhs
);
1589 result
= value_struct_elt (&lhs
, {}, field_name
, NULL
, "structure");
1591 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1592 result
= value::zero (result
->type (), result
->lval ());
1597 rust_aggregate_operation::evaluate (struct type
*expect_type
,
1598 struct expression
*exp
,
1601 struct type
*type
= std::get
<0> (m_storage
);
1603 struct value
*addrval
= NULL
;
1606 if (noside
== EVAL_NORMAL
)
1608 addrval
= value_allocate_space_in_inferior (type
->length ());
1609 addr
= value_as_long (addrval
);
1610 result
= value_at_lazy (type
, addr
);
1613 if (std::get
<1> (m_storage
) != nullptr)
1615 struct value
*init
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
1618 if (noside
== EVAL_NORMAL
)
1620 /* This isn't quite right but will do for the time
1621 being, seeing that we can't implement the Copy
1623 value_assign (result
, init
);
1627 for (const auto &item
: std::get
<2> (m_storage
))
1629 value
*val
= item
.second
->evaluate (nullptr, exp
, noside
);
1630 if (noside
== EVAL_NORMAL
)
1632 const char *fieldname
= item
.first
.c_str ();
1633 value
*field
= value_struct_elt (&result
, {}, fieldname
,
1634 nullptr, "structure");
1635 value_assign (field
, val
);
1639 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1640 result
= value::allocate (type
);
1642 result
= value_at_lazy (type
, addr
);
1648 rust_structop::evaluate_funcall (struct type
*expect_type
,
1649 struct expression
*exp
,
1651 const std::vector
<operation_up
> &ops
)
1653 std::vector
<struct value
*> args (ops
.size () + 1);
1655 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1656 type in order to look up the method. */
1657 args
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1658 /* We don't yet implement real Deref semantics. */
1659 while (args
[0]->type ()->code () == TYPE_CODE_PTR
)
1660 args
[0] = value_ind (args
[0]);
1662 struct type
*type
= args
[0]->type ();
1663 if ((type
->code () != TYPE_CODE_STRUCT
1664 && type
->code () != TYPE_CODE_UNION
1665 && type
->code () != TYPE_CODE_ENUM
)
1666 || rust_tuple_type_p (type
))
1667 error (_("Method calls only supported on struct or enum types"));
1668 if (type
->name () == NULL
)
1669 error (_("Method call on nameless type"));
1671 std::string name
= (std::string (type
->name ()) + "::"
1672 + std::get
<1> (m_storage
));
1674 const struct block
*block
= get_selected_block (0);
1675 struct block_symbol sym
= lookup_symbol (name
.c_str (), block
,
1676 SEARCH_FUNCTION_DOMAIN
,
1678 if (sym
.symbol
== NULL
)
1679 error (_("Could not find function named '%s'"), name
.c_str ());
1681 struct type
*fn_type
= sym
.symbol
->type ();
1682 if (fn_type
->num_fields () == 0)
1683 error (_("Function '%s' takes no arguments"), name
.c_str ());
1685 if (fn_type
->field (0).type ()->code () == TYPE_CODE_PTR
)
1686 args
[0] = value_addr (args
[0]);
1688 value
*function
= address_of_variable (sym
.symbol
, block
);
1690 for (int i
= 0; i
< ops
.size (); ++i
)
1691 args
[i
+ 1] = ops
[i
]->evaluate (nullptr, exp
, noside
);
1693 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1694 return value::zero (fn_type
->target_type (), not_lval
);
1695 return call_function_by_hand (function
, NULL
, args
);
1702 /* See language.h. */
1705 rust_language::language_arch_info (struct gdbarch
*gdbarch
,
1706 struct language_arch_info
*lai
) const
1708 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1710 /* Helper function to allow shorter lines below. */
1711 auto add
= [&] (struct type
* t
) -> struct type
*
1713 lai
->add_primitive_type (t
);
1717 type_allocator
alloc (gdbarch
);
1718 struct type
*bool_type
1719 = add (init_boolean_type (alloc
, 8, 1, "bool"));
1720 add (init_character_type (alloc
, 32, 1, "char"));
1721 add (init_integer_type (alloc
, 8, 0, "i8"));
1722 struct type
*u8_type
1723 = add (init_integer_type (alloc
, 8, 1, "u8"));
1724 add (init_integer_type (alloc
, 16, 0, "i16"));
1725 add (init_integer_type (alloc
, 16, 1, "u16"));
1726 add (init_integer_type (alloc
, 32, 0, "i32"));
1727 add (init_integer_type (alloc
, 32, 1, "u32"));
1728 add (init_integer_type (alloc
, 64, 0, "i64"));
1729 add (init_integer_type (alloc
, 64, 1, "u64"));
1730 add (init_integer_type (alloc
, 128, 0, "i128"));
1731 add (init_integer_type (alloc
, 128, 1, "u128"));
1733 unsigned int length
= 8 * builtin
->builtin_data_ptr
->length ();
1734 add (init_integer_type (alloc
, length
, 0, "isize"));
1735 struct type
*usize_type
1736 = add (init_integer_type (alloc
, length
, 1, "usize"));
1738 add (init_float_type (alloc
, 32, "f32", floatformats_ieee_single
));
1739 add (init_float_type (alloc
, 64, "f64", floatformats_ieee_double
));
1740 add (init_integer_type (alloc
, 0, 1, "()"));
1742 struct type
*tem
= make_cv_type (1, 0, u8_type
, NULL
);
1743 add (rust_slice_type ("&str", tem
, usize_type
));
1745 lai
->set_bool_type (bool_type
);
1746 lai
->set_string_char_type (u8_type
);
1749 /* See language.h. */
1752 rust_language::print_type (struct type
*type
, const char *varstring
,
1753 struct ui_file
*stream
, int show
, int level
,
1754 const struct type_print_options
*flags
) const
1756 print_offset_data
podata (flags
);
1757 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
1758 flags
, false, &podata
);
1761 /* See language.h. */
1764 rust_language::emitchar (int ch
, struct type
*chtype
,
1765 struct ui_file
*stream
, int quoter
) const
1767 if (!rust_chartype_p (chtype
))
1768 generic_emit_char (ch
, chtype
, stream
, quoter
,
1769 target_charset (chtype
->arch ()));
1770 else if (ch
== '\\' || ch
== quoter
)
1771 gdb_printf (stream
, "\\%c", ch
);
1772 else if (ch
== '\n')
1773 gdb_puts ("\\n", stream
);
1774 else if (ch
== '\r')
1775 gdb_puts ("\\r", stream
);
1776 else if (ch
== '\t')
1777 gdb_puts ("\\t", stream
);
1778 else if (ch
== '\0')
1779 gdb_puts ("\\0", stream
);
1780 else if (ch
>= 32 && ch
<= 127 && isprint (ch
))
1781 gdb_putc (ch
, stream
);
1783 gdb_printf (stream
, "\\x%02x", ch
);
1785 gdb_printf (stream
, "\\u{%06x}", ch
);
1788 /* See language.h. */
1791 rust_language::is_array_like (struct type
*type
) const
1793 if (!rust_slice_type_p (type
))
1795 return rust_array_like_element_type (type
) != nullptr;
1798 /* See language.h. */
1801 rust_language::is_string_type_p (struct type
*type
) const
1803 LONGEST low_bound
, high_bound
;
1805 type
= check_typedef (type
);
1806 return ((type
->code () == TYPE_CODE_STRING
)
1807 || (type
->code () == TYPE_CODE_PTR
1808 && (type
->target_type ()->code () == TYPE_CODE_ARRAY
1809 && rust_u8_type_p (type
->target_type ()->target_type ())
1810 && get_array_bounds (type
->target_type (), &low_bound
,
1812 || (type
->code () == TYPE_CODE_STRUCT
1813 && !rust_enum_p (type
)
1814 && rust_slice_type_p (type
)
1815 && strcmp (type
->name (), "&str") == 0));
1818 /* See language.h. */
1821 rust_language::lookup_symbol_nonlocal
1822 (const char *name
, const struct block
*block
,
1823 const domain_search_flags domain
) const
1825 struct block_symbol result
= {};
1827 const char *scope
= block
== nullptr ? "" : block
->scope ();
1828 symbol_lookup_debug_printf
1829 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1830 name
, host_address_to_string (block
), scope
,
1831 domain_name (domain
).c_str ());
1833 /* Look up bare names in the block's scope. */
1834 std::string scopedname
;
1835 if (name
[cp_find_first_component (name
)] == '\0')
1837 if (scope
[0] != '\0')
1839 scopedname
= std::string (scope
) + "::" + name
;
1840 name
= scopedname
.c_str ();
1848 result
= lookup_symbol_in_static_block (name
, block
, domain
);
1849 if (result
.symbol
== NULL
)
1850 result
= lookup_global_symbol (name
, block
, domain
);
1855 /* Single instance of the Rust language class. */
1857 static rust_language rust_language_defn
;