1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cp-support.h"
33 #include "rust-lang.h"
34 #include "typeprint.h"
40 #include "cli/cli-style.h"
42 /* See rust-lang.h. */
45 rust_last_path_segment (const char *path
)
47 const char *result
= strrchr (path
, ':');
54 /* See rust-lang.h. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
62 return std::string ();
64 return std::string (scope
, cp_find_first_component (scope
));
67 /* Return true if TYPE, which must be a struct type, represents a Rust
71 rust_enum_p (const struct type
*type
)
73 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
74 && TYPE_NFIELDS (type
) == 1
75 && TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type
, 0)));
78 /* Return true if TYPE, which must be an enum type, has no
82 rust_empty_enum_p (const struct type
*type
)
84 gdb_assert (rust_enum_p (type
));
85 /* In Rust the enum always fills the containing structure. */
86 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
88 return TYPE_NFIELDS (TYPE_FIELD_TYPE (type
, 0)) == 0;
91 /* Given an enum type and contents, find which variant is active. */
94 rust_enum_variant (struct type
*type
, const gdb_byte
*contents
)
96 /* In Rust the enum always fills the containing structure. */
97 gdb_assert (TYPE_FIELD_BITPOS (type
, 0) == 0);
99 struct type
*union_type
= TYPE_FIELD_TYPE (type
, 0);
101 int fieldno
= value_union_variant (union_type
, contents
);
102 return &TYPE_FIELD (union_type
, fieldno
);
105 /* See rust-lang.h. */
108 rust_tuple_type_p (struct type
*type
)
110 /* The current implementation is a bit of a hack, but there's
111 nothing else in the debuginfo to distinguish a tuple from a
113 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
114 && TYPE_NAME (type
) != NULL
115 && TYPE_NAME (type
)[0] == '(');
118 /* Return true if all non-static fields of a structlike type are in a
119 sequence like __0, __1, __2. */
122 rust_underscore_fields (struct type
*type
)
128 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
130 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
132 if (!field_is_static (&TYPE_FIELD (type
, i
)))
136 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
137 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
145 /* See rust-lang.h. */
148 rust_tuple_struct_type_p (struct type
*type
)
150 /* This is just an approximation until DWARF can represent Rust more
151 precisely. We exclude zero-length structs because they may not
152 be tuple structs, and there's no way to tell. */
153 return TYPE_NFIELDS (type
) > 0 && rust_underscore_fields (type
);
156 /* Return true if TYPE is a slice type, otherwise false. */
159 rust_slice_type_p (struct type
*type
)
161 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
162 && TYPE_NAME (type
) != NULL
163 && (strncmp (TYPE_NAME (type
), "&[", 2) == 0
164 || strcmp (TYPE_NAME (type
), "&str") == 0));
167 /* Return true if TYPE is a range type, otherwise false. */
170 rust_range_type_p (struct type
*type
)
174 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
175 || TYPE_NFIELDS (type
) > 2
176 || TYPE_NAME (type
) == NULL
177 || strstr (TYPE_NAME (type
), "::Range") == NULL
)
180 if (TYPE_NFIELDS (type
) == 0)
184 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
186 if (TYPE_NFIELDS (type
) == 1)
190 else if (TYPE_NFIELDS (type
) == 2)
192 /* First field had to be "start". */
196 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
199 /* Return true if TYPE is an inclusive range type, otherwise false.
200 This is only valid for types which are already known to be range
204 rust_inclusive_range_type_p (struct type
*type
)
206 return (strstr (TYPE_NAME (type
), "::RangeInclusive") != NULL
207 || strstr (TYPE_NAME (type
), "::RangeToInclusive") != NULL
);
210 /* Return true if TYPE seems to be the type "u8", otherwise false. */
213 rust_u8_type_p (struct type
*type
)
215 return (TYPE_CODE (type
) == TYPE_CODE_INT
216 && TYPE_UNSIGNED (type
)
217 && TYPE_LENGTH (type
) == 1);
220 /* Return true if TYPE is a Rust character type. */
223 rust_chartype_p (struct type
*type
)
225 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
226 && TYPE_LENGTH (type
) == 4
227 && TYPE_UNSIGNED (type
));
230 /* Return true if TYPE is a string type. */
233 rust_is_string_type_p (struct type
*type
)
235 LONGEST low_bound
, high_bound
;
237 type
= check_typedef (type
);
238 return ((TYPE_CODE (type
) == TYPE_CODE_STRING
)
239 || (TYPE_CODE (type
) == TYPE_CODE_PTR
240 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
241 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
242 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
244 || (TYPE_CODE (type
) == TYPE_CODE_STRUCT
245 && !rust_enum_p (type
)
246 && rust_slice_type_p (type
)
247 && strcmp (TYPE_NAME (type
), "&str") == 0));
250 /* If VALUE represents a trait object pointer, return the underlying
251 pointer with the correct (i.e., runtime) type. Otherwise, return
254 static struct value
*
255 rust_get_trait_object_pointer (struct value
*value
)
257 struct type
*type
= check_typedef (value_type (value
));
259 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
|| TYPE_NFIELDS (type
) != 2)
262 /* Try to be a bit resilient if the ABI changes. */
263 int vtable_field
= 0;
264 for (int i
= 0; i
< 2; ++i
)
266 if (strcmp (TYPE_FIELD_NAME (type
, i
), "vtable") == 0)
268 else if (strcmp (TYPE_FIELD_NAME (type
, i
), "pointer") != 0)
272 CORE_ADDR vtable
= value_as_address (value_field (value
, vtable_field
));
273 struct symbol
*symbol
= find_symbol_at_address (vtable
);
274 if (symbol
== NULL
|| symbol
->subclass
!= SYMBOL_RUST_VTABLE
)
277 struct rust_vtable_symbol
*vtable_sym
278 = static_cast<struct rust_vtable_symbol
*> (symbol
);
279 struct type
*pointer_type
= lookup_pointer_type (vtable_sym
->concrete_type
);
280 return value_cast (pointer_type
, value_field (value
, 1 - vtable_field
));
285 /* la_emitchar implementation for Rust. */
288 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
290 if (!rust_chartype_p (type
))
291 generic_emit_char (c
, type
, stream
, quoter
,
292 target_charset (get_type_arch (type
)));
293 else if (c
== '\\' || c
== quoter
)
294 fprintf_filtered (stream
, "\\%c", c
);
296 fputs_filtered ("\\n", stream
);
298 fputs_filtered ("\\r", stream
);
300 fputs_filtered ("\\t", stream
);
302 fputs_filtered ("\\0", stream
);
303 else if (c
>= 32 && c
<= 127 && isprint (c
))
304 fputc_filtered (c
, stream
);
306 fprintf_filtered (stream
, "\\x%02x", c
);
308 fprintf_filtered (stream
, "\\u{%06x}", c
);
311 /* la_printchar implementation for Rust. */
314 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
316 fputs_filtered ("'", stream
);
317 LA_EMIT_CHAR (c
, type
, stream
, '\'');
318 fputs_filtered ("'", stream
);
321 /* la_printstr implementation for Rust. */
324 rust_printstr (struct ui_file
*stream
, struct type
*type
,
325 const gdb_byte
*string
, unsigned int length
,
326 const char *user_encoding
, int force_ellipses
,
327 const struct value_print_options
*options
)
329 /* Rust always uses UTF-8, but let the caller override this if need
331 const char *encoding
= user_encoding
;
332 if (user_encoding
== NULL
|| !*user_encoding
)
334 /* In Rust strings, characters are "u8". */
335 if (rust_u8_type_p (type
))
339 /* This is probably some C string, so let's let C deal with
341 c_printstr (stream
, type
, string
, length
, user_encoding
,
342 force_ellipses
, options
);
347 /* This is not ideal as it doesn't use our character printer. */
348 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
354 /* Helper function to print a string slice. */
357 rust_val_print_str (struct ui_file
*stream
, struct value
*val
,
358 const struct value_print_options
*options
)
360 struct value
*base
= value_struct_elt (&val
, NULL
, "data_ptr", NULL
,
362 struct value
*len
= value_struct_elt (&val
, NULL
, "length", NULL
, "slice");
364 val_print_string (TYPE_TARGET_TYPE (value_type (base
)), "UTF-8",
365 value_as_address (base
), value_as_long (len
), stream
,
369 /* rust_val_print helper for structs and untagged unions. */
372 val_print_struct (struct type
*type
, int embedded_offset
,
373 CORE_ADDR address
, struct ui_file
*stream
,
374 int recurse
, struct value
*val
,
375 const struct value_print_options
*options
)
380 if (rust_slice_type_p (type
) && strcmp (TYPE_NAME (type
), "&str") == 0)
382 /* If what we are printing here is actually a string within a
383 structure then VAL will be the original parent value, while TYPE
384 will be the type of the structure representing the string we want
386 However, RUST_VAL_PRINT_STR looks up the fields of the string
387 inside VAL, assuming that VAL is the string.
388 So, recreate VAL as a value representing just the string. */
389 val
= value_at_lazy (type
, value_address (val
) + embedded_offset
);
390 rust_val_print_str (stream
, val
, options
);
394 bool is_tuple
= rust_tuple_type_p (type
);
395 bool is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
396 struct value_print_options opts
;
400 if (TYPE_NAME (type
) != NULL
)
401 fprintf_filtered (stream
, "%s", TYPE_NAME (type
));
403 if (TYPE_NFIELDS (type
) == 0)
406 if (TYPE_NAME (type
) != NULL
)
407 fputs_filtered (" ", stream
);
410 if (is_tuple
|| is_tuple_struct
)
411 fputs_filtered ("(", stream
);
413 fputs_filtered ("{", stream
);
419 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
421 if (field_is_static (&TYPE_FIELD (type
, i
)))
425 fputs_filtered (",", stream
);
427 if (options
->prettyformat
)
429 fputs_filtered ("\n", stream
);
430 print_spaces_filtered (2 + 2 * recurse
, stream
);
432 else if (!first_field
)
433 fputs_filtered (" ", stream
);
437 if (!is_tuple
&& !is_tuple_struct
)
439 fputs_styled (TYPE_FIELD_NAME (type
, i
),
440 variable_name_style
.style (), stream
);
441 fputs_filtered (": ", stream
);
444 val_print (TYPE_FIELD_TYPE (type
, i
),
445 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
447 stream
, recurse
+ 1, val
, &opts
,
451 if (options
->prettyformat
)
453 fputs_filtered ("\n", stream
);
454 print_spaces_filtered (2 * recurse
, stream
);
457 if (is_tuple
|| is_tuple_struct
)
458 fputs_filtered (")", stream
);
460 fputs_filtered ("}", stream
);
463 /* rust_val_print helper for discriminated unions (Rust enums). */
466 rust_print_enum (struct type
*type
, int embedded_offset
,
467 CORE_ADDR address
, struct ui_file
*stream
,
468 int recurse
, struct value
*val
,
469 const struct value_print_options
*options
)
471 struct value_print_options opts
= *options
;
475 if (rust_empty_enum_p (type
))
477 /* Print the enum type name here to be more clear. */
478 fprintf_filtered (stream
, _("%s {%p[<No data fields>%p]}"),
480 metadata_style
.style ().ptr (), nullptr);
484 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
485 struct field
*variant_field
= rust_enum_variant (type
,
486 valaddr
+ embedded_offset
);
487 embedded_offset
+= FIELD_BITPOS (*variant_field
) / 8;
488 struct type
*variant_type
= FIELD_TYPE (*variant_field
);
490 int nfields
= TYPE_NFIELDS (variant_type
);
492 bool is_tuple
= rust_tuple_struct_type_p (variant_type
);
494 fprintf_filtered (stream
, "%s", TYPE_NAME (variant_type
));
497 /* In case of a nullary variant like 'None', just output
502 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
504 fprintf_filtered (stream
, "(");
507 /* struct variant. */
508 fprintf_filtered (stream
, "{");
511 bool first_field
= true;
512 for (int j
= 0; j
< TYPE_NFIELDS (variant_type
); j
++)
515 fputs_filtered (", ", stream
);
519 fprintf_filtered (stream
, "%ps: ",
520 styled_string (variable_name_style
.style (),
521 TYPE_FIELD_NAME (variant_type
, j
)));
523 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
525 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
527 stream
, recurse
+ 1, val
, &opts
,
532 fputs_filtered (")", stream
);
534 fputs_filtered ("}", stream
);
537 static const struct generic_val_print_decorations rust_decorations
=
539 /* Complex isn't used in Rust, but we provide C-ish values just in
551 /* la_val_print implementation for Rust. */
554 rust_val_print (struct type
*type
, int embedded_offset
,
555 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
557 const struct value_print_options
*options
)
559 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
561 type
= check_typedef (type
);
562 switch (TYPE_CODE (type
))
566 LONGEST low_bound
, high_bound
;
568 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
569 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
570 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
572 /* We have a pointer to a byte string, so just print
574 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
576 struct gdbarch
*arch
= get_type_arch (type
);
577 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
579 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
580 if (options
->addressprint
)
582 fputs_filtered (paddress (arch
, addr
), stream
);
583 fputs_filtered (" ", stream
);
586 fputs_filtered ("b", stream
);
587 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
588 high_bound
- low_bound
+ 1, stream
,
595 case TYPE_CODE_METHODPTR
:
596 case TYPE_CODE_MEMBERPTR
:
597 c_val_print (type
, embedded_offset
, address
, stream
,
598 recurse
, val
, options
);
602 /* Recognize the unit type. */
603 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
604 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
606 fputs_filtered ("()", stream
);
611 case TYPE_CODE_STRING
:
613 struct gdbarch
*arch
= get_type_arch (type
);
614 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
615 LONGEST low_bound
, high_bound
;
617 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
618 error (_("Could not determine the array bounds"));
620 /* If we see a plain TYPE_CODE_STRING, then we're printing a
621 byte string, hence the choice of "ASCII" as the
623 fputs_filtered ("b", stream
);
624 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
625 valaddr
+ embedded_offset
* unit_size
,
626 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
630 case TYPE_CODE_ARRAY
:
632 LONGEST low_bound
, high_bound
;
634 if (get_array_bounds (type
, &low_bound
, &high_bound
)
635 && high_bound
- low_bound
+ 1 == 0)
636 fputs_filtered ("[]", stream
);
642 case TYPE_CODE_UNION
:
643 /* Untagged unions are printed as if they are structs. Since
644 the field bit positions overlap in the debuginfo, the code
645 for printing a union is same as that for a struct, the only
646 difference is that the input type will have overlapping
648 val_print_struct (type
, embedded_offset
, address
, stream
,
649 recurse
, val
, options
);
652 case TYPE_CODE_STRUCT
:
653 if (rust_enum_p (type
))
654 rust_print_enum (type
, embedded_offset
, address
, stream
,
655 recurse
, val
, options
);
657 val_print_struct (type
, embedded_offset
, address
, stream
,
658 recurse
, val
, options
);
663 /* Nothing special yet. */
664 generic_val_print (type
, embedded_offset
, address
, stream
,
665 recurse
, val
, options
, &rust_decorations
);
672 rust_internal_print_type (struct type
*type
, const char *varstring
,
673 struct ui_file
*stream
, int show
, int level
,
674 const struct type_print_options
*flags
,
675 bool for_rust_enum
, print_offset_data
*podata
);
677 /* Print a struct or union typedef. */
679 rust_print_struct_def (struct type
*type
, const char *varstring
,
680 struct ui_file
*stream
, int show
, int level
,
681 const struct type_print_options
*flags
,
682 bool for_rust_enum
, print_offset_data
*podata
)
684 /* Print a tuple type simply. */
685 if (rust_tuple_type_p (type
))
687 fputs_filtered (TYPE_NAME (type
), stream
);
691 /* If we see a base class, delegate to C. */
692 if (TYPE_N_BASECLASSES (type
) > 0)
693 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
695 if (flags
->print_offsets
)
697 /* Temporarily bump the level so that the output lines up
702 /* Compute properties of TYPE here because, in the enum case, the
703 rest of the code ends up looking only at the variant part. */
704 const char *tagname
= TYPE_NAME (type
);
705 bool is_tuple_struct
= rust_tuple_struct_type_p (type
);
706 bool is_tuple
= rust_tuple_type_p (type
);
707 bool is_enum
= rust_enum_p (type
);
709 int enum_discriminant_index
= -1;
713 /* Already printing an outer enum, so nothing to print here. */
717 /* This code path is also used by unions and enums. */
720 fputs_filtered ("enum ", stream
);
722 if (rust_empty_enum_p (type
))
726 fputs_filtered (tagname
, stream
);
727 fputs_filtered (" ", stream
);
729 fputs_filtered ("{}", stream
);
733 type
= TYPE_FIELD_TYPE (type
, 0);
735 struct dynamic_prop
*discriminant_prop
736 = get_dyn_prop (DYN_PROP_DISCRIMINATED
, type
);
737 struct discriminant_info
*info
738 = (struct discriminant_info
*) discriminant_prop
->data
.baton
;
739 enum_discriminant_index
= info
->discriminant_index
;
741 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
742 fputs_filtered ("struct ", stream
);
744 fputs_filtered ("union ", stream
);
747 fputs_filtered (tagname
, stream
);
750 if (TYPE_NFIELDS (type
) == 0 && !is_tuple
)
752 if (for_rust_enum
&& !flags
->print_offsets
)
753 fputs_filtered (is_tuple_struct
? "(" : "{", stream
);
755 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
757 /* When printing offsets, we rearrange the fields into storage
758 order. This lets us show holes more clearly. We work using
759 field indices here because it simplifies calls to
760 print_offset_data::update below. */
761 std::vector
<int> fields
;
762 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
764 if (field_is_static (&TYPE_FIELD (type
, i
)))
766 if (is_enum
&& i
== enum_discriminant_index
)
768 fields
.push_back (i
);
770 if (flags
->print_offsets
)
771 std::sort (fields
.begin (), fields
.end (),
774 return (TYPE_FIELD_BITPOS (type
, a
)
775 < TYPE_FIELD_BITPOS (type
, b
));
782 gdb_assert (!field_is_static (&TYPE_FIELD (type
, i
)));
783 gdb_assert (! (is_enum
&& i
== enum_discriminant_index
));
785 if (flags
->print_offsets
)
786 podata
->update (type
, i
, stream
);
788 /* We'd like to print "pub" here as needed, but rustc
789 doesn't emit the debuginfo, and our types don't have
790 cplus_struct_type attached. */
792 /* For a tuple struct we print the type but nothing
794 if (!for_rust_enum
|| flags
->print_offsets
)
795 print_spaces_filtered (level
+ 2, stream
);
797 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
799 else if (!is_tuple_struct
)
800 fprintf_filtered (stream
, "%ps: ",
801 styled_string (variable_name_style
.style (),
802 TYPE_FIELD_NAME (type
, i
)));
804 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
805 stream
, (is_enum
? show
: show
- 1),
806 level
+ 2, flags
, is_enum
, podata
);
807 if (!for_rust_enum
|| flags
->print_offsets
)
808 fputs_filtered (",\n", stream
);
809 /* Note that this check of "I" is ok because we only sorted the
810 fields by offset when print_offsets was set, so we won't take
811 this branch in that case. */
812 else if (i
+ 1 < TYPE_NFIELDS (type
))
813 fputs_filtered (", ", stream
);
816 if (flags
->print_offsets
)
818 /* Undo the temporary level increase we did above. */
820 podata
->finish (type
, level
, stream
);
821 print_spaces_filtered (print_offset_data::indentation
, stream
);
823 print_spaces_filtered (2, stream
);
825 if (!for_rust_enum
|| flags
->print_offsets
)
826 print_spaces_filtered (level
, stream
);
827 fputs_filtered (is_tuple_struct
? ")" : "}", stream
);
830 /* la_print_typedef implementation for Rust. */
833 rust_print_typedef (struct type
*type
,
834 struct symbol
*new_symbol
,
835 struct ui_file
*stream
)
837 type
= check_typedef (type
);
838 fprintf_filtered (stream
, "type %s = ", new_symbol
->print_name ());
839 type_print (type
, "", stream
, 0);
840 fprintf_filtered (stream
, ";");
843 /* la_print_type implementation for Rust. */
846 rust_internal_print_type (struct type
*type
, const char *varstring
,
847 struct ui_file
*stream
, int show
, int level
,
848 const struct type_print_options
*flags
,
849 bool for_rust_enum
, print_offset_data
*podata
)
853 && TYPE_NAME (type
) != NULL
)
855 /* Rust calls the unit type "void" in its debuginfo,
856 but we don't want to print it as that. */
857 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
858 fputs_filtered ("()", stream
);
860 fputs_filtered (TYPE_NAME (type
), stream
);
864 type
= check_typedef (type
);
865 switch (TYPE_CODE (type
))
868 /* If we have an enum, we've already printed the type's
869 unqualified name, and there is nothing else to print
872 fputs_filtered ("()", stream
);
876 /* Delegate varargs to the C printer. */
877 if (TYPE_VARARGS (type
))
880 fputs_filtered ("fn ", stream
);
881 if (varstring
!= NULL
)
882 fputs_filtered (varstring
, stream
);
883 fputs_filtered ("(", stream
);
884 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
888 fputs_filtered (", ", stream
);
889 rust_internal_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
,
890 -1, 0, flags
, false, podata
);
892 fputs_filtered (")", stream
);
893 /* If it returns unit, we can omit the return type. */
894 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
896 fputs_filtered (" -> ", stream
);
897 rust_internal_print_type (TYPE_TARGET_TYPE (type
), "", stream
,
898 -1, 0, flags
, false, podata
);
902 case TYPE_CODE_ARRAY
:
904 LONGEST low_bound
, high_bound
;
906 fputs_filtered ("[", stream
);
907 rust_internal_print_type (TYPE_TARGET_TYPE (type
), NULL
,
908 stream
, show
- 1, level
, flags
, false,
911 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
912 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
913 fprintf_filtered (stream
, "; variable length");
914 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
915 fprintf_filtered (stream
, "; %s",
916 plongest (high_bound
- low_bound
+ 1));
917 fputs_filtered ("]", stream
);
921 case TYPE_CODE_UNION
:
922 case TYPE_CODE_STRUCT
:
923 rust_print_struct_def (type
, varstring
, stream
, show
, level
, flags
,
924 for_rust_enum
, podata
);
931 fputs_filtered ("enum ", stream
);
932 if (TYPE_NAME (type
) != NULL
)
934 fputs_filtered (TYPE_NAME (type
), stream
);
935 fputs_filtered (" ", stream
);
936 len
= strlen (TYPE_NAME (type
));
938 fputs_filtered ("{\n", stream
);
940 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
942 const char *name
= TYPE_FIELD_NAME (type
, i
);
947 && strncmp (name
, TYPE_NAME (type
), len
) == 0
949 && name
[len
+ 1] == ':')
951 fprintfi_filtered (level
+ 2, stream
, "%ps,\n",
952 styled_string (variable_name_style
.style (),
956 fputs_filtered ("}", stream
);
962 if (TYPE_NAME (type
) != nullptr)
963 fputs_filtered (TYPE_NAME (type
), stream
);
966 /* We currently can't distinguish between pointers and
968 fputs_filtered ("*mut ", stream
);
969 type_print (TYPE_TARGET_TYPE (type
), "", stream
, 0);
976 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
981 rust_print_type (struct type
*type
, const char *varstring
,
982 struct ui_file
*stream
, int show
, int level
,
983 const struct type_print_options
*flags
)
985 print_offset_data podata
;
986 rust_internal_print_type (type
, varstring
, stream
, show
, level
,
987 flags
, false, &podata
);
992 /* Like arch_composite_type, but uses TYPE to decide how to allocate
993 -- either on an obstack or on a gdbarch. */
996 rust_composite_type (struct type
*original
,
998 const char *field1
, struct type
*type1
,
999 const char *field2
, struct type
*type2
)
1001 struct type
*result
= alloc_type_copy (original
);
1002 int i
, nfields
, bitpos
;
1010 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1011 TYPE_NAME (result
) = name
;
1013 TYPE_NFIELDS (result
) = nfields
;
1014 TYPE_FIELDS (result
)
1015 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1021 struct field
*field
= &TYPE_FIELD (result
, i
);
1023 SET_FIELD_BITPOS (*field
, bitpos
);
1024 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1026 FIELD_NAME (*field
) = field1
;
1027 FIELD_TYPE (*field
) = type1
;
1032 struct field
*field
= &TYPE_FIELD (result
, i
);
1033 unsigned align
= type_align (type2
);
1039 align
*= TARGET_CHAR_BIT
;
1040 delta
= bitpos
% align
;
1042 bitpos
+= align
- delta
;
1044 SET_FIELD_BITPOS (*field
, bitpos
);
1046 FIELD_NAME (*field
) = field2
;
1047 FIELD_TYPE (*field
) = type2
;
1052 TYPE_LENGTH (result
)
1053 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1054 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1058 /* See rust-lang.h. */
1061 rust_slice_type (const char *name
, struct type
*elt_type
,
1062 struct type
*usize_type
)
1066 elt_type
= lookup_pointer_type (elt_type
);
1067 type
= rust_composite_type (elt_type
, name
,
1068 "data_ptr", elt_type
,
1069 "length", usize_type
);
1074 enum rust_primitive_types
1076 rust_primitive_bool
,
1077 rust_primitive_char
,
1086 rust_primitive_isize
,
1087 rust_primitive_usize
,
1090 rust_primitive_unit
,
1092 nr_rust_primitive_types
1095 /* la_language_arch_info implementation for Rust. */
1098 rust_language_arch_info (struct gdbarch
*gdbarch
,
1099 struct language_arch_info
*lai
)
1101 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1103 struct type
**types
;
1104 unsigned int length
;
1106 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1109 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1110 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1111 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1112 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1113 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1114 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1115 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1116 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1117 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1118 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1120 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1121 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1122 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1124 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32",
1125 floatformats_ieee_single
);
1126 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64",
1127 floatformats_ieee_double
);
1129 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1131 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1132 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1133 types
[rust_primitive_usize
]);
1135 lai
->primitive_type_vector
= types
;
1136 lai
->bool_type_default
= types
[rust_primitive_bool
];
1137 lai
->string_char_type
= types
[rust_primitive_u8
];
1142 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1144 static struct value
*
1145 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1148 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1150 struct value
*function
, *result
, *arg0
;
1151 struct type
*type
, *fn_type
;
1152 const struct block
*block
;
1153 struct block_symbol sym
;
1155 /* For an ordinary function call we can simply defer to the
1156 generic implementation. */
1157 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1158 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1160 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1162 method
= &exp
->elts
[*pos
+ 1].string
;
1163 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1165 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1166 type in order to look up the method. */
1167 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1169 if (noside
== EVAL_SKIP
)
1171 for (i
= 0; i
< num_args
; ++i
)
1172 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1176 std::vector
<struct value
*> args (num_args
+ 1);
1179 /* We don't yet implement real Deref semantics. */
1180 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1181 args
[0] = value_ind (args
[0]);
1183 type
= value_type (args
[0]);
1184 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1185 && TYPE_CODE (type
) != TYPE_CODE_UNION
1186 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1187 || rust_tuple_type_p (type
))
1188 error (_("Method calls only supported on struct or enum types"));
1189 if (TYPE_NAME (type
) == NULL
)
1190 error (_("Method call on nameless type"));
1192 std::string name
= std::string (TYPE_NAME (type
)) + "::" + method
;
1194 block
= get_selected_block (0);
1195 sym
= lookup_symbol (name
.c_str (), block
, VAR_DOMAIN
, NULL
);
1196 if (sym
.symbol
== NULL
)
1197 error (_("Could not find function named '%s'"), name
.c_str ());
1199 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1200 if (TYPE_NFIELDS (fn_type
) == 0)
1201 error (_("Function '%s' takes no arguments"), name
.c_str ());
1203 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1204 args
[0] = value_addr (args
[0]);
1206 function
= address_of_variable (sym
.symbol
, block
);
1208 for (i
= 0; i
< num_args
; ++i
)
1209 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1211 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1212 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1214 result
= call_function_by_hand (function
, NULL
, args
);
1218 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1220 static struct value
*
1221 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1223 enum range_type kind
;
1224 struct value
*low
= NULL
, *high
= NULL
;
1225 struct value
*addrval
, *result
;
1227 struct type
*range_type
;
1228 struct type
*index_type
;
1229 struct type
*temp_type
;
1232 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1235 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
1236 || kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1237 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1238 if (kind
== LOW_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT_EXCLUSIVE
1239 || kind
== NONE_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT_EXCLUSIVE
)
1240 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1241 bool inclusive
= (kind
== NONE_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
);
1243 if (noside
== EVAL_SKIP
)
1244 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1251 name
= "std::ops::RangeFull";
1255 index_type
= value_type (high
);
1257 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1264 index_type
= value_type (low
);
1265 name
= "std::ops::RangeFrom";
1269 if (!types_equal (value_type (low
), value_type (high
)))
1270 error (_("Range expression with different types"));
1271 index_type
= value_type (low
);
1272 name
= inclusive
? "std::ops::RangeInclusive" : "std::ops::Range";
1276 /* If we don't have an index type, just allocate this on the
1277 arch. Here any type will do. */
1278 temp_type
= (index_type
== NULL
1279 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1281 /* It would be nicer to cache the range type. */
1282 range_type
= rust_composite_type (temp_type
, name
,
1283 low
== NULL
? NULL
: "start", index_type
,
1284 high
== NULL
? NULL
: "end", index_type
);
1286 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1287 return value_zero (range_type
, lval_memory
);
1289 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1290 addr
= value_as_long (addrval
);
1291 result
= value_at_lazy (range_type
, addr
);
1295 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1298 value_assign (start
, low
);
1303 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1306 value_assign (end
, high
);
1309 result
= value_at_lazy (range_type
, addr
);
1313 /* A helper function to compute the range and kind given a range
1314 value. TYPE is the type of the range value. RANGE is the range
1315 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1316 parameters might be filled in, or might not be, depending on the
1317 kind of range this is. KIND will always be set to the appropriate
1318 value describing the kind of range, and this can be used to
1319 determine whether LOW or HIGH are valid. */
1322 rust_compute_range (struct type
*type
, struct value
*range
,
1323 LONGEST
*low
, LONGEST
*high
,
1324 enum range_type
*kind
)
1330 *kind
= BOTH_BOUND_DEFAULT
;
1332 if (TYPE_NFIELDS (type
) == 0)
1336 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1338 *kind
= HIGH_BOUND_DEFAULT
;
1339 *low
= value_as_long (value_field (range
, 0));
1342 if (TYPE_NFIELDS (type
) > i
1343 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1345 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1346 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1347 *high
= value_as_long (value_field (range
, i
));
1349 if (rust_inclusive_range_type_p (type
))
1354 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1356 static struct value
*
1357 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1360 struct value
*lhs
, *rhs
, *result
;
1361 struct type
*rhstype
;
1362 LONGEST low
, high_bound
;
1363 /* Initialized to appease the compiler. */
1364 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1369 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1370 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1372 if (noside
== EVAL_SKIP
)
1375 rhstype
= check_typedef (value_type (rhs
));
1376 if (rust_range_type_p (rhstype
))
1379 error (_("Can't take slice of array without '&'"));
1380 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1384 low
= value_as_long (rhs
);
1386 struct type
*type
= check_typedef (value_type (lhs
));
1387 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1389 struct type
*base_type
= nullptr;
1390 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1391 base_type
= TYPE_TARGET_TYPE (type
);
1392 else if (rust_slice_type_p (type
))
1394 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
1396 if (strcmp (TYPE_FIELD_NAME (type
, i
), "data_ptr") == 0)
1398 base_type
= TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type
, i
));
1402 if (base_type
== nullptr)
1403 error (_("Could not find 'data_ptr' in slice type"));
1405 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1406 base_type
= TYPE_TARGET_TYPE (type
);
1408 error (_("Cannot subscript non-array type"));
1410 struct type
*new_type
;
1413 if (rust_slice_type_p (type
))
1418 = language_lookup_primitive_type (exp
->language_defn
,
1421 new_type
= rust_slice_type ("&[*gdb*]", base_type
, usize
);
1425 new_type
= base_type
;
1427 return value_zero (new_type
, VALUE_LVAL (lhs
));
1434 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1437 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1438 error (_("Can't compute array bounds"));
1440 error (_("Found array with non-zero lower bound"));
1443 else if (rust_slice_type_p (type
))
1447 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1448 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1450 high_bound
= value_as_long (len
);
1452 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1456 high_bound
= LONGEST_MAX
;
1459 error (_("Cannot subscript non-array type"));
1462 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1465 error (_("Index less than zero"));
1466 if (low
> high_bound
)
1467 error (_("Index greater than length"));
1469 result
= value_subscript (base
, low
);
1476 struct type
*usize
, *slice
;
1478 struct value
*addrval
, *tem
;
1480 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1483 error (_("High index less than zero"));
1485 error (_("Low index greater than high index"));
1486 if (high
> high_bound
)
1487 error (_("High index greater than length"));
1489 usize
= language_lookup_primitive_type (exp
->language_defn
,
1492 const char *new_name
= ((type
!= nullptr
1493 && rust_slice_type_p (type
))
1494 ? TYPE_NAME (type
) : "&[*gdb*]");
1496 slice
= rust_slice_type (new_name
, value_type (result
), usize
);
1498 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1499 addr
= value_as_long (addrval
);
1500 tem
= value_at_lazy (slice
, addr
);
1502 value_assign (value_field (tem
, 0), value_addr (result
));
1503 value_assign (value_field (tem
, 1),
1504 value_from_longest (usize
, high
- low
));
1506 result
= value_at_lazy (slice
, addr
);
1509 result
= value_addr (result
);
1515 /* evaluate_exp implementation for Rust. */
1517 static struct value
*
1518 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1519 int *pos
, enum noside noside
)
1521 struct value
*result
;
1523 switch (exp
->elts
[*pos
].opcode
)
1527 if (noside
!= EVAL_NORMAL
)
1528 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1532 struct value
*value
= evaluate_subexp (expect_type
, exp
, pos
,
1535 struct value
*trait_ptr
= rust_get_trait_object_pointer (value
);
1536 if (trait_ptr
!= NULL
)
1539 result
= value_ind (value
);
1544 case UNOP_COMPLEMENT
:
1546 struct value
*value
;
1549 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1550 if (noside
== EVAL_SKIP
)
1552 /* Preserving the type is enough. */
1555 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1556 result
= value_from_longest (value_type (value
),
1557 value_logical_not (value
));
1559 result
= value_complement (value
);
1563 case BINOP_SUBSCRIPT
:
1564 result
= rust_subscript (exp
, pos
, noside
, 0);
1568 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1574 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1575 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1578 struct value
*addrval
= NULL
;
1582 if (noside
== EVAL_NORMAL
)
1584 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1585 addr
= value_as_long (addrval
);
1586 result
= value_at_lazy (type
, addr
);
1589 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1594 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1595 if (noside
== EVAL_NORMAL
)
1597 /* This isn't quite right but will do for the time
1598 being, seeing that we can't implement the Copy
1600 value_assign (result
, init
);
1606 gdb_assert (arglen
% 2 == 0);
1607 for (i
= 0; i
< arglen
; i
+= 2)
1610 const char *fieldname
;
1611 struct value
*value
, *field
;
1613 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1615 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1617 fieldname
= &exp
->elts
[*pos
].string
;
1618 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1620 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1621 if (noside
== EVAL_NORMAL
)
1623 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1625 value_assign (field
, value
);
1629 if (noside
== EVAL_SKIP
)
1630 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1632 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1633 result
= allocate_value (type
);
1635 result
= value_at_lazy (type
, addr
);
1644 struct value
*ncopies
;
1646 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1647 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1648 copies
= value_as_long (ncopies
);
1650 error (_("Array with negative number of elements"));
1652 if (noside
== EVAL_NORMAL
)
1655 std::vector
<struct value
*> eltvec (copies
);
1657 for (i
= 0; i
< copies
; ++i
)
1659 result
= value_array (0, copies
- 1, eltvec
.data ());
1663 struct type
*arraytype
1664 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1665 result
= allocate_value (arraytype
);
1670 case STRUCTOP_ANONYMOUS
:
1672 /* Anonymous field access, i.e. foo.1. */
1674 int pc
, field_number
, nfields
;
1678 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1680 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1682 type
= value_type (lhs
);
1684 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1686 struct type
*outer_type
= NULL
;
1688 if (rust_enum_p (type
))
1690 if (rust_empty_enum_p (type
))
1691 error (_("Cannot access field %d of empty enum %s"),
1692 field_number
, TYPE_NAME (type
));
1694 const gdb_byte
*valaddr
= value_contents (lhs
);
1695 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1697 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1700 int fieldno
= (variant_field
1701 - &TYPE_FIELD (value_type (union_value
), 0));
1702 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1703 value_type (union_value
));
1705 type
= value_type (lhs
);
1708 /* Tuples and tuple structs */
1709 nfields
= TYPE_NFIELDS (type
);
1711 if (field_number
>= nfields
|| field_number
< 0)
1713 if (outer_type
!= NULL
)
1714 error(_("Cannot access field %d of variant %s::%s, "
1715 "there are only %d fields"),
1716 field_number
, TYPE_NAME (outer_type
),
1717 rust_last_path_segment (TYPE_NAME (type
)),
1720 error(_("Cannot access field %d of %s, "
1721 "there are only %d fields"),
1722 field_number
, TYPE_NAME (type
), nfields
);
1725 /* Tuples are tuple structs too. */
1726 if (!rust_tuple_struct_type_p (type
))
1728 if (outer_type
!= NULL
)
1729 error(_("Variant %s::%s is not a tuple variant"),
1730 TYPE_NAME (outer_type
),
1731 rust_last_path_segment (TYPE_NAME (type
)));
1733 error(_("Attempting to access anonymous field %d "
1734 "of %s, which is not a tuple, tuple struct, or "
1735 "tuple-like variant"),
1736 field_number
, TYPE_NAME (type
));
1739 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1742 error(_("Anonymous field access is only allowed on tuples, \
1743 tuple structs, and tuple-like enum variants"));
1747 case STRUCTOP_STRUCT
:
1754 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1755 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1756 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1758 const char *field_name
= &exp
->elts
[pc
+ 2].string
;
1759 type
= value_type (lhs
);
1760 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& rust_enum_p (type
))
1762 if (rust_empty_enum_p (type
))
1763 error (_("Cannot access field %s of empty enum %s"),
1764 field_name
, TYPE_NAME (type
));
1766 const gdb_byte
*valaddr
= value_contents (lhs
);
1767 struct field
*variant_field
= rust_enum_variant (type
, valaddr
);
1769 struct value
*union_value
= value_primitive_field (lhs
, 0, 0,
1772 int fieldno
= (variant_field
1773 - &TYPE_FIELD (value_type (union_value
), 0));
1774 lhs
= value_primitive_field (union_value
, 0, fieldno
,
1775 value_type (union_value
));
1777 struct type
*outer_type
= type
;
1778 type
= value_type (lhs
);
1779 if (rust_tuple_type_p (type
) || rust_tuple_struct_type_p (type
))
1780 error (_("Attempting to access named field %s of tuple "
1781 "variant %s::%s, which has only anonymous fields"),
1782 field_name
, TYPE_NAME (outer_type
),
1783 rust_last_path_segment (TYPE_NAME (type
)));
1787 result
= value_struct_elt (&lhs
, NULL
, field_name
,
1790 catch (const gdb_exception_error
&except
)
1792 error (_("Could not find field %s of struct variant %s::%s"),
1793 field_name
, TYPE_NAME (outer_type
),
1794 rust_last_path_segment (TYPE_NAME (type
)));
1798 result
= value_struct_elt (&lhs
, NULL
, field_name
, NULL
, "structure");
1799 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1800 result
= value_zero (value_type (result
), VALUE_LVAL (result
));
1805 result
= rust_range (exp
, pos
, noside
);
1809 /* We might have &array[range], in which case we need to make a
1811 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1814 result
= rust_subscript (exp
, pos
, noside
, 1);
1819 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1826 /* operator_length implementation for Rust. */
1829 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1835 switch (exp
->elts
[pc
- 1].opcode
)
1838 /* We handle aggregate as a type and argument count. The first
1839 argument might be OP_OTHERS. After that the arguments
1840 alternate: first an OP_NAME, then an expression. */
1842 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1850 case STRUCTOP_ANONYMOUS
:
1861 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1869 /* op_name implementation for Rust. */
1872 rust_op_name (enum exp_opcode opcode
)
1877 return "OP_AGGREGATE";
1881 return op_name_standard (opcode
);
1885 /* dump_subexp_body implementation for Rust. */
1888 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1891 switch (exp
->elts
[elt
].opcode
)
1895 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1898 fprintf_filtered (stream
, "Type @");
1899 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1900 fprintf_filtered (stream
, " (");
1901 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1902 fprintf_filtered (stream
, "), length %d", length
);
1905 for (i
= 0; i
< length
; ++i
)
1906 elt
= dump_subexp (exp
, stream
, elt
);
1913 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1915 fprintf_filtered (stream
, "%s: %s",
1916 (exp
->elts
[elt
].opcode
== OP_STRING
1917 ? "string" : "name"),
1918 &exp
->elts
[elt
+ 2].string
);
1919 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1924 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1927 case STRUCTOP_ANONYMOUS
:
1931 field_number
= longest_to_int (exp
->elts
[elt
+ 1].longconst
);
1933 fprintf_filtered (stream
, "Field number: %d", field_number
);
1934 elt
= dump_subexp (exp
, stream
, elt
+ 3);
1943 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1950 /* print_subexp implementation for Rust. */
1953 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1954 enum precedence prec
)
1956 switch (exp
->elts
[*pos
].opcode
)
1960 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1963 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1964 fputs_filtered (" { ", stream
);
1967 for (i
= 0; i
< length
; ++i
)
1969 rust_print_subexp (exp
, pos
, stream
, prec
);
1970 fputs_filtered (", ", stream
);
1972 fputs_filtered (" }", stream
);
1978 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1980 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1981 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1987 fputs_filtered ("<<others>> (", stream
);
1989 rust_print_subexp (exp
, pos
, stream
, prec
);
1990 fputs_filtered (")", stream
);
1994 case STRUCTOP_ANONYMOUS
:
1996 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1999 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
2000 fprintf_filtered (stream
, ".%d", tem
);
2006 fprintf_filtered (stream
, "[");
2007 rust_print_subexp (exp
, pos
, stream
, prec
);
2008 fprintf_filtered (stream
, "; ");
2009 rust_print_subexp (exp
, pos
, stream
, prec
);
2010 fprintf_filtered (stream
, "]");
2014 print_subexp_standard (exp
, pos
, stream
, prec
);
2019 /* operator_check implementation for Rust. */
2022 rust_operator_check (struct expression
*exp
, int pos
,
2023 int (*objfile_func
) (struct objfile
*objfile
,
2027 switch (exp
->elts
[pos
].opcode
)
2031 struct type
*type
= exp
->elts
[pos
+ 1].type
;
2032 struct objfile
*objfile
= TYPE_OBJFILE (type
);
2034 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
2045 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2053 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2055 static struct block_symbol
2056 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2058 const struct block
*block
,
2059 const domain_enum domain
)
2061 struct block_symbol result
= {};
2063 if (symbol_lookup_debug
)
2065 fprintf_unfiltered (gdb_stdlog
,
2066 "rust_lookup_symbol_non_local"
2067 " (%s, %s (scope %s), %s)\n",
2068 name
, host_address_to_string (block
),
2069 block_scope (block
), domain_name (domain
));
2072 /* Look up bare names in the block's scope. */
2073 std::string scopedname
;
2074 if (name
[cp_find_first_component (name
)] == '\0')
2076 const char *scope
= block_scope (block
);
2078 if (scope
[0] != '\0')
2080 scopedname
= std::string (scope
) + "::" + name
;
2081 name
= scopedname
.c_str ();
2089 result
= lookup_symbol_in_static_block (name
, block
, domain
);
2090 if (result
.symbol
== NULL
)
2091 result
= lookup_global_symbol (name
, block
, domain
);
2098 /* la_sniff_from_mangled_name for Rust. */
2101 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2103 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2104 return *demangled
!= NULL
;
2109 /* la_watch_location_expression for Rust. */
2111 static gdb::unique_xmalloc_ptr
<char>
2112 rust_watch_location_expression (struct type
*type
, CORE_ADDR addr
)
2114 type
= check_typedef (TYPE_TARGET_TYPE (check_typedef (type
)));
2115 std::string name
= type_to_string (type
);
2116 return gdb::unique_xmalloc_ptr
<char>
2117 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr
),
2123 static const struct exp_descriptor exp_descriptor_rust
=
2126 rust_operator_length
,
2127 rust_operator_check
,
2129 rust_dump_subexp_body
,
2130 rust_evaluate_subexp
2133 static const char *rust_extensions
[] =
2138 extern const struct language_defn rust_language_defn
=
2148 &exp_descriptor_rust
,
2151 rust_printchar
, /* Print a character constant */
2152 rust_printstr
, /* Function to print string constant */
2153 rust_emitchar
, /* Print a single char */
2154 rust_print_type
, /* Print a type using appropriate syntax */
2155 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2156 rust_val_print
, /* Print a value using appropriate syntax */
2157 c_value_print
, /* Print a top-level value */
2158 default_read_var_value
, /* la_read_var_value */
2159 NULL
, /* Language specific skip_trampoline */
2160 NULL
, /* name_of_this */
2161 false, /* la_store_sym_names_in_linkage_form_p */
2162 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2163 basic_lookup_transparent_type
,/* lookup_transparent_type */
2164 gdb_demangle
, /* Language specific symbol demangler */
2165 rust_sniff_from_mangled_name
,
2166 NULL
, /* Language specific
2167 class_name_from_physname */
2168 c_op_print_tab
, /* expression operators for printing */
2169 1, /* c-style arrays */
2170 0, /* String lower bound */
2171 default_word_break_characters
,
2172 default_collect_symbol_completion_matches
,
2173 rust_language_arch_info
,
2174 default_print_array_index
,
2175 default_pass_by_reference
,
2176 rust_watch_location_expression
,
2177 NULL
, /* la_get_symbol_name_matcher */
2178 iterate_over_symbols
,
2179 default_search_name_hash
,
2180 &default_varobj_ops
,
2183 rust_is_string_type_p
,
2184 "{...}" /* la_struct_too_deep_ellipsis */