1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016 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"
32 #include "rust-lang.h"
36 extern initialize_file_ftype _initialize_rust_language
;
38 /* Returns the last segment of a Rust path like foo::bar::baz. Will
39 not handle cases where the last segment contains generics. This
40 will return NULL if the last segment cannot be found. */
43 rust_last_path_segment (const char * path
)
45 const char *result
= strrchr (path
, ':');
52 /* Find the Rust crate for BLOCK. If no crate can be found, returns
53 NULL. Otherwise, returns a newly allocated string that the caller
54 is responsible for freeing. */
57 rust_crate_for_block (const struct block
*block
)
59 const char *scope
= block_scope (block
);
64 return xstrndup (scope
, cp_find_first_component (scope
));
67 /* Information about the discriminant/variant of an enum */
71 /* Name of field. Must be freed by caller. */
73 /* Field number in union. Negative on error. For an encoded enum,
74 the "hidden" member will always be field 1, and the "real" member
75 will always be field 0. */
77 /* True if this is an encoded enum that has a single "real" member
78 and a single "hidden" member. */
79 unsigned int is_encoded
: 1;
82 /* The prefix of a specially-encoded enum. */
84 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
86 /* The number of the real field. */
88 #define RUST_ENCODED_ENUM_REAL 0
90 /* The number of the hidden field. */
92 #define RUST_ENCODED_ENUM_HIDDEN 1
94 /* Utility function to get discriminant info for a given value. */
96 static struct disr_info
97 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
98 int embedded_offset
, CORE_ADDR address
,
99 const struct value
*val
)
102 struct disr_info ret
;
103 struct type
*disr_type
;
104 struct ui_file
*temp_file
;
105 struct value_print_options opts
;
106 struct cleanup
*cleanup
;
107 const char *name_segment
;
109 get_no_prettyformat_print_options (&opts
);
114 if (TYPE_NFIELDS (type
) == 0)
115 error (_("Encountered void enum value"));
117 /* If an enum has two values where one is empty and the other holds
118 a pointer that cannot be zero; then the Rust compiler optimizes
119 away the discriminant and instead uses a zero value in the
120 pointer field to indicate the empty variant. */
121 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
122 strlen (RUST_ENUM_PREFIX
)) == 0)
124 char *name
, *tail
, *token
;
125 unsigned long fieldno
;
126 struct type
*member_type
;
131 if (TYPE_NFIELDS (type
) != 1)
132 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
134 /* Optimized enums have only one field. */
135 member_type
= TYPE_FIELD_TYPE (type
, 0);
137 name
= xstrdup (TYPE_FIELD_NAME (type
, 0));
138 cleanup
= make_cleanup (xfree
, name
);
139 tail
= name
+ strlen (RUST_ENUM_PREFIX
);
141 /* The location of the value that doubles as a discriminant is
142 stored in the name of the field, as
143 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
144 where the fieldnos are the indices of the fields that should be
145 traversed in order to find the field (which may be several fields deep)
146 and the variantname is the name of the variant of the case when the
148 while ((token
= strsep (&tail
, "$")) != NULL
)
150 if (sscanf (token
, "%lu", &fieldno
) != 1)
152 /* We have reached the enum name, which cannot start
156 if (fieldno
>= TYPE_NFIELDS (member_type
))
157 error (_("%s refers to field after end of member type"),
160 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
161 member_type
= TYPE_FIELD_TYPE (member_type
, fieldno
);
164 if (token
>= name
+ strlen (TYPE_FIELD_NAME (type
, 0)))
165 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
166 value
= unpack_long (member_type
, valaddr
+ embedded_offset
);
170 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
171 ret
.name
= concat (TYPE_NAME (type
), "::", token
, (char *) NULL
);
175 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
176 ret
.name
= concat (TYPE_NAME (type
), "::",
177 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type
, 0))),
181 do_cleanups (cleanup
);
185 disr_type
= TYPE_FIELD_TYPE (type
, 0);
187 if (TYPE_NFIELDS (disr_type
) == 0)
189 /* This is a bounds check and should never be hit unless Rust
190 has changed its debuginfo format. */
191 error (_("Could not find enum discriminant field"));
194 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
195 error (_("Rust debug format has changed"));
197 temp_file
= mem_fileopen ();
198 cleanup
= make_cleanup_ui_file_delete (temp_file
);
199 /* The first value of the first field (or any field)
200 is the discriminant value. */
201 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
202 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
203 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
207 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
208 name_segment
= rust_last_path_segment (ret
.name
);
209 if (name_segment
!= NULL
)
211 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
213 /* Sadly, the discriminant value paths do not match the type
214 field name paths ('core::option::Option::Some' vs
215 'core::option::Some'). However, enum variant names are
216 unique in the last path segment and the generics are not
217 part of this path, so we can just compare those. This is
218 hackish and would be better fixed by improving rustc's
219 metadata for enums. */
220 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
222 if (field_type
!= NULL
223 && strcmp (name_segment
,
224 rust_last_path_segment (field_type
)) == 0)
232 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
234 /* Somehow the discriminant wasn't found. */
235 make_cleanup (xfree
, ret
.name
);
236 error (_("Could not find variant of %s with discriminant %s"),
237 TYPE_TAG_NAME (type
), ret
.name
);
240 do_cleanups (cleanup
);
244 /* See rust-lang.h. */
247 rust_tuple_type_p (struct type
*type
)
249 /* The current implementation is a bit of a hack, but there's
250 nothing else in the debuginfo to distinguish a tuple from a
252 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
253 && TYPE_TAG_NAME (type
) != NULL
254 && TYPE_TAG_NAME (type
)[0] == '(');
258 /* Return true if all non-static fields of a structlike type are in a
259 sequence like __0, __1, __2. OFFSET lets us skip fields. */
262 rust_underscore_fields (struct type
*type
, int offset
)
268 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
270 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
272 if (!field_is_static (&TYPE_FIELD (type
, i
)))
280 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
281 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
290 /* See rust-lang.h. */
293 rust_tuple_struct_type_p (struct type
*type
)
295 return rust_underscore_fields (type
, 0);
298 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
301 rust_tuple_variant_type_p (struct type
*type
)
303 /* First field is discriminant */
304 return rust_underscore_fields (type
, 1);
307 /* Return true if TYPE is a slice type, otherwise false. */
310 rust_slice_type_p (struct type
*type
)
312 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
313 && TYPE_TAG_NAME (type
) != NULL
314 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
317 /* Return true if TYPE is a range type, otherwise false. */
320 rust_range_type_p (struct type
*type
)
324 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
325 || TYPE_NFIELDS (type
) > 2
326 || TYPE_TAG_NAME (type
) == NULL
327 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
330 if (TYPE_NFIELDS (type
) == 0)
334 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
336 if (TYPE_NFIELDS (type
) == 1)
340 else if (TYPE_NFIELDS (type
) == 2)
342 /* First field had to be "start". */
346 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
349 /* Return true if TYPE seems to be the type "u8", otherwise false. */
352 rust_u8_type_p (struct type
*type
)
354 return (TYPE_CODE (type
) == TYPE_CODE_INT
355 && TYPE_UNSIGNED (type
)
356 && TYPE_LENGTH (type
) == 1);
359 /* Return true if TYPE is a Rust character type. */
362 rust_chartype_p (struct type
*type
)
364 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
365 && TYPE_LENGTH (type
) == 4
366 && TYPE_UNSIGNED (type
));
371 /* la_emitchar implementation for Rust. */
374 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
376 if (!rust_chartype_p (type
))
377 generic_emit_char (c
, type
, stream
, quoter
,
378 target_charset (get_type_arch (type
)));
379 else if (c
== '\\' || c
== quoter
)
380 fprintf_filtered (stream
, "\\%c", c
);
382 fputs_filtered ("\\n", stream
);
384 fputs_filtered ("\\r", stream
);
386 fputs_filtered ("\\t", stream
);
388 fputs_filtered ("\\0", stream
);
389 else if (c
>= 32 && c
<= 127 && isprint (c
))
390 fputc_filtered (c
, stream
);
392 fprintf_filtered (stream
, "\\x%02x", c
);
394 fprintf_filtered (stream
, "\\u{%06x}", c
);
397 /* la_printchar implementation for Rust. */
400 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
402 fputs_filtered ("'", stream
);
403 LA_EMIT_CHAR (c
, type
, stream
, '\'');
404 fputs_filtered ("'", stream
);
407 /* la_printstr implementation for Rust. */
410 rust_printstr (struct ui_file
*stream
, struct type
*type
,
411 const gdb_byte
*string
, unsigned int length
,
412 const char *user_encoding
, int force_ellipses
,
413 const struct value_print_options
*options
)
415 /* Rust always uses UTF-8, but let the caller override this if need
417 const char *encoding
= user_encoding
;
418 if (user_encoding
== NULL
|| !*user_encoding
)
420 /* In Rust strings, characters are "u8". */
421 if (rust_u8_type_p (type
))
425 /* This is probably some C string, so let's let C deal with
427 c_printstr (stream
, type
, string
, length
, user_encoding
,
428 force_ellipses
, options
);
433 /* This is not ideal as it doesn't use our character printer. */
434 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
440 static const struct generic_val_print_decorations rust_decorations
=
442 /* Complex isn't used in Rust, but we provide C-ish values just in
454 /* la_val_print implementation for Rust. */
457 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
458 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
459 const struct value
*val
,
460 const struct value_print_options
*options
)
462 type
= check_typedef (type
);
463 switch (TYPE_CODE (type
))
467 LONGEST low_bound
, high_bound
;
469 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
470 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
471 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
473 /* We have a pointer to a byte string, so just print
475 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
477 struct gdbarch
*arch
= get_type_arch (type
);
478 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
480 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
481 if (options
->addressprint
)
483 fputs_filtered (paddress (arch
, addr
), stream
);
484 fputs_filtered (" ", stream
);
487 fputs_filtered ("b", stream
);
488 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
489 high_bound
- low_bound
+ 1, stream
,
496 case TYPE_CODE_METHODPTR
:
497 case TYPE_CODE_MEMBERPTR
:
498 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
499 recurse
, val
, options
);
503 /* Recognize the unit type. */
504 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
505 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
507 fputs_filtered ("()", stream
);
512 case TYPE_CODE_STRING
:
514 struct gdbarch
*arch
= get_type_arch (type
);
515 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
516 LONGEST low_bound
, high_bound
;
518 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
519 error (_("Could not determine the array bounds"));
521 /* If we see a plain TYPE_CODE_STRING, then we're printing a
522 byte string, hence the choice of "ASCII" as the
524 fputs_filtered ("b", stream
);
525 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
526 valaddr
+ embedded_offset
* unit_size
,
527 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
531 case TYPE_CODE_ARRAY
:
533 LONGEST low_bound
, high_bound
;
535 if (get_array_bounds (type
, &low_bound
, &high_bound
)
536 && high_bound
- low_bound
+ 1 == 0)
537 fputs_filtered ("[]", stream
);
543 case TYPE_CODE_UNION
:
545 int j
, nfields
, first_field
, is_tuple
, start
;
546 struct type
*variant_type
;
547 struct disr_info disr
;
548 struct value_print_options opts
;
549 struct cleanup
*cleanup
;
554 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
556 cleanup
= make_cleanup (xfree
, disr
.name
);
558 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
560 fprintf_filtered (stream
, "%s", disr
.name
);
565 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
566 nfields
= TYPE_NFIELDS (variant_type
);
568 is_tuple
= (disr
.is_encoded
569 ? rust_tuple_struct_type_p (variant_type
)
570 : rust_tuple_variant_type_p (variant_type
));
571 start
= disr
.is_encoded
? 0 : 1;
575 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
577 fprintf_filtered (stream
, "%s(", disr
.name
);
580 /* struct variant. */
581 fprintf_filtered (stream
, "%s{", disr
.name
);
586 /* In case of a nullary variant like 'None', just output
588 fprintf_filtered (stream
, "%s", disr
.name
);
592 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
595 fputs_filtered (", ", stream
);
599 fprintf_filtered (stream
, "%s: ",
600 TYPE_FIELD_NAME (variant_type
, j
));
602 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
605 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
606 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
608 stream
, recurse
+ 1, val
, &opts
,
613 fputs_filtered (")", stream
);
615 fputs_filtered ("}", stream
);
618 do_cleanups (cleanup
);
622 case TYPE_CODE_STRUCT
:
626 int is_tuple
= rust_tuple_type_p (type
);
627 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
628 struct value_print_options opts
;
632 if (TYPE_TAG_NAME (type
) != NULL
)
633 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
635 if (TYPE_NFIELDS (type
) == 0)
638 if (TYPE_TAG_NAME (type
) != NULL
)
639 fputs_filtered (" ", stream
);
642 if (is_tuple
|| is_tuple_struct
)
643 fputs_filtered ("(", stream
);
645 fputs_filtered ("{", stream
);
651 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
653 if (field_is_static (&TYPE_FIELD (type
, i
)))
657 fputs_filtered (",", stream
);
659 if (options
->prettyformat
)
661 fputs_filtered ("\n", stream
);
662 print_spaces_filtered (2 + 2 * recurse
, stream
);
664 else if (!first_field
)
665 fputs_filtered (" ", stream
);
669 if (!is_tuple
&& !is_tuple_struct
)
671 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
672 fputs_filtered (": ", stream
);
675 val_print (TYPE_FIELD_TYPE (type
, i
),
677 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
679 stream
, recurse
+ 1, val
, &opts
,
683 if (options
->prettyformat
)
685 fputs_filtered ("\n", stream
);
686 print_spaces_filtered (2 * recurse
, stream
);
689 if (is_tuple
|| is_tuple_struct
)
690 fputs_filtered (")", stream
);
692 fputs_filtered ("}", stream
);
698 /* Nothing special yet. */
699 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
700 recurse
, val
, options
, &rust_decorations
);
706 /* la_print_typedef implementation for Rust. */
709 rust_print_typedef (struct type
*type
,
710 struct symbol
*new_symbol
,
711 struct ui_file
*stream
)
713 type
= check_typedef (type
);
714 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
715 type_print (type
, "", stream
, 0);
716 fprintf_filtered (stream
, ";\n");
719 /* la_print_type implementation for Rust. */
722 rust_print_type (struct type
*type
, const char *varstring
,
723 struct ui_file
*stream
, int show
, int level
,
724 const struct type_print_options
*flags
)
730 && TYPE_NAME (type
) != NULL
)
732 /* Rust calls the unit type "void" in its debuginfo,
733 but we don't want to print it as that. */
734 if (TYPE_CODE (type
) == TYPE_CODE_VOID
)
735 fputs_filtered ("()", stream
);
737 fputs_filtered (TYPE_NAME (type
), stream
);
741 type
= check_typedef (type
);
742 switch (TYPE_CODE (type
))
745 fputs_filtered ("()", stream
);
749 /* Delegate varargs to the C printer. */
750 if (TYPE_VARARGS (type
))
753 fputs_filtered ("fn ", stream
);
754 if (varstring
!= NULL
)
755 fputs_filtered (varstring
, stream
);
756 fputs_filtered ("(", stream
);
757 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
761 fputs_filtered (", ", stream
);
762 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
765 fputs_filtered (")", stream
);
766 /* If it returns unit, we can omit the return type. */
767 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) != TYPE_CODE_VOID
)
769 fputs_filtered (" -> ", stream
);
770 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
774 case TYPE_CODE_ARRAY
:
776 LONGEST low_bound
, high_bound
;
778 fputs_filtered ("[", stream
);
779 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
780 stream
, show
- 1, level
, flags
);
781 fputs_filtered ("; ", stream
);
783 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
784 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
785 fprintf_filtered (stream
, "variable length");
786 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
787 fprintf_filtered (stream
, "%s",
788 plongest (high_bound
- low_bound
+ 1));
789 fputs_filtered ("]", stream
);
793 case TYPE_CODE_STRUCT
:
797 /* Print a tuple type simply. */
798 if (rust_tuple_type_p (type
))
800 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
804 /* If we see a base class, delegate to C. */
805 if (TYPE_N_BASECLASSES (type
) > 0)
808 fputs_filtered ("struct ", stream
);
809 if (TYPE_TAG_NAME (type
) != NULL
)
810 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
812 is_tuple_struct
= rust_tuple_struct_type_p (type
);
814 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
816 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
818 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
823 if (field_is_static (&TYPE_FIELD (type
, i
)))
826 /* We'd like to print "pub" here as needed, but rustc
827 doesn't emit the debuginfo, and our types don't have
828 cplus_struct_type attached. */
830 /* For a tuple struct we print the type but nothing
832 print_spaces_filtered (level
+ 2, stream
);
833 if (!is_tuple_struct
)
834 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
836 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
837 stream
, show
- 1, level
+ 2,
839 fputs_filtered (",\n", stream
);
842 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
850 fputs_filtered ("enum ", stream
);
851 if (TYPE_TAG_NAME (type
) != NULL
)
853 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
854 fputs_filtered (" ", stream
);
855 len
= strlen (TYPE_TAG_NAME (type
));
857 fputs_filtered ("{\n", stream
);
859 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
861 const char *name
= TYPE_FIELD_NAME (type
, i
);
866 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
868 && name
[len
+ 1] == ':')
870 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
873 fputs_filtered ("}", stream
);
877 case TYPE_CODE_UNION
:
881 /* Skip the discriminant field. */
884 fputs_filtered ("enum ", stream
);
885 if (TYPE_TAG_NAME (type
) != NULL
)
887 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
888 fputs_filtered (" ", stream
);
889 len
= strlen (TYPE_TAG_NAME (type
));
891 fputs_filtered ("{\n", stream
);
893 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
894 strlen (RUST_ENUM_PREFIX
)) == 0)
896 const char *zero_field
= strrchr (TYPE_FIELD_NAME (type
, 0), '$');
897 if (zero_field
!= NULL
&& strlen (zero_field
) > 1)
899 fprintfi_filtered (level
+ 2, stream
, "%s,\n", zero_field
+ 1);
900 /* There is no explicit discriminant field, skip nothing. */
905 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
907 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
909 = rust_last_path_segment (TYPE_NAME (variant_type
));
911 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
913 if (TYPE_NFIELDS (variant_type
) > skip_to
)
916 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
919 fputs_filtered (is_tuple
? "(" : "{", stream
);
920 for (j
= skip_to
; j
< TYPE_NFIELDS (variant_type
); j
++)
925 fputs_filtered (", ", stream
);
928 fprintf_filtered (stream
, "%s: ",
929 TYPE_FIELD_NAME (variant_type
, j
));
931 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
932 stream
, show
- 1, level
+ 2,
935 fputs_filtered (is_tuple
? ")" : "}", stream
);
938 fputs_filtered (",\n", stream
);
941 fputs_filtered ("}", stream
);
947 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
953 /* Compute the alignment of the type T. */
956 rust_type_alignment (struct type
*t
)
958 t
= check_typedef (t
);
959 switch (TYPE_CODE (t
))
962 error (_("Could not compute alignment of type"));
971 return TYPE_LENGTH (t
);
973 case TYPE_CODE_ARRAY
:
974 case TYPE_CODE_COMPLEX
:
975 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
977 case TYPE_CODE_STRUCT
:
978 case TYPE_CODE_UNION
:
983 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
985 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
994 /* Like arch_composite_type, but uses TYPE to decide how to allocate
995 -- either on an obstack or on a gdbarch. */
998 rust_composite_type (struct type
*original
,
1000 const char *field1
, struct type
*type1
,
1001 const char *field2
, struct type
*type2
)
1003 struct type
*result
= alloc_type_copy (original
);
1004 int i
, nfields
, bitpos
;
1012 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
1013 TYPE_NAME (result
) = name
;
1014 TYPE_TAG_NAME (result
) = name
;
1016 TYPE_NFIELDS (result
) = nfields
;
1017 TYPE_FIELDS (result
)
1018 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
1024 struct field
*field
= &TYPE_FIELD (result
, i
);
1026 SET_FIELD_BITPOS (*field
, bitpos
);
1027 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
1029 FIELD_NAME (*field
) = field1
;
1030 FIELD_TYPE (*field
) = type1
;
1035 struct field
*field
= &TYPE_FIELD (result
, i
);
1036 int align
= rust_type_alignment (type2
);
1042 align
*= TARGET_CHAR_BIT
;
1043 delta
= bitpos
% align
;
1045 bitpos
+= align
- delta
;
1047 SET_FIELD_BITPOS (*field
, bitpos
);
1049 FIELD_NAME (*field
) = field2
;
1050 FIELD_TYPE (*field
) = type2
;
1055 TYPE_LENGTH (result
)
1056 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1057 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1061 /* See rust-lang.h. */
1064 rust_slice_type (const char *name
, struct type
*elt_type
,
1065 struct type
*usize_type
)
1069 elt_type
= lookup_pointer_type (elt_type
);
1070 type
= rust_composite_type (elt_type
, name
,
1071 "data_ptr", elt_type
,
1072 "length", usize_type
);
1077 enum rust_primitive_types
1079 rust_primitive_bool
,
1080 rust_primitive_char
,
1089 rust_primitive_isize
,
1090 rust_primitive_usize
,
1093 rust_primitive_unit
,
1095 nr_rust_primitive_types
1098 /* la_language_arch_info implementation for Rust. */
1101 rust_language_arch_info (struct gdbarch
*gdbarch
,
1102 struct language_arch_info
*lai
)
1104 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1106 struct type
**types
;
1107 unsigned int length
;
1109 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1112 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1113 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1114 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1115 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1116 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1117 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1118 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1119 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1120 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1121 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1123 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1124 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1125 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1127 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1128 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1130 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1132 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1133 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1134 types
[rust_primitive_usize
]);
1136 lai
->primitive_type_vector
= types
;
1137 lai
->bool_type_default
= types
[rust_primitive_bool
];
1138 lai
->string_char_type
= types
[rust_primitive_u8
];
1143 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1145 static struct value
*
1146 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1149 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1152 struct value
*function
, *result
, *arg0
;
1153 struct value
**args
;
1154 struct cleanup
*cleanup
;
1155 struct type
*type
, *fn_type
;
1156 const struct block
*block
;
1157 struct block_symbol sym
;
1159 /* For an ordinary function call we can simply defer to the
1160 generic implementation. */
1161 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1162 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1164 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1166 method
= &exp
->elts
[*pos
+ 1].string
;
1167 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1169 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1170 type in order to look up the method. */
1171 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1173 if (noside
== EVAL_SKIP
)
1175 for (i
= 0; i
< num_args
; ++i
)
1176 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1180 args
= XNEWVEC (struct value
*, num_args
+ 1);
1181 cleanup
= make_cleanup (xfree
, args
);
1184 /* We don't yet implement real Deref semantics. */
1185 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1186 args
[0] = value_ind (args
[0]);
1188 type
= value_type (args
[0]);
1189 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1190 && TYPE_CODE (type
) != TYPE_CODE_UNION
1191 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1192 || rust_tuple_type_p (type
))
1193 error (_("Method calls only supported on struct or enum types"));
1194 if (TYPE_TAG_NAME (type
) == NULL
)
1195 error (_("Method call on nameless type"));
1197 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1198 make_cleanup (xfree
, name
);
1200 block
= get_selected_block (0);
1201 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1202 if (sym
.symbol
== NULL
)
1203 error (_("Could not find function named '%s'"), name
);
1205 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1206 if (TYPE_NFIELDS (fn_type
) == 0)
1207 error (_("Function '%s' takes no arguments"), name
);
1209 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1210 args
[0] = value_addr (args
[0]);
1212 function
= address_of_variable (sym
.symbol
, block
);
1214 for (i
= 0; i
< num_args
; ++i
)
1215 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1217 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1218 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1220 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1221 do_cleanups (cleanup
);
1225 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1227 static struct value
*
1228 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1230 enum range_type kind
;
1231 struct value
*low
= NULL
, *high
= NULL
;
1232 struct value
*addrval
, *result
;
1234 struct type
*range_type
;
1235 struct type
*index_type
;
1236 struct type
*temp_type
;
1239 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1242 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1243 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1244 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1245 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1247 if (noside
== EVAL_SKIP
)
1248 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1255 name
= "std::ops::RangeFull";
1259 index_type
= value_type (high
);
1260 name
= "std::ops::RangeTo";
1267 index_type
= value_type (low
);
1268 name
= "std::ops::RangeFrom";
1272 if (!types_equal (value_type (low
), value_type (high
)))
1273 error (_("Range expression with different types"));
1274 index_type
= value_type (low
);
1275 name
= "std::ops::Range";
1279 /* If we don't have an index type, just allocate this on the
1280 arch. Here any type will do. */
1281 temp_type
= (index_type
== NULL
1282 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1284 /* It would be nicer to cache the range type. */
1285 range_type
= rust_composite_type (temp_type
, name
,
1286 low
== NULL
? NULL
: "start", index_type
,
1287 high
== NULL
? NULL
: "end", index_type
);
1289 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1290 return value_zero (range_type
, lval_memory
);
1292 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1293 addr
= value_as_long (addrval
);
1294 result
= value_at_lazy (range_type
, addr
);
1298 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1301 value_assign (start
, low
);
1306 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1309 value_assign (end
, high
);
1312 result
= value_at_lazy (range_type
, addr
);
1316 /* A helper function to compute the range and kind given a range
1317 value. TYPE is the type of the range value. RANGE is the range
1318 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1319 parameters might be filled in, or might not be, depending on the
1320 kind of range this is. KIND will always be set to the appropriate
1321 value describing the kind of range, and this can be used to
1322 determine whether LOW or HIGH are valid. */
1325 rust_compute_range (struct type
*type
, struct value
*range
,
1326 LONGEST
*low
, LONGEST
*high
,
1327 enum range_type
*kind
)
1333 *kind
= BOTH_BOUND_DEFAULT
;
1335 if (TYPE_NFIELDS (type
) == 0)
1339 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1341 *kind
= HIGH_BOUND_DEFAULT
;
1342 *low
= value_as_long (value_field (range
, 0));
1345 if (TYPE_NFIELDS (type
) > i
1346 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1348 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1349 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1350 *high
= value_as_long (value_field (range
, i
));
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 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1388 struct type
*type
= check_typedef (value_type (lhs
));
1390 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1396 struct type
*type
= check_typedef (value_type (lhs
));
1398 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1401 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1402 error (_("Can't compute array bounds"));
1404 error (_("Found array with non-zero lower bound"));
1407 else if (rust_slice_type_p (type
))
1411 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1412 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1414 high_bound
= value_as_long (len
);
1417 error (_("Cannot subscript non-array type"));
1420 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1423 error (_("Index less than zero"));
1424 if (low
> high_bound
)
1425 error (_("Index greater than length"));
1427 result
= value_subscript (base
, low
);
1434 struct type
*usize
, *slice
;
1436 struct value
*addrval
, *tem
;
1438 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1441 error (_("High index less than zero"));
1443 error (_("Low index greater than high index"));
1444 if (high
> high_bound
)
1445 error (_("High index greater than length"));
1447 usize
= language_lookup_primitive_type (exp
->language_defn
,
1450 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1453 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1454 addr
= value_as_long (addrval
);
1455 tem
= value_at_lazy (slice
, addr
);
1457 value_assign (value_field (tem
, 0), value_addr (result
));
1458 value_assign (value_field (tem
, 1),
1459 value_from_longest (usize
, high
- low
));
1461 result
= value_at_lazy (slice
, addr
);
1464 result
= value_addr (result
);
1470 /* evaluate_exp implementation for Rust. */
1472 static struct value
*
1473 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1474 int *pos
, enum noside noside
)
1476 struct value
*result
;
1478 switch (exp
->elts
[*pos
].opcode
)
1480 case UNOP_COMPLEMENT
:
1482 struct value
*value
;
1485 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1486 if (noside
== EVAL_SKIP
)
1488 /* Preserving the type is enough. */
1491 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1492 result
= value_from_longest (value_type (value
),
1493 value_logical_not (value
));
1495 result
= value_complement (value
);
1499 case BINOP_SUBSCRIPT
:
1500 result
= rust_subscript (exp
, pos
, noside
, 0);
1504 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1510 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1511 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1514 struct value
*addrval
= NULL
;
1518 if (noside
== EVAL_NORMAL
)
1520 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1521 addr
= value_as_long (addrval
);
1522 result
= value_at_lazy (type
, addr
);
1525 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1530 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1531 if (noside
== EVAL_NORMAL
)
1533 /* This isn't quite right but will do for the time
1534 being, seeing that we can't implement the Copy
1536 value_assign (result
, init
);
1542 gdb_assert (arglen
% 2 == 0);
1543 for (i
= 0; i
< arglen
; i
+= 2)
1546 const char *fieldname
;
1547 struct value
*value
, *field
;
1549 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1551 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1553 fieldname
= &exp
->elts
[*pos
].string
;
1554 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1556 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1557 if (noside
== EVAL_NORMAL
)
1559 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1561 value_assign (field
, value
);
1565 if (noside
== EVAL_SKIP
)
1566 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1568 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1569 result
= allocate_value (type
);
1571 result
= value_at_lazy (type
, addr
);
1580 struct value
*ncopies
;
1582 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1583 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1584 copies
= value_as_long (ncopies
);
1586 error (_("Array with negative number of elements"));
1588 if (noside
== EVAL_NORMAL
)
1592 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1593 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1595 for (i
= 0; i
< copies
; ++i
)
1597 result
= value_array (0, copies
- 1, eltvec
);
1599 do_cleanups (cleanup
);
1603 struct type
*arraytype
1604 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1605 result
= allocate_value (arraytype
);
1610 case STRUCTOP_ANONYMOUS
:
1612 /* Anonymous field access, i.e. foo.1. */
1614 int pc
, field_number
, nfields
;
1615 struct type
*type
, *variant_type
;
1616 struct disr_info disr
;
1619 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1621 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1623 type
= value_type (lhs
);
1624 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1626 struct cleanup
*cleanup
;
1628 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1629 value_embedded_offset (lhs
),
1630 value_address (lhs
), lhs
);
1632 cleanup
= make_cleanup (xfree
, disr
.name
);
1634 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1636 variant_type
= NULL
;
1641 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1642 nfields
= TYPE_NFIELDS (variant_type
);
1645 if (!disr
.is_encoded
)
1648 if (field_number
>= nfields
|| field_number
< 0)
1649 error(_("Cannot access field %d of variant %s, \
1650 there are only %d fields"),
1651 disr
.is_encoded
? field_number
: field_number
- 1,
1653 disr
.is_encoded
? nfields
: nfields
- 1);
1655 if (!(disr
.is_encoded
1656 ? rust_tuple_struct_type_p (variant_type
)
1657 : rust_tuple_variant_type_p (variant_type
)))
1658 error(_("Variant %s is not a tuple variant"), disr
.name
);
1660 result
= value_primitive_field (lhs
, 0, field_number
,
1662 do_cleanups (cleanup
);
1664 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1666 /* Tuples and tuple structs */
1667 nfields
= TYPE_NFIELDS(type
);
1669 if (field_number
>= nfields
|| field_number
< 0)
1670 error(_("Cannot access field %d of %s, there are only %d fields"),
1671 field_number
, TYPE_TAG_NAME (type
), nfields
);
1673 /* Tuples are tuple structs too. */
1674 if (!rust_tuple_struct_type_p (type
))
1675 error(_("Attempting to access anonymous field %d of %s, which is \
1676 not a tuple, tuple struct, or tuple-like variant"),
1677 field_number
, TYPE_TAG_NAME (type
));
1679 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1682 error(_("Anonymous field access is only allowed on tuples, \
1683 tuple structs, and tuple-like enum variants"));
1687 case STRUCTOP_STRUCT
:
1694 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1695 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1696 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1698 type
= value_type (lhs
);
1700 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1703 struct disr_info disr
;
1704 struct cleanup
* cleanup
;
1705 struct type
* variant_type
;
1708 field_name
= &exp
->elts
[pc
+ 2].string
;
1710 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1711 value_embedded_offset (lhs
),
1712 value_address (lhs
), lhs
);
1714 cleanup
= make_cleanup (xfree
, disr
.name
);
1716 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1717 error(_("Could not find field %s of struct variant %s"),
1718 field_name
, disr
.name
);
1720 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1722 if (variant_type
== NULL
1723 || rust_tuple_variant_type_p (variant_type
))
1724 error(_("Attempting to access named field %s of tuple variant %s, \
1725 which has only anonymous fields"),
1726 field_name
, disr
.name
);
1728 start
= disr
.is_encoded
? 0 : 1;
1729 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1731 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1733 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1738 if (i
== TYPE_NFIELDS (variant_type
))
1739 /* We didn't find it. */
1740 error(_("Could not find field %s of struct variant %s"),
1741 field_name
, disr
.name
);
1743 do_cleanups (cleanup
);
1748 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1754 result
= rust_range (exp
, pos
, noside
);
1758 /* We might have &array[range], in which case we need to make a
1760 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1763 result
= rust_subscript (exp
, pos
, noside
, 1);
1768 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1775 /* operator_length implementation for Rust. */
1778 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1784 switch (exp
->elts
[pc
- 1].opcode
)
1787 /* We handle aggregate as a type and argument count. The first
1788 argument might be OP_OTHERS. After that the arguments
1789 alternate: first an OP_NAME, then an expression. */
1791 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1799 case STRUCTOP_ANONYMOUS
:
1810 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1818 /* op_name implementation for Rust. */
1821 rust_op_name (enum exp_opcode opcode
)
1826 return "OP_AGGREGATE";
1830 return op_name_standard (opcode
);
1834 /* dump_subexp_body implementation for Rust. */
1837 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1840 switch (exp
->elts
[elt
].opcode
)
1844 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1847 fprintf_filtered (stream
, "Type @");
1848 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1849 fprintf_filtered (stream
, " (");
1850 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1851 fprintf_filtered (stream
, "), length %d", length
);
1854 for (i
= 0; i
< length
; ++i
)
1855 elt
= dump_subexp (exp
, stream
, elt
);
1862 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1864 fprintf_filtered (stream
, "%s: %s",
1865 (exp
->elts
[elt
].opcode
== OP_STRING
1866 ? "string" : "name"),
1867 &exp
->elts
[elt
+ 2].string
);
1868 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1873 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1876 case STRUCTOP_ANONYMOUS
:
1880 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1882 fprintf_filtered (stream
, "Field number: %d", field_number
);
1883 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1891 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1898 /* print_subexp implementation for Rust. */
1901 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1902 enum precedence prec
)
1904 switch (exp
->elts
[*pos
].opcode
)
1908 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1911 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1912 fputs_filtered (" { ", stream
);
1915 for (i
= 0; i
< length
; ++i
)
1917 rust_print_subexp (exp
, pos
, stream
, prec
);
1918 fputs_filtered (", ", stream
);
1920 fputs_filtered (" }", stream
);
1926 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1928 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1929 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1935 fputs_filtered ("<<others>> (", stream
);
1937 rust_print_subexp (exp
, pos
, stream
, prec
);
1938 fputs_filtered (")", stream
);
1942 case STRUCTOP_ANONYMOUS
:
1944 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1947 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1948 fprintf_filtered (stream
, ".%d", tem
);
1954 fprintf_filtered (stream
, "[");
1955 rust_print_subexp (exp
, pos
, stream
, prec
);
1956 fprintf_filtered (stream
, "; ");
1957 rust_print_subexp (exp
, pos
, stream
, prec
);
1958 fprintf_filtered (stream
, "]");
1962 print_subexp_standard (exp
, pos
, stream
, prec
);
1967 /* operator_check implementation for Rust. */
1970 rust_operator_check (struct expression
*exp
, int pos
,
1971 int (*objfile_func
) (struct objfile
*objfile
,
1975 switch (exp
->elts
[pos
].opcode
)
1979 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1980 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1982 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1993 return operator_check_standard (exp
, pos
, objfile_func
, data
);
2001 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2003 static struct block_symbol
2004 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
2006 const struct block
*block
,
2007 const domain_enum domain
)
2009 struct block_symbol result
= {NULL
, NULL
};
2011 if (symbol_lookup_debug
)
2013 fprintf_unfiltered (gdb_stdlog
,
2014 "rust_lookup_symbol_non_local"
2015 " (%s, %s (scope %s), %s)\n",
2016 name
, host_address_to_string (block
),
2017 block_scope (block
), domain_name (domain
));
2020 /* Look up bare names in the block's scope. */
2021 if (name
[cp_find_first_component (name
)] == '\0')
2023 const char *scope
= block_scope (block
);
2025 if (scope
[0] != '\0')
2027 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
2028 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
2030 result
= lookup_symbol_in_static_block (scopedname
, block
,
2032 if (result
.symbol
== NULL
)
2033 result
= lookup_global_symbol (scopedname
, block
, domain
);
2034 do_cleanups (cleanup
);
2042 /* la_sniff_from_mangled_name for Rust. */
2045 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
2047 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
2048 return *demangled
!= NULL
;
2053 static const struct exp_descriptor exp_descriptor_rust
=
2056 rust_operator_length
,
2057 rust_operator_check
,
2059 rust_dump_subexp_body
,
2060 rust_evaluate_subexp
2063 static const char *rust_extensions
[] =
2068 static const struct language_defn rust_language_defn
=
2078 &exp_descriptor_rust
,
2082 rust_printchar
, /* Print a character constant */
2083 rust_printstr
, /* Function to print string constant */
2084 rust_emitchar
, /* Print a single char */
2085 rust_print_type
, /* Print a type using appropriate syntax */
2086 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2087 rust_val_print
, /* Print a value using appropriate syntax */
2088 c_value_print
, /* Print a top-level value */
2089 default_read_var_value
, /* la_read_var_value */
2090 NULL
, /* Language specific skip_trampoline */
2091 NULL
, /* name_of_this */
2092 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2093 basic_lookup_transparent_type
,/* lookup_transparent_type */
2094 gdb_demangle
, /* Language specific symbol demangler */
2095 rust_sniff_from_mangled_name
,
2096 NULL
, /* Language specific
2097 class_name_from_physname */
2098 c_op_print_tab
, /* expression operators for printing */
2099 1, /* c-style arrays */
2100 0, /* String lower bound */
2101 default_word_break_characters
,
2102 default_make_symbol_completion_list
,
2103 rust_language_arch_info
,
2104 default_print_array_index
,
2105 default_pass_by_reference
,
2107 NULL
, /* la_get_symbol_name_cmp */
2108 iterate_over_symbols
,
2109 &default_varobj_ops
,
2116 _initialize_rust_language (void)
2118 add_language (&rust_language_defn
);