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)
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 fieldno
= strtoul (TYPE_FIELD_NAME (type
, 0) + strlen (RUST_ENUM_PREFIX
),
137 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
139 member_type
= TYPE_FIELD_TYPE (type
, 0);
140 if (fieldno
>= TYPE_NFIELDS (member_type
))
141 error (_("%s refers to field after end of member type"),
144 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
145 value
= unpack_long (TYPE_FIELD_TYPE (member_type
, fieldno
),
146 valaddr
+ embedded_offset
);
149 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
150 ret
.name
= concat (TYPE_NAME (type
), "::", tail
+ 1, (char *) NULL
);
154 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
155 ret
.name
= concat (TYPE_NAME (type
), "::",
156 rust_last_path_segment (TYPE_NAME (member_type
)),
163 disr_type
= TYPE_FIELD_TYPE (type
, 0);
165 if (TYPE_NFIELDS (disr_type
) == 0)
167 /* This is a bounds check and should never be hit unless Rust
168 has changed its debuginfo format. */
169 error (_("Could not find enum discriminant field"));
172 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
173 error (_("Rust debug format has changed"));
175 temp_file
= mem_fileopen ();
176 cleanup
= make_cleanup_ui_file_delete (temp_file
);
177 /* The first value of the first field (or any field)
178 is the discriminant value. */
179 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
180 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
181 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
185 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
186 name_segment
= rust_last_path_segment (ret
.name
);
187 if (name_segment
!= NULL
)
189 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
191 /* Sadly, the discriminant value paths do not match the type
192 field name paths ('core::option::Option::Some' vs
193 'core::option::Some'). However, enum variant names are
194 unique in the last path segment and the generics are not
195 part of this path, so we can just compare those. This is
196 hackish and would be better fixed by improving rustc's
197 metadata for enums. */
198 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
200 if (field_type
!= NULL
201 && strcmp (name_segment
,
202 rust_last_path_segment (field_type
)) == 0)
210 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
212 /* Somehow the discriminant wasn't found. */
213 make_cleanup (xfree
, ret
.name
);
214 error (_("Could not find variant of %s with discriminant %s"),
215 TYPE_TAG_NAME (type
), ret
.name
);
218 do_cleanups (cleanup
);
222 /* See rust-lang.h. */
225 rust_tuple_type_p (struct type
*type
)
227 /* The current implementation is a bit of a hack, but there's
228 nothing else in the debuginfo to distinguish a tuple from a
230 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
231 && TYPE_TAG_NAME (type
) != NULL
232 && TYPE_TAG_NAME (type
)[0] == '(');
236 /* Return true if all non-static fields of a structlike type are in a
237 sequence like __0, __1, __2. OFFSET lets us skip fields. */
240 rust_underscore_fields (struct type
*type
, int offset
)
246 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
248 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
250 if (!field_is_static (&TYPE_FIELD (type
, i
)))
258 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
259 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
268 /* See rust-lang.h. */
271 rust_tuple_struct_type_p (struct type
*type
)
273 return rust_underscore_fields (type
, 0);
276 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
279 rust_tuple_variant_type_p (struct type
*type
)
281 /* First field is discriminant */
282 return rust_underscore_fields (type
, 1);
285 /* Return true if TYPE is a slice type, otherwise false. */
288 rust_slice_type_p (struct type
*type
)
290 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
291 && TYPE_TAG_NAME (type
) != NULL
292 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
295 /* Return true if TYPE is a range type, otherwise false. */
298 rust_range_type_p (struct type
*type
)
302 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
303 || TYPE_NFIELDS (type
) > 2
304 || TYPE_TAG_NAME (type
) == NULL
305 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
308 if (TYPE_NFIELDS (type
) == 0)
312 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
314 if (TYPE_NFIELDS (type
) == 1)
318 else if (TYPE_NFIELDS (type
) == 2)
320 /* First field had to be "start". */
324 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
327 /* Return true if TYPE seems to be the type "u8", otherwise false. */
330 rust_u8_type_p (struct type
*type
)
332 return (TYPE_CODE (type
) == TYPE_CODE_INT
333 && TYPE_UNSIGNED (type
)
334 && TYPE_LENGTH (type
) == 1);
337 /* Return true if TYPE is a Rust character type. */
340 rust_chartype_p (struct type
*type
)
342 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
343 && TYPE_LENGTH (type
) == 4
344 && TYPE_UNSIGNED (type
));
349 /* la_emitchar implementation for Rust. */
352 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
354 if (!rust_chartype_p (type
))
355 generic_emit_char (c
, type
, stream
, quoter
,
356 target_charset (get_type_arch (type
)));
357 else if (c
== '\\' || c
== quoter
)
358 fprintf_filtered (stream
, "\\%c", c
);
360 fputs_filtered ("\\n", stream
);
362 fputs_filtered ("\\r", stream
);
364 fputs_filtered ("\\t", stream
);
366 fputs_filtered ("\\0", stream
);
367 else if (c
>= 32 && c
<= 127 && isprint (c
))
368 fputc_filtered (c
, stream
);
370 fprintf_filtered (stream
, "\\x%02x", c
);
372 fprintf_filtered (stream
, "\\u{%06x}", c
);
375 /* la_printchar implementation for Rust. */
378 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
380 fputs_filtered ("'", stream
);
381 LA_EMIT_CHAR (c
, type
, stream
, '\'');
382 fputs_filtered ("'", stream
);
385 /* la_printstr implementation for Rust. */
388 rust_printstr (struct ui_file
*stream
, struct type
*type
,
389 const gdb_byte
*string
, unsigned int length
,
390 const char *user_encoding
, int force_ellipses
,
391 const struct value_print_options
*options
)
393 /* Rust always uses UTF-8, but let the caller override this if need
395 const char *encoding
= user_encoding
;
396 if (user_encoding
== NULL
|| !*user_encoding
)
398 /* In Rust strings, characters are "u8". */
399 if (rust_u8_type_p (type
))
403 /* This is probably some C string, so let's let C deal with
405 c_printstr (stream
, type
, string
, length
, user_encoding
,
406 force_ellipses
, options
);
411 /* This is not ideal as it doesn't use our character printer. */
412 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
418 static const struct generic_val_print_decorations rust_decorations
=
420 /* Complex isn't used in Rust, but we provide C-ish values just in
432 /* la_val_print implementation for Rust. */
435 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
436 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
437 const struct value
*val
,
438 const struct value_print_options
*options
)
440 type
= check_typedef (type
);
441 switch (TYPE_CODE (type
))
445 LONGEST low_bound
, high_bound
;
447 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
448 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
449 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
451 /* We have a pointer to a byte string, so just print
453 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
455 struct gdbarch
*arch
= get_type_arch (type
);
456 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
458 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
459 if (options
->addressprint
)
461 fputs_filtered (paddress (arch
, addr
), stream
);
462 fputs_filtered (" ", stream
);
465 fputs_filtered ("b", stream
);
466 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
467 high_bound
- low_bound
+ 1, stream
,
474 case TYPE_CODE_METHODPTR
:
475 case TYPE_CODE_MEMBERPTR
:
476 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
477 recurse
, val
, options
);
481 /* Recognize the unit type. */
482 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
483 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
485 fputs_filtered ("()", stream
);
490 case TYPE_CODE_STRING
:
492 struct gdbarch
*arch
= get_type_arch (type
);
493 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
494 LONGEST low_bound
, high_bound
;
496 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
497 error (_("Could not determine the array bounds"));
499 /* If we see a plain TYPE_CODE_STRING, then we're printing a
500 byte string, hence the choice of "ASCII" as the
502 fputs_filtered ("b", stream
);
503 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
504 valaddr
+ embedded_offset
* unit_size
,
505 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
509 case TYPE_CODE_ARRAY
:
511 LONGEST low_bound
, high_bound
;
513 if (get_array_bounds (type
, &low_bound
, &high_bound
)
514 && high_bound
- low_bound
+ 1 == 0)
515 fputs_filtered ("[]", stream
);
521 case TYPE_CODE_UNION
:
523 int j
, nfields
, first_field
, is_tuple
, start
;
524 struct type
*variant_type
;
525 struct disr_info disr
;
526 struct value_print_options opts
;
527 struct cleanup
*cleanup
;
532 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
534 cleanup
= make_cleanup (xfree
, disr
.name
);
536 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
538 fprintf_filtered (stream
, "%s", disr
.name
);
543 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
544 nfields
= TYPE_NFIELDS (variant_type
);
546 is_tuple
= (disr
.is_encoded
547 ? rust_tuple_struct_type_p (variant_type
)
548 : rust_tuple_variant_type_p (variant_type
));
549 start
= disr
.is_encoded
? 0 : 1;
553 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
555 fprintf_filtered (stream
, "%s(", disr
.name
);
558 /* struct variant. */
559 fprintf_filtered (stream
, "%s{", disr
.name
);
564 /* In case of a nullary variant like 'None', just output
566 fprintf_filtered (stream
, "%s", disr
.name
);
570 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
573 fputs_filtered (", ", stream
);
577 fprintf_filtered (stream
, "%s: ",
578 TYPE_FIELD_NAME (variant_type
, j
));
580 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
583 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
584 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
586 stream
, recurse
+ 1, val
, &opts
,
591 fputs_filtered (")", stream
);
593 fputs_filtered ("}", stream
);
596 do_cleanups (cleanup
);
600 case TYPE_CODE_STRUCT
:
604 int is_tuple
= rust_tuple_type_p (type
);
605 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
606 struct value_print_options opts
;
610 if (TYPE_TAG_NAME (type
) != NULL
)
611 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
613 if (TYPE_NFIELDS (type
) == 0)
616 if (TYPE_TAG_NAME (type
) != NULL
)
617 fputs_filtered (" ", stream
);
620 if (is_tuple
|| is_tuple_struct
)
621 fputs_filtered ("(", stream
);
623 fputs_filtered ("{", stream
);
629 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
631 if (field_is_static (&TYPE_FIELD (type
, i
)))
635 fputs_filtered (",", stream
);
637 if (options
->prettyformat
)
639 fputs_filtered ("\n", stream
);
640 print_spaces_filtered (2 + 2 * recurse
, stream
);
642 else if (!first_field
)
643 fputs_filtered (" ", stream
);
647 if (!is_tuple
&& !is_tuple_struct
)
649 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
650 fputs_filtered (": ", stream
);
653 val_print (TYPE_FIELD_TYPE (type
, i
),
655 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
657 stream
, recurse
+ 1, val
, &opts
,
661 if (options
->prettyformat
)
663 fputs_filtered ("\n", stream
);
664 print_spaces_filtered (2 * recurse
, stream
);
667 if (is_tuple
|| is_tuple_struct
)
668 fputs_filtered (")", stream
);
670 fputs_filtered ("}", stream
);
676 /* Nothing special yet. */
677 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
678 recurse
, val
, options
, &rust_decorations
);
684 /* la_print_typedef implementation for Rust. */
687 rust_print_typedef (struct type
*type
,
688 struct symbol
*new_symbol
,
689 struct ui_file
*stream
)
691 type
= check_typedef (type
);
692 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
693 type_print (type
, "", stream
, 0);
694 fprintf_filtered (stream
, ";\n");
697 /* la_print_type implementation for Rust. */
700 rust_print_type (struct type
*type
, const char *varstring
,
701 struct ui_file
*stream
, int show
, int level
,
702 const struct type_print_options
*flags
)
708 && TYPE_NAME (type
) != NULL
)
710 fputs_filtered (TYPE_NAME (type
), stream
);
714 type
= check_typedef (type
);
715 switch (TYPE_CODE (type
))
718 /* Delegate varargs to the C printer. */
719 if (TYPE_VARARGS (type
))
722 fputs_filtered ("fn ", stream
);
723 if (varstring
!= NULL
)
724 fputs_filtered (varstring
, stream
);
725 fputs_filtered ("(", stream
);
726 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
730 fputs_filtered (", ", stream
);
731 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
734 fputs_filtered (") -> ", stream
);
735 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
738 case TYPE_CODE_ARRAY
:
740 LONGEST low_bound
, high_bound
;
742 fputs_filtered ("[", stream
);
743 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
744 stream
, show
- 1, level
, flags
);
745 fputs_filtered ("; ", stream
);
747 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
748 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
749 fprintf_filtered (stream
, "variable length");
750 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
751 fprintf_filtered (stream
, "%s",
752 plongest (high_bound
- low_bound
+ 1));
753 fputs_filtered ("]", stream
);
757 case TYPE_CODE_STRUCT
:
761 /* Print a tuple type simply. */
762 if (rust_tuple_type_p (type
))
764 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
768 /* If we see a base class, delegate to C. */
769 if (TYPE_N_BASECLASSES (type
) > 0)
772 fputs_filtered ("struct ", stream
);
773 if (TYPE_TAG_NAME (type
) != NULL
)
774 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
776 is_tuple_struct
= rust_tuple_struct_type_p (type
);
778 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
780 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
782 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
787 if (field_is_static (&TYPE_FIELD (type
, i
)))
790 /* We'd like to print "pub" here as needed, but rustc
791 doesn't emit the debuginfo, and our types don't have
792 cplus_struct_type attached. */
794 /* For a tuple struct we print the type but nothing
796 print_spaces_filtered (level
+ 2, stream
);
797 if (!is_tuple_struct
)
798 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
800 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
801 stream
, show
- 1, level
+ 2,
803 fputs_filtered (",\n", stream
);
806 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
814 fputs_filtered ("enum ", stream
);
815 if (TYPE_TAG_NAME (type
) != NULL
)
817 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
818 fputs_filtered (" ", stream
);
819 len
= strlen (TYPE_TAG_NAME (type
));
821 fputs_filtered ("{\n", stream
);
823 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
825 const char *name
= TYPE_FIELD_NAME (type
, i
);
830 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
832 && name
[len
+ 1] == ':')
834 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
837 fputs_filtered ("}", stream
);
841 case TYPE_CODE_UNION
:
846 fputs_filtered ("enum ", stream
);
847 if (TYPE_TAG_NAME (type
) != NULL
)
849 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
850 fputs_filtered (" ", stream
);
851 len
= strlen (TYPE_TAG_NAME (type
));
853 fputs_filtered ("{\n", stream
);
855 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
857 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
859 = rust_last_path_segment (TYPE_NAME (variant_type
));
861 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
863 if (TYPE_NFIELDS (variant_type
) > 1)
866 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
869 fputs_filtered (is_tuple
? "(" : "{", stream
);
870 for (j
= 1; j
< TYPE_NFIELDS (variant_type
); j
++)
875 fputs_filtered (", ", stream
);
878 fprintf_filtered (stream
, "%s: ",
879 TYPE_FIELD_NAME (variant_type
, j
));
881 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
882 stream
, show
- 1, level
+ 2,
885 fputs_filtered (is_tuple
? ")" : "}", stream
);
888 fputs_filtered (",\n", stream
);
891 fputs_filtered ("}", stream
);
897 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
903 /* Compute the alignment of the type T. */
906 rust_type_alignment (struct type
*t
)
908 t
= check_typedef (t
);
909 switch (TYPE_CODE (t
))
912 error (_("Could not compute alignment of type"));
921 return TYPE_LENGTH (t
);
923 case TYPE_CODE_ARRAY
:
924 case TYPE_CODE_COMPLEX
:
925 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
927 case TYPE_CODE_STRUCT
:
928 case TYPE_CODE_UNION
:
933 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
935 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
944 /* Like arch_composite_type, but uses TYPE to decide how to allocate
945 -- either on an obstack or on a gdbarch. */
948 rust_composite_type (struct type
*original
,
950 const char *field1
, struct type
*type1
,
951 const char *field2
, struct type
*type2
)
953 struct type
*result
= alloc_type_copy (original
);
954 int i
, nfields
, bitpos
;
962 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
963 TYPE_NAME (result
) = name
;
964 TYPE_TAG_NAME (result
) = name
;
966 TYPE_NFIELDS (result
) = nfields
;
968 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
974 struct field
*field
= &TYPE_FIELD (result
, i
);
976 SET_FIELD_BITPOS (*field
, bitpos
);
977 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
979 FIELD_NAME (*field
) = field1
;
980 FIELD_TYPE (*field
) = type1
;
985 struct field
*field
= &TYPE_FIELD (result
, i
);
986 int align
= rust_type_alignment (type2
);
992 align
*= TARGET_CHAR_BIT
;
993 delta
= bitpos
% align
;
995 bitpos
+= align
- delta
;
997 SET_FIELD_BITPOS (*field
, bitpos
);
999 FIELD_NAME (*field
) = field2
;
1000 FIELD_TYPE (*field
) = type2
;
1005 TYPE_LENGTH (result
)
1006 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1007 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1011 /* See rust-lang.h. */
1014 rust_slice_type (const char *name
, struct type
*elt_type
,
1015 struct type
*usize_type
)
1019 elt_type
= lookup_pointer_type (elt_type
);
1020 type
= rust_composite_type (elt_type
, name
,
1021 "data_ptr", elt_type
,
1022 "length", usize_type
);
1027 enum rust_primitive_types
1029 rust_primitive_bool
,
1030 rust_primitive_char
,
1039 rust_primitive_isize
,
1040 rust_primitive_usize
,
1043 rust_primitive_unit
,
1045 nr_rust_primitive_types
1048 /* la_language_arch_info implementation for Rust. */
1051 rust_language_arch_info (struct gdbarch
*gdbarch
,
1052 struct language_arch_info
*lai
)
1054 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1056 struct type
**types
;
1057 unsigned int length
;
1059 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1062 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1063 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1064 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1065 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1066 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1067 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1068 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1069 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1070 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1071 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1073 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1074 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1075 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1077 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1078 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1080 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1082 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1083 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1084 types
[rust_primitive_usize
]);
1086 lai
->primitive_type_vector
= types
;
1087 lai
->bool_type_default
= types
[rust_primitive_bool
];
1088 lai
->string_char_type
= types
[rust_primitive_u8
];
1093 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1095 static struct value
*
1096 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1099 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1102 struct value
*function
, *result
, *arg0
;
1103 struct value
**args
;
1104 struct cleanup
*cleanup
;
1105 struct type
*type
, *fn_type
;
1106 const struct block
*block
;
1107 struct block_symbol sym
;
1109 /* For an ordinary function call we can simply defer to the
1110 generic implementation. */
1111 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1112 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1114 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1116 method
= &exp
->elts
[*pos
+ 1].string
;
1117 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1119 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1120 type in order to look up the method. */
1121 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1123 if (noside
== EVAL_SKIP
)
1125 for (i
= 0; i
< num_args
; ++i
)
1126 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1130 args
= XNEWVEC (struct value
*, num_args
+ 1);
1131 cleanup
= make_cleanup (xfree
, args
);
1134 /* We don't yet implement real Deref semantics. */
1135 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1136 args
[0] = value_ind (args
[0]);
1138 type
= value_type (args
[0]);
1139 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1140 && TYPE_CODE (type
) != TYPE_CODE_UNION
1141 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1142 || rust_tuple_type_p (type
))
1143 error (_("Method calls only supported on struct or enum types"));
1144 if (TYPE_TAG_NAME (type
) == NULL
)
1145 error (_("Method call on nameless type"));
1147 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1148 make_cleanup (xfree
, name
);
1150 block
= get_selected_block (0);
1151 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1152 if (sym
.symbol
== NULL
)
1153 error (_("Could not find function named '%s'"), name
);
1155 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1156 if (TYPE_NFIELDS (fn_type
) == 0)
1157 error (_("Function '%s' takes no arguments"), name
);
1159 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1160 args
[0] = value_addr (args
[0]);
1162 function
= address_of_variable (sym
.symbol
, block
);
1164 for (i
= 0; i
< num_args
; ++i
)
1165 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1167 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1168 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1170 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1171 do_cleanups (cleanup
);
1175 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1177 static struct value
*
1178 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1180 enum range_type kind
;
1181 struct value
*low
= NULL
, *high
= NULL
;
1182 struct value
*addrval
, *result
;
1184 struct type
*range_type
;
1185 struct type
*index_type
;
1186 struct type
*temp_type
;
1189 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1192 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1193 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1194 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1195 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1197 if (noside
== EVAL_SKIP
)
1198 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1205 name
= "std::ops::RangeFull";
1209 index_type
= value_type (high
);
1210 name
= "std::ops::RangeTo";
1217 index_type
= value_type (low
);
1218 name
= "std::ops::RangeFrom";
1222 if (!types_equal (value_type (low
), value_type (high
)))
1223 error (_("Range expression with different types"));
1224 index_type
= value_type (low
);
1225 name
= "std::ops::Range";
1229 /* If we don't have an index type, just allocate this on the
1230 arch. Here any type will do. */
1231 temp_type
= (index_type
== NULL
1232 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1234 /* It would be nicer to cache the range type. */
1235 range_type
= rust_composite_type (temp_type
, name
,
1236 low
== NULL
? NULL
: "start", index_type
,
1237 high
== NULL
? NULL
: "end", index_type
);
1239 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1240 return value_zero (range_type
, lval_memory
);
1242 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1243 addr
= value_as_long (addrval
);
1244 result
= value_at_lazy (range_type
, addr
);
1248 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1251 value_assign (start
, low
);
1256 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1259 value_assign (end
, high
);
1262 result
= value_at_lazy (range_type
, addr
);
1266 /* A helper function to compute the range and kind given a range
1267 value. TYPE is the type of the range value. RANGE is the range
1268 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1269 parameters might be filled in, or might not be, depending on the
1270 kind of range this is. KIND will always be set to the appropriate
1271 value describing the kind of range, and this can be used to
1272 determine whether LOW or HIGH are valid. */
1275 rust_compute_range (struct type
*type
, struct value
*range
,
1276 LONGEST
*low
, LONGEST
*high
,
1277 enum range_type
*kind
)
1283 *kind
= BOTH_BOUND_DEFAULT
;
1285 if (TYPE_NFIELDS (type
) == 0)
1289 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1291 *kind
= HIGH_BOUND_DEFAULT
;
1292 *low
= value_as_long (value_field (range
, 0));
1295 if (TYPE_NFIELDS (type
) > i
1296 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1298 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1299 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1300 *high
= value_as_long (value_field (range
, i
));
1304 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1306 static struct value
*
1307 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1310 struct value
*lhs
, *rhs
, *result
;
1311 struct type
*rhstype
;
1312 LONGEST low
, high_bound
;
1313 /* Initialized to appease the compiler. */
1314 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1319 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1320 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1322 if (noside
== EVAL_SKIP
)
1325 rhstype
= check_typedef (value_type (rhs
));
1326 if (rust_range_type_p (rhstype
))
1329 error (_("Can't take slice of array without '&'"));
1330 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1334 low
= value_as_long (rhs
);
1336 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1338 struct type
*type
= check_typedef (value_type (lhs
));
1340 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1346 struct type
*type
= check_typedef (value_type (lhs
));
1348 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1351 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1352 error (_("Can't compute array bounds"));
1354 error (_("Found array with non-zero lower bound"));
1357 else if (rust_slice_type_p (type
))
1361 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1362 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1364 high_bound
= value_as_long (len
);
1367 error (_("Cannot subscript non-array type"));
1370 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1373 error (_("Index less than zero"));
1374 if (low
> high_bound
)
1375 error (_("Index greater than length"));
1377 result
= value_subscript (base
, low
);
1384 struct type
*usize
, *slice
;
1386 struct value
*addrval
, *tem
;
1388 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1391 error (_("High index less than zero"));
1393 error (_("Low index greater than high index"));
1394 if (high
> high_bound
)
1395 error (_("High index greater than length"));
1397 usize
= language_lookup_primitive_type (exp
->language_defn
,
1400 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1403 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1404 addr
= value_as_long (addrval
);
1405 tem
= value_at_lazy (slice
, addr
);
1407 value_assign (value_field (tem
, 0), value_addr (result
));
1408 value_assign (value_field (tem
, 1),
1409 value_from_longest (usize
, high
- low
));
1411 result
= value_at_lazy (slice
, addr
);
1414 result
= value_addr (result
);
1420 /* evaluate_exp implementation for Rust. */
1422 static struct value
*
1423 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1424 int *pos
, enum noside noside
)
1426 struct value
*result
;
1428 switch (exp
->elts
[*pos
].opcode
)
1430 case UNOP_COMPLEMENT
:
1432 struct value
*value
;
1435 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1436 if (noside
== EVAL_SKIP
)
1438 /* Preserving the type is enough. */
1441 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1442 result
= value_from_longest (value_type (value
),
1443 value_logical_not (value
));
1445 result
= value_complement (value
);
1449 case BINOP_SUBSCRIPT
:
1450 result
= rust_subscript (exp
, pos
, noside
, 0);
1454 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1460 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1461 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1464 struct value
*addrval
= NULL
;
1468 if (noside
== EVAL_NORMAL
)
1470 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1471 addr
= value_as_long (addrval
);
1472 result
= value_at_lazy (type
, addr
);
1475 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1480 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1481 if (noside
== EVAL_NORMAL
)
1483 /* This isn't quite right but will do for the time
1484 being, seeing that we can't implement the Copy
1486 value_assign (result
, init
);
1492 gdb_assert (arglen
% 2 == 0);
1493 for (i
= 0; i
< arglen
; i
+= 2)
1496 const char *fieldname
;
1497 struct value
*value
, *field
;
1499 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1501 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1503 fieldname
= &exp
->elts
[*pos
].string
;
1504 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1506 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1507 if (noside
== EVAL_NORMAL
)
1509 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1511 value_assign (field
, value
);
1515 if (noside
== EVAL_SKIP
)
1516 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1518 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1519 result
= allocate_value (type
);
1521 result
= value_at_lazy (type
, addr
);
1530 struct value
*ncopies
;
1532 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1533 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1534 copies
= value_as_long (ncopies
);
1536 error (_("Array with negative number of elements"));
1538 if (noside
== EVAL_NORMAL
)
1542 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1543 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1545 for (i
= 0; i
< copies
; ++i
)
1547 result
= value_array (0, copies
- 1, eltvec
);
1549 do_cleanups (cleanup
);
1553 struct type
*arraytype
1554 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1555 result
= allocate_value (arraytype
);
1560 case STRUCTOP_ANONYMOUS
:
1562 /* Anonymous field access, i.e. foo.1. */
1564 int pc
, field_number
, nfields
;
1565 struct type
*type
, *variant_type
;
1566 struct disr_info disr
;
1569 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1571 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1573 type
= value_type (lhs
);
1574 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1576 struct cleanup
*cleanup
;
1578 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1579 value_embedded_offset (lhs
),
1580 value_address (lhs
), lhs
);
1582 cleanup
= make_cleanup (xfree
, disr
.name
);
1584 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1586 variant_type
= NULL
;
1591 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1592 nfields
= TYPE_NFIELDS (variant_type
);
1595 if (!disr
.is_encoded
)
1598 if (field_number
>= nfields
|| field_number
< 0)
1599 error(_("Cannot access field %d of variant %s, \
1600 there are only %d fields"),
1601 disr
.is_encoded
? field_number
: field_number
- 1,
1603 disr
.is_encoded
? nfields
: nfields
- 1);
1605 if (!(disr
.is_encoded
1606 ? rust_tuple_struct_type_p (variant_type
)
1607 : rust_tuple_variant_type_p (variant_type
)))
1608 error(_("Variant %s is not a tuple variant"), disr
.name
);
1610 result
= value_primitive_field (lhs
, 0, field_number
,
1612 do_cleanups (cleanup
);
1614 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1616 /* Tuples and tuple structs */
1617 nfields
= TYPE_NFIELDS(type
);
1619 if (field_number
>= nfields
|| field_number
< 0)
1620 error(_("Cannot access field %d of %s, there are only %d fields"),
1621 field_number
, TYPE_TAG_NAME (type
), nfields
);
1623 /* Tuples are tuple structs too. */
1624 if (!rust_tuple_struct_type_p (type
))
1625 error(_("Attempting to access anonymous field %d of %s, which is \
1626 not a tuple, tuple struct, or tuple-like variant"),
1627 field_number
, TYPE_TAG_NAME (type
));
1629 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1632 error(_("Anonymous field access is only allowed on tuples, \
1633 tuple structs, and tuple-like enum variants"));
1637 case STRUCTOP_STRUCT
:
1644 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1645 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1646 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1648 type
= value_type (lhs
);
1650 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1653 struct disr_info disr
;
1654 struct cleanup
* cleanup
;
1655 struct type
* variant_type
;
1658 field_name
= &exp
->elts
[pc
+ 2].string
;
1660 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1661 value_embedded_offset (lhs
),
1662 value_address (lhs
), lhs
);
1664 cleanup
= make_cleanup (xfree
, disr
.name
);
1666 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1667 error(_("Could not find field %s of struct variant %s"),
1668 field_name
, disr
.name
);
1670 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1672 if (variant_type
== NULL
1673 || rust_tuple_variant_type_p (variant_type
))
1674 error(_("Attempting to access named field %s of tuple variant %s, \
1675 which has only anonymous fields"),
1676 field_name
, disr
.name
);
1678 start
= disr
.is_encoded
? 0 : 1;
1679 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1681 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1683 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1688 if (i
== TYPE_NFIELDS (variant_type
))
1689 /* We didn't find it. */
1690 error(_("Could not find field %s of struct variant %s"),
1691 field_name
, disr
.name
);
1693 do_cleanups (cleanup
);
1698 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1704 result
= rust_range (exp
, pos
, noside
);
1708 /* We might have &array[range], in which case we need to make a
1710 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1713 result
= rust_subscript (exp
, pos
, noside
, 1);
1718 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1725 /* operator_length implementation for Rust. */
1728 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1734 switch (exp
->elts
[pc
- 1].opcode
)
1737 /* We handle aggregate as a type and argument count. The first
1738 argument might be OP_OTHERS. After that the arguments
1739 alternate: first an OP_NAME, then an expression. */
1741 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1749 case STRUCTOP_ANONYMOUS
:
1760 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1768 /* op_name implementation for Rust. */
1771 rust_op_name (enum exp_opcode opcode
)
1776 return "OP_AGGREGATE";
1780 return op_name_standard (opcode
);
1784 /* dump_subexp_body implementation for Rust. */
1787 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1790 switch (exp
->elts
[elt
].opcode
)
1794 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1797 fprintf_filtered (stream
, "Type @");
1798 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1799 fprintf_filtered (stream
, " (");
1800 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1801 fprintf_filtered (stream
, "), length %d", length
);
1804 for (i
= 0; i
< length
; ++i
)
1805 elt
= dump_subexp (exp
, stream
, elt
);
1812 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1814 fprintf_filtered (stream
, "%s: %s",
1815 (exp
->elts
[elt
].opcode
== OP_STRING
1816 ? "string" : "name"),
1817 &exp
->elts
[elt
+ 2].string
);
1818 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1823 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1826 case STRUCTOP_ANONYMOUS
:
1830 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1832 fprintf_filtered (stream
, "Field number: %d", field_number
);
1833 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1841 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1848 /* print_subexp implementation for Rust. */
1851 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1852 enum precedence prec
)
1854 switch (exp
->elts
[*pos
].opcode
)
1858 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1861 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1862 fputs_filtered (" { ", stream
);
1865 for (i
= 0; i
< length
; ++i
)
1867 rust_print_subexp (exp
, pos
, stream
, prec
);
1868 fputs_filtered (", ", stream
);
1870 fputs_filtered (" }", stream
);
1876 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1878 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1879 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1885 fputs_filtered ("<<others>> (", stream
);
1887 rust_print_subexp (exp
, pos
, stream
, prec
);
1888 fputs_filtered (")", stream
);
1892 case STRUCTOP_ANONYMOUS
:
1894 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1897 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1898 fprintf_filtered (stream
, ".%d", tem
);
1904 fprintf_filtered (stream
, "[");
1905 rust_print_subexp (exp
, pos
, stream
, prec
);
1906 fprintf_filtered (stream
, "; ");
1907 rust_print_subexp (exp
, pos
, stream
, prec
);
1908 fprintf_filtered (stream
, "]");
1912 print_subexp_standard (exp
, pos
, stream
, prec
);
1917 /* operator_check implementation for Rust. */
1920 rust_operator_check (struct expression
*exp
, int pos
,
1921 int (*objfile_func
) (struct objfile
*objfile
,
1925 switch (exp
->elts
[pos
].opcode
)
1929 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1930 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1932 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1943 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1951 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1953 static struct block_symbol
1954 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1956 const struct block
*block
,
1957 const domain_enum domain
)
1959 struct block_symbol result
= {NULL
, NULL
};
1961 if (symbol_lookup_debug
)
1963 fprintf_unfiltered (gdb_stdlog
,
1964 "rust_lookup_symbol_non_local"
1965 " (%s, %s (scope %s), %s)\n",
1966 name
, host_address_to_string (block
),
1967 block_scope (block
), domain_name (domain
));
1970 /* Look up bare names in the block's scope. */
1971 if (name
[cp_find_first_component (name
)] == '\0')
1973 const char *scope
= block_scope (block
);
1975 if (scope
[0] != '\0')
1977 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
1978 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
1980 result
= lookup_symbol_in_static_block (scopedname
, block
,
1982 if (result
.symbol
== NULL
)
1983 result
= lookup_global_symbol (scopedname
, block
, domain
);
1984 do_cleanups (cleanup
);
1992 /* la_sniff_from_mangled_name for Rust. */
1995 rust_sniff_from_mangled_name (const char *mangled
, char **demangled
)
1997 *demangled
= gdb_demangle (mangled
, DMGL_PARAMS
| DMGL_ANSI
);
1998 return *demangled
!= NULL
;
2003 static const struct exp_descriptor exp_descriptor_rust
=
2006 rust_operator_length
,
2007 rust_operator_check
,
2009 rust_dump_subexp_body
,
2010 rust_evaluate_subexp
2013 static const char *rust_extensions
[] =
2018 static const struct language_defn rust_language_defn
=
2028 &exp_descriptor_rust
,
2032 rust_printchar
, /* Print a character constant */
2033 rust_printstr
, /* Function to print string constant */
2034 rust_emitchar
, /* Print a single char */
2035 rust_print_type
, /* Print a type using appropriate syntax */
2036 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2037 rust_val_print
, /* Print a value using appropriate syntax */
2038 c_value_print
, /* Print a top-level value */
2039 default_read_var_value
, /* la_read_var_value */
2040 NULL
, /* Language specific skip_trampoline */
2041 NULL
, /* name_of_this */
2042 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2043 basic_lookup_transparent_type
,/* lookup_transparent_type */
2044 gdb_demangle
, /* Language specific symbol demangler */
2045 rust_sniff_from_mangled_name
,
2046 NULL
, /* Language specific
2047 class_name_from_physname */
2048 c_op_print_tab
, /* expression operators for printing */
2049 1, /* c-style arrays */
2050 0, /* String lower bound */
2051 default_word_break_characters
,
2052 default_make_symbol_completion_list
,
2053 rust_language_arch_info
,
2054 default_print_array_index
,
2055 default_pass_by_reference
,
2057 NULL
, /* la_get_symbol_name_cmp */
2058 iterate_over_symbols
,
2059 &default_varobj_ops
,
2066 _initialize_rust_language (void)
2068 add_language (&rust_language_defn
);