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"
31 #include "rust-lang.h"
35 extern initialize_file_ftype _initialize_rust_language
;
37 /* Returns the last segment of a Rust path like foo::bar::baz. Will
38 not handle cases where the last segment contains generics. This
39 will return NULL if the last segment cannot be found. */
42 rust_last_path_segment (const char * path
)
44 const char *result
= strrchr (path
, ':');
51 /* Find the Rust crate for BLOCK. If no crate can be found, returns
52 NULL. Otherwise, returns a newly allocated string that the caller
53 is responsible for freeing. */
56 rust_crate_for_block (const struct block
*block
)
58 const char *scope
= block_scope (block
);
63 return xstrndup (scope
, cp_find_first_component (scope
));
66 /* Information about the discriminant/variant of an enum */
70 /* Name of field. Must be freed by caller. */
72 /* Field number in union. Negative on error. For an encoded enum,
73 the "hidden" member will always be field 1, and the "real" member
74 will always be field 0. */
76 /* True if this is an encoded enum that has a single "real" member
77 and a single "hidden" member. */
78 unsigned int is_encoded
: 1;
81 /* The prefix of a specially-encoded enum. */
83 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
85 /* The number of the real field. */
87 #define RUST_ENCODED_ENUM_REAL 0
89 /* The number of the hidden field. */
91 #define RUST_ENCODED_ENUM_HIDDEN 1
93 /* Utility function to get discriminant info for a given value. */
95 static struct disr_info
96 rust_get_disr_info (struct type
*type
, const gdb_byte
*valaddr
,
97 int embedded_offset
, CORE_ADDR address
,
98 const struct value
*val
)
101 struct disr_info ret
;
102 struct type
*disr_type
;
103 struct ui_file
*temp_file
;
104 struct value_print_options opts
;
105 struct cleanup
*cleanup
;
106 const char *name_segment
;
108 get_no_prettyformat_print_options (&opts
);
113 if (TYPE_NFIELDS (type
) == 0)
114 error (_("Encountered void enum value"));
116 /* If an enum has two values where one is empty and the other holds
117 a pointer that cannot be zero; then the Rust compiler optimizes
118 away the discriminant and instead uses a zero value in the
119 pointer field to indicate the empty variant. */
120 if (strncmp (TYPE_FIELD_NAME (type
, 0), RUST_ENUM_PREFIX
,
121 strlen (RUST_ENUM_PREFIX
)) == 0)
124 unsigned long fieldno
;
125 struct type
*member_type
;
130 if (TYPE_NFIELDS (type
) != 1)
131 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX
);
133 fieldno
= strtoul (TYPE_FIELD_NAME (type
, 0) + strlen (RUST_ENUM_PREFIX
),
136 error (_("Invalid form for %s"), RUST_ENUM_PREFIX
);
138 member_type
= TYPE_FIELD_TYPE (type
, 0);
139 if (fieldno
>= TYPE_NFIELDS (member_type
))
140 error (_("%s refers to field after end of member type"),
143 embedded_offset
+= TYPE_FIELD_BITPOS (member_type
, fieldno
) / 8;
144 value
= unpack_long (TYPE_FIELD_TYPE (member_type
, fieldno
),
145 valaddr
+ embedded_offset
);
148 ret
.field_no
= RUST_ENCODED_ENUM_HIDDEN
;
149 ret
.name
= concat (TYPE_NAME (type
), "::", tail
+ 1, (char *) NULL
);
153 ret
.field_no
= RUST_ENCODED_ENUM_REAL
;
154 ret
.name
= concat (TYPE_NAME (type
), "::",
155 rust_last_path_segment (TYPE_NAME (member_type
)),
162 disr_type
= TYPE_FIELD_TYPE (type
, 0);
164 if (TYPE_NFIELDS (disr_type
) == 0)
166 /* This is a bounds check and should never be hit unless Rust
167 has changed its debuginfo format. */
168 error (_("Could not find enum discriminant field"));
171 if (strcmp (TYPE_FIELD_NAME (disr_type
, 0), "RUST$ENUM$DISR") != 0)
172 error (_("Rust debug format has changed"));
174 temp_file
= mem_fileopen ();
175 cleanup
= make_cleanup_ui_file_delete (temp_file
);
176 /* The first value of the first field (or any field)
177 is the discriminant value. */
178 c_val_print (TYPE_FIELD_TYPE (disr_type
, 0), valaddr
,
179 (embedded_offset
+ TYPE_FIELD_BITPOS (type
, 0) / 8
180 + TYPE_FIELD_BITPOS (disr_type
, 0) / 8),
184 ret
.name
= ui_file_xstrdup (temp_file
, NULL
);
185 name_segment
= rust_last_path_segment (ret
.name
);
186 if (name_segment
!= NULL
)
188 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
190 /* Sadly, the discriminant value paths do not match the type
191 field name paths ('core::option::Option::Some' vs
192 'core::option::Some'). However, enum variant names are
193 unique in the last path segment and the generics are not
194 part of this path, so we can just compare those. This is
195 hackish and would be better fixed by improving rustc's
196 metadata for enums. */
197 const char *field_type
= TYPE_NAME (TYPE_FIELD_TYPE (type
, i
));
199 if (field_type
!= NULL
200 && strcmp (name_segment
,
201 rust_last_path_segment (field_type
)) == 0)
209 if (ret
.field_no
== -1 && ret
.name
!= NULL
)
211 /* Somehow the discriminant wasn't found. */
212 make_cleanup (xfree
, ret
.name
);
213 error (_("Could not find variant of %s with discriminant %s"),
214 TYPE_TAG_NAME (type
), ret
.name
);
217 do_cleanups (cleanup
);
221 /* See rust-lang.h. */
224 rust_tuple_type_p (struct type
*type
)
226 /* The current implementation is a bit of a hack, but there's
227 nothing else in the debuginfo to distinguish a tuple from a
229 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
230 && TYPE_TAG_NAME (type
) != NULL
231 && TYPE_TAG_NAME (type
)[0] == '(');
235 /* Return true if all non-static fields of a structlike type are in a
236 sequence like __0, __1, __2. OFFSET lets us skip fields. */
239 rust_underscore_fields (struct type
*type
, int offset
)
245 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
)
247 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
249 if (!field_is_static (&TYPE_FIELD (type
, i
)))
257 xsnprintf (buf
, sizeof (buf
), "__%d", field_number
);
258 if (strcmp (buf
, TYPE_FIELD_NAME (type
, i
)) != 0)
267 /* See rust-lang.h. */
270 rust_tuple_struct_type_p (struct type
*type
)
272 return rust_underscore_fields (type
, 0);
275 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
278 rust_tuple_variant_type_p (struct type
*type
)
280 /* First field is discriminant */
281 return rust_underscore_fields (type
, 1);
284 /* Return true if TYPE is a slice type, otherwise false. */
287 rust_slice_type_p (struct type
*type
)
289 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
290 && TYPE_TAG_NAME (type
) != NULL
291 && strncmp (TYPE_TAG_NAME (type
), "&[", 2) == 0);
294 /* Return true if TYPE is a range type, otherwise false. */
297 rust_range_type_p (struct type
*type
)
301 if (TYPE_CODE (type
) != TYPE_CODE_STRUCT
302 || TYPE_NFIELDS (type
) > 2
303 || TYPE_TAG_NAME (type
) == NULL
304 || strstr (TYPE_TAG_NAME (type
), "::Range") == NULL
)
307 if (TYPE_NFIELDS (type
) == 0)
311 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
313 if (TYPE_NFIELDS (type
) == 1)
317 else if (TYPE_NFIELDS (type
) == 2)
319 /* First field had to be "start". */
323 return strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0;
326 /* Return true if TYPE seems to be the type "u8", otherwise false. */
329 rust_u8_type_p (struct type
*type
)
331 return (TYPE_CODE (type
) == TYPE_CODE_INT
332 && TYPE_UNSIGNED (type
)
333 && TYPE_LENGTH (type
) == 1);
336 /* Return true if TYPE is a Rust character type. */
339 rust_chartype_p (struct type
*type
)
341 return (TYPE_CODE (type
) == TYPE_CODE_CHAR
342 && TYPE_LENGTH (type
) == 4
343 && TYPE_UNSIGNED (type
));
348 /* la_emitchar implementation for Rust. */
351 rust_emitchar (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
353 if (!rust_chartype_p (type
))
354 generic_emit_char (c
, type
, stream
, quoter
,
355 target_charset (get_type_arch (type
)));
356 else if (c
== '\\' || c
== quoter
)
357 fprintf_filtered (stream
, "\\%c", c
);
359 fputs_filtered ("\\n", stream
);
361 fputs_filtered ("\\r", stream
);
363 fputs_filtered ("\\t", stream
);
365 fputs_filtered ("\\0", stream
);
366 else if (c
>= 32 && c
<= 127 && isprint (c
))
367 fputc_filtered (c
, stream
);
369 fprintf_filtered (stream
, "\\x%02x", c
);
371 fprintf_filtered (stream
, "\\u{%06x}", c
);
374 /* la_printchar implementation for Rust. */
377 rust_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
379 fputs_filtered ("'", stream
);
380 LA_EMIT_CHAR (c
, type
, stream
, '\'');
381 fputs_filtered ("'", stream
);
384 /* la_printstr implementation for Rust. */
387 rust_printstr (struct ui_file
*stream
, struct type
*type
,
388 const gdb_byte
*string
, unsigned int length
,
389 const char *user_encoding
, int force_ellipses
,
390 const struct value_print_options
*options
)
392 /* Rust always uses UTF-8, but let the caller override this if need
394 const char *encoding
= user_encoding
;
395 if (user_encoding
== NULL
|| !*user_encoding
)
397 /* In Rust strings, characters are "u8". */
398 if (rust_u8_type_p (type
))
402 /* This is probably some C string, so let's let C deal with
404 c_printstr (stream
, type
, string
, length
, user_encoding
,
405 force_ellipses
, options
);
410 /* This is not ideal as it doesn't use our character printer. */
411 generic_printstr (stream
, type
, string
, length
, encoding
, force_ellipses
,
417 static const struct generic_val_print_decorations rust_decorations
=
419 /* Complex isn't used in Rust, but we provide C-ish values just in
431 /* la_val_print implementation for Rust. */
434 rust_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
435 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
436 const struct value
*val
,
437 const struct value_print_options
*options
)
439 type
= check_typedef (type
);
440 switch (TYPE_CODE (type
))
444 LONGEST low_bound
, high_bound
;
446 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_ARRAY
447 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type
)))
448 && get_array_bounds (TYPE_TARGET_TYPE (type
), &low_bound
,
450 /* We have a pointer to a byte string, so just print
452 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
454 struct gdbarch
*arch
= get_type_arch (type
);
455 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
457 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
* unit_size
);
458 if (options
->addressprint
)
460 fputs_filtered (paddress (arch
, addr
), stream
);
461 fputs_filtered (" ", stream
);
464 fputs_filtered ("b", stream
);
465 val_print_string (TYPE_TARGET_TYPE (elttype
), "ASCII", addr
,
466 high_bound
- low_bound
+ 1, stream
,
473 case TYPE_CODE_METHODPTR
:
474 case TYPE_CODE_MEMBERPTR
:
475 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
476 recurse
, val
, options
);
480 /* Recognize the unit type. */
481 if (TYPE_UNSIGNED (type
) && TYPE_LENGTH (type
) == 0
482 && TYPE_NAME (type
) != NULL
&& strcmp (TYPE_NAME (type
), "()") == 0)
484 fputs_filtered ("()", stream
);
489 case TYPE_CODE_STRING
:
491 struct gdbarch
*arch
= get_type_arch (type
);
492 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
493 LONGEST low_bound
, high_bound
;
495 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
496 error (_("Could not determine the array bounds"));
498 /* If we see a plain TYPE_CODE_STRING, then we're printing a
499 byte string, hence the choice of "ASCII" as the
501 fputs_filtered ("b", stream
);
502 rust_printstr (stream
, TYPE_TARGET_TYPE (type
),
503 valaddr
+ embedded_offset
* unit_size
,
504 high_bound
- low_bound
+ 1, "ASCII", 0, options
);
508 case TYPE_CODE_ARRAY
:
510 LONGEST low_bound
, high_bound
;
512 if (get_array_bounds (type
, &low_bound
, &high_bound
)
513 && high_bound
- low_bound
+ 1 == 0)
514 fputs_filtered ("[]", stream
);
520 case TYPE_CODE_UNION
:
522 int j
, nfields
, first_field
, is_tuple
, start
;
523 struct type
*variant_type
;
524 struct disr_info disr
;
525 struct value_print_options opts
;
526 struct cleanup
*cleanup
;
531 disr
= rust_get_disr_info (type
, valaddr
, embedded_offset
, address
,
533 cleanup
= make_cleanup (xfree
, disr
.name
);
535 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
537 fprintf_filtered (stream
, "%s", disr
.name
);
542 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
543 nfields
= TYPE_NFIELDS (variant_type
);
545 is_tuple
= (disr
.is_encoded
546 ? rust_tuple_struct_type_p (variant_type
)
547 : rust_tuple_variant_type_p (variant_type
));
548 start
= disr
.is_encoded
? 0 : 1;
552 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
554 fprintf_filtered (stream
, "%s(", disr
.name
);
557 /* struct variant. */
558 fprintf_filtered (stream
, "%s{", disr
.name
);
563 /* In case of a nullary variant like 'None', just output
565 fprintf_filtered (stream
, "%s", disr
.name
);
569 for (j
= start
; j
< TYPE_NFIELDS (variant_type
); j
++)
572 fputs_filtered (", ", stream
);
576 fprintf_filtered (stream
, "%s: ",
577 TYPE_FIELD_NAME (variant_type
, j
));
579 val_print (TYPE_FIELD_TYPE (variant_type
, j
),
582 + TYPE_FIELD_BITPOS (type
, disr
.field_no
) / 8
583 + TYPE_FIELD_BITPOS (variant_type
, j
) / 8),
585 stream
, recurse
+ 1, val
, &opts
,
590 fputs_filtered (")", stream
);
592 fputs_filtered ("}", stream
);
595 do_cleanups (cleanup
);
599 case TYPE_CODE_STRUCT
:
603 int is_tuple
= rust_tuple_type_p (type
);
604 int is_tuple_struct
= !is_tuple
&& rust_tuple_struct_type_p (type
);
605 struct value_print_options opts
;
609 if (TYPE_TAG_NAME (type
) != NULL
)
610 fprintf_filtered (stream
, "%s", TYPE_TAG_NAME (type
));
612 if (TYPE_NFIELDS (type
) == 0)
615 if (TYPE_TAG_NAME (type
) != NULL
)
616 fputs_filtered (" ", stream
);
619 if (is_tuple
|| is_tuple_struct
)
620 fputs_filtered ("(", stream
);
622 fputs_filtered ("{", stream
);
628 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
630 if (field_is_static (&TYPE_FIELD (type
, i
)))
634 fputs_filtered (",", stream
);
636 if (options
->prettyformat
)
638 fputs_filtered ("\n", stream
);
639 print_spaces_filtered (2 + 2 * recurse
, stream
);
641 else if (!first_field
)
642 fputs_filtered (" ", stream
);
646 if (!is_tuple
&& !is_tuple_struct
)
648 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
649 fputs_filtered (": ", stream
);
652 val_print (TYPE_FIELD_TYPE (type
, i
),
654 embedded_offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
656 stream
, recurse
+ 1, val
, &opts
,
660 if (options
->prettyformat
)
662 fputs_filtered ("\n", stream
);
663 print_spaces_filtered (2 * recurse
, stream
);
666 if (is_tuple
|| is_tuple_struct
)
667 fputs_filtered (")", stream
);
669 fputs_filtered ("}", stream
);
675 /* Nothing special yet. */
676 generic_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
677 recurse
, val
, options
, &rust_decorations
);
683 /* la_print_typedef implementation for Rust. */
686 rust_print_typedef (struct type
*type
,
687 struct symbol
*new_symbol
,
688 struct ui_file
*stream
)
690 type
= check_typedef (type
);
691 fprintf_filtered (stream
, "type %s = ", SYMBOL_PRINT_NAME (new_symbol
));
692 type_print (type
, "", stream
, 0);
693 fprintf_filtered (stream
, ";\n");
696 /* la_print_type implementation for Rust. */
699 rust_print_type (struct type
*type
, const char *varstring
,
700 struct ui_file
*stream
, int show
, int level
,
701 const struct type_print_options
*flags
)
707 && TYPE_NAME (type
) != NULL
)
709 fputs_filtered (TYPE_NAME (type
), stream
);
713 type
= check_typedef (type
);
714 switch (TYPE_CODE (type
))
717 /* Delegate varargs to the C printer. */
718 if (TYPE_VARARGS (type
))
721 fputs_filtered ("fn ", stream
);
722 if (varstring
!= NULL
)
723 fputs_filtered (varstring
, stream
);
724 fputs_filtered ("(", stream
);
725 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
729 fputs_filtered (", ", stream
);
730 rust_print_type (TYPE_FIELD_TYPE (type
, i
), "", stream
, -1, 0,
733 fputs_filtered (") -> ", stream
);
734 rust_print_type (TYPE_TARGET_TYPE (type
), "", stream
, -1, 0, flags
);
737 case TYPE_CODE_ARRAY
:
739 LONGEST low_bound
, high_bound
;
741 fputs_filtered ("[", stream
);
742 rust_print_type (TYPE_TARGET_TYPE (type
), NULL
,
743 stream
, show
- 1, level
, flags
);
744 fputs_filtered ("; ", stream
);
746 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCEXPR
747 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type
)) == PROP_LOCLIST
)
748 fprintf_filtered (stream
, "variable length");
749 else if (get_array_bounds (type
, &low_bound
, &high_bound
))
750 fprintf_filtered (stream
, "%s",
751 plongest (high_bound
- low_bound
+ 1));
752 fputs_filtered ("]", stream
);
756 case TYPE_CODE_STRUCT
:
760 /* Print a tuple type simply. */
761 if (rust_tuple_type_p (type
))
763 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
767 /* If we see a base class, delegate to C. */
768 if (TYPE_N_BASECLASSES (type
) > 0)
771 fputs_filtered ("struct ", stream
);
772 if (TYPE_TAG_NAME (type
) != NULL
)
773 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
775 is_tuple_struct
= rust_tuple_struct_type_p (type
);
777 if (TYPE_NFIELDS (type
) == 0 && !rust_tuple_type_p (type
))
779 fputs_filtered (is_tuple_struct
? " (\n" : " {\n", stream
);
781 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
786 if (field_is_static (&TYPE_FIELD (type
, i
)))
789 /* We'd like to print "pub" here as needed, but rustc
790 doesn't emit the debuginfo, and our types don't have
791 cplus_struct_type attached. */
793 /* For a tuple struct we print the type but nothing
795 print_spaces_filtered (level
+ 2, stream
);
796 if (!is_tuple_struct
)
797 fprintf_filtered (stream
, "%s: ", TYPE_FIELD_NAME (type
, i
));
799 rust_print_type (TYPE_FIELD_TYPE (type
, i
), NULL
,
800 stream
, show
- 1, level
+ 2,
802 fputs_filtered (",\n", stream
);
805 fprintfi_filtered (level
, stream
, is_tuple_struct
? ")" : "}");
813 fputs_filtered ("enum ", stream
);
814 if (TYPE_TAG_NAME (type
) != NULL
)
816 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
817 fputs_filtered (" ", stream
);
818 len
= strlen (TYPE_TAG_NAME (type
));
820 fputs_filtered ("{\n", stream
);
822 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
824 const char *name
= TYPE_FIELD_NAME (type
, i
);
829 && strncmp (name
, TYPE_TAG_NAME (type
), len
) == 0
831 && name
[len
+ 1] == ':')
833 fprintfi_filtered (level
+ 2, stream
, "%s,\n", name
);
836 fputs_filtered ("}", stream
);
840 case TYPE_CODE_UNION
:
845 fputs_filtered ("enum ", stream
);
846 if (TYPE_TAG_NAME (type
) != NULL
)
848 fputs_filtered (TYPE_TAG_NAME (type
), stream
);
849 fputs_filtered (" ", stream
);
850 len
= strlen (TYPE_TAG_NAME (type
));
852 fputs_filtered ("{\n", stream
);
854 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
856 struct type
*variant_type
= TYPE_FIELD_TYPE (type
, i
);
858 = rust_last_path_segment (TYPE_NAME (variant_type
));
860 fprintfi_filtered (level
+ 2, stream
, "%s", name
);
862 if (TYPE_NFIELDS (variant_type
) > 1)
865 int is_tuple
= rust_tuple_variant_type_p (variant_type
);
868 fputs_filtered (is_tuple
? "(" : "{", stream
);
869 for (j
= 1; j
< TYPE_NFIELDS (variant_type
); j
++)
874 fputs_filtered (", ", stream
);
877 fprintf_filtered (stream
, "%s: ",
878 TYPE_FIELD_NAME (variant_type
, j
));
880 rust_print_type (TYPE_FIELD_TYPE (variant_type
, j
), NULL
,
881 stream
, show
- 1, level
+ 2,
884 fputs_filtered (is_tuple
? ")" : "}", stream
);
887 fputs_filtered (",\n", stream
);
890 fputs_filtered ("}", stream
);
896 c_print_type (type
, varstring
, stream
, show
, level
, flags
);
902 /* Compute the alignment of the type T. */
905 rust_type_alignment (struct type
*t
)
907 t
= check_typedef (t
);
908 switch (TYPE_CODE (t
))
911 error (_("Could not compute alignment of type"));
920 return TYPE_LENGTH (t
);
922 case TYPE_CODE_ARRAY
:
923 case TYPE_CODE_COMPLEX
:
924 return rust_type_alignment (TYPE_TARGET_TYPE (t
));
926 case TYPE_CODE_STRUCT
:
927 case TYPE_CODE_UNION
:
932 for (i
= 0; i
< TYPE_NFIELDS (t
); ++i
)
934 int a
= rust_type_alignment (TYPE_FIELD_TYPE (t
, i
));
943 /* Like arch_composite_type, but uses TYPE to decide how to allocate
944 -- either on an obstack or on a gdbarch. */
947 rust_composite_type (struct type
*original
,
949 const char *field1
, struct type
*type1
,
950 const char *field2
, struct type
*type2
)
952 struct type
*result
= alloc_type_copy (original
);
953 int i
, nfields
, bitpos
;
961 TYPE_CODE (result
) = TYPE_CODE_STRUCT
;
962 TYPE_NAME (result
) = name
;
963 TYPE_TAG_NAME (result
) = name
;
965 TYPE_NFIELDS (result
) = nfields
;
967 = (struct field
*) TYPE_ZALLOC (result
, nfields
* sizeof (struct field
));
973 struct field
*field
= &TYPE_FIELD (result
, i
);
975 SET_FIELD_BITPOS (*field
, bitpos
);
976 bitpos
+= TYPE_LENGTH (type1
) * TARGET_CHAR_BIT
;
978 FIELD_NAME (*field
) = field1
;
979 FIELD_TYPE (*field
) = type1
;
984 struct field
*field
= &TYPE_FIELD (result
, i
);
985 int align
= rust_type_alignment (type2
);
991 align
*= TARGET_CHAR_BIT
;
992 delta
= bitpos
% align
;
994 bitpos
+= align
- delta
;
996 SET_FIELD_BITPOS (*field
, bitpos
);
998 FIELD_NAME (*field
) = field2
;
999 FIELD_TYPE (*field
) = type2
;
1004 TYPE_LENGTH (result
)
1005 = (TYPE_FIELD_BITPOS (result
, i
- 1) / TARGET_CHAR_BIT
+
1006 TYPE_LENGTH (TYPE_FIELD_TYPE (result
, i
- 1)));
1010 /* See rust-lang.h. */
1013 rust_slice_type (const char *name
, struct type
*elt_type
,
1014 struct type
*usize_type
)
1018 elt_type
= lookup_pointer_type (elt_type
);
1019 type
= rust_composite_type (elt_type
, name
,
1020 "data_ptr", elt_type
,
1021 "length", usize_type
);
1026 enum rust_primitive_types
1028 rust_primitive_bool
,
1029 rust_primitive_char
,
1038 rust_primitive_isize
,
1039 rust_primitive_usize
,
1042 rust_primitive_unit
,
1044 nr_rust_primitive_types
1047 /* la_language_arch_info implementation for Rust. */
1050 rust_language_arch_info (struct gdbarch
*gdbarch
,
1051 struct language_arch_info
*lai
)
1053 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1055 struct type
**types
;
1056 unsigned int length
;
1058 types
= GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_rust_primitive_types
+ 1,
1061 types
[rust_primitive_bool
] = arch_boolean_type (gdbarch
, 8, 1, "bool");
1062 types
[rust_primitive_char
] = arch_character_type (gdbarch
, 32, 1, "char");
1063 types
[rust_primitive_i8
] = arch_integer_type (gdbarch
, 8, 0, "i8");
1064 types
[rust_primitive_u8
] = arch_integer_type (gdbarch
, 8, 1, "u8");
1065 types
[rust_primitive_i16
] = arch_integer_type (gdbarch
, 16, 0, "i16");
1066 types
[rust_primitive_u16
] = arch_integer_type (gdbarch
, 16, 1, "u16");
1067 types
[rust_primitive_i32
] = arch_integer_type (gdbarch
, 32, 0, "i32");
1068 types
[rust_primitive_u32
] = arch_integer_type (gdbarch
, 32, 1, "u32");
1069 types
[rust_primitive_i64
] = arch_integer_type (gdbarch
, 64, 0, "i64");
1070 types
[rust_primitive_u64
] = arch_integer_type (gdbarch
, 64, 1, "u64");
1072 length
= 8 * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1073 types
[rust_primitive_isize
] = arch_integer_type (gdbarch
, length
, 0, "isize");
1074 types
[rust_primitive_usize
] = arch_integer_type (gdbarch
, length
, 1, "usize");
1076 types
[rust_primitive_f32
] = arch_float_type (gdbarch
, 32, "f32", NULL
);
1077 types
[rust_primitive_f64
] = arch_float_type (gdbarch
, 64, "f64", NULL
);
1079 types
[rust_primitive_unit
] = arch_integer_type (gdbarch
, 0, 1, "()");
1081 tem
= make_cv_type (1, 0, types
[rust_primitive_u8
], NULL
);
1082 types
[rust_primitive_str
] = rust_slice_type ("&str", tem
,
1083 types
[rust_primitive_usize
]);
1085 lai
->primitive_type_vector
= types
;
1086 lai
->bool_type_default
= types
[rust_primitive_bool
];
1087 lai
->string_char_type
= types
[rust_primitive_u8
];
1092 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1094 static struct value
*
1095 rust_evaluate_funcall (struct expression
*exp
, int *pos
, enum noside noside
)
1098 int num_args
= exp
->elts
[*pos
+ 1].longconst
;
1101 struct value
*function
, *result
, *arg0
;
1102 struct value
**args
;
1103 struct cleanup
*cleanup
;
1104 struct type
*type
, *fn_type
;
1105 const struct block
*block
;
1106 struct block_symbol sym
;
1108 /* For an ordinary function call we can simply defer to the
1109 generic implementation. */
1110 if (exp
->elts
[*pos
+ 3].opcode
!= STRUCTOP_STRUCT
)
1111 return evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1113 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1115 method
= &exp
->elts
[*pos
+ 1].string
;
1116 *pos
+= 3 + BYTES_TO_EXP_ELEM (exp
->elts
[*pos
].longconst
+ 1);
1118 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1119 type in order to look up the method. */
1120 arg0
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1122 if (noside
== EVAL_SKIP
)
1124 for (i
= 0; i
< num_args
; ++i
)
1125 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1129 args
= XNEWVEC (struct value
*, num_args
+ 1);
1130 cleanup
= make_cleanup (xfree
, args
);
1133 /* We don't yet implement real Deref semantics. */
1134 while (TYPE_CODE (value_type (args
[0])) == TYPE_CODE_PTR
)
1135 args
[0] = value_ind (args
[0]);
1137 type
= value_type (args
[0]);
1138 if ((TYPE_CODE (type
) != TYPE_CODE_STRUCT
1139 && TYPE_CODE (type
) != TYPE_CODE_UNION
1140 && TYPE_CODE (type
) != TYPE_CODE_ENUM
)
1141 || rust_tuple_type_p (type
))
1142 error (_("Method calls only supported on struct or enum types"));
1143 if (TYPE_TAG_NAME (type
) == NULL
)
1144 error (_("Method call on nameless type"));
1146 name
= concat (TYPE_TAG_NAME (type
), "::", method
, (char *) NULL
);
1147 make_cleanup (xfree
, name
);
1149 block
= get_selected_block (0);
1150 sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
);
1151 if (sym
.symbol
== NULL
)
1152 error (_("Could not find function named '%s'"), name
);
1154 fn_type
= SYMBOL_TYPE (sym
.symbol
);
1155 if (TYPE_NFIELDS (fn_type
) == 0)
1156 error (_("Function '%s' takes no arguments"), name
);
1158 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type
, 0)) == TYPE_CODE_PTR
)
1159 args
[0] = value_addr (args
[0]);
1161 function
= address_of_variable (sym
.symbol
, block
);
1163 for (i
= 0; i
< num_args
; ++i
)
1164 args
[i
+ 1] = evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1166 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1167 result
= value_zero (TYPE_TARGET_TYPE (fn_type
), not_lval
);
1169 result
= call_function_by_hand (function
, num_args
+ 1, args
);
1170 do_cleanups (cleanup
);
1174 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1176 static struct value
*
1177 rust_range (struct expression
*exp
, int *pos
, enum noside noside
)
1179 enum range_type kind
;
1180 struct value
*low
= NULL
, *high
= NULL
;
1181 struct value
*addrval
, *result
;
1183 struct type
*range_type
;
1184 struct type
*index_type
;
1185 struct type
*temp_type
;
1188 kind
= (enum range_type
) longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1191 if (kind
== HIGH_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1192 low
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1193 if (kind
== LOW_BOUND_DEFAULT
|| kind
== NONE_BOUND_DEFAULT
)
1194 high
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1196 if (noside
== EVAL_SKIP
)
1197 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
1204 name
= "std::ops::RangeFull";
1208 index_type
= value_type (high
);
1209 name
= "std::ops::RangeTo";
1216 index_type
= value_type (low
);
1217 name
= "std::ops::RangeFrom";
1221 if (!types_equal (value_type (low
), value_type (high
)))
1222 error (_("Range expression with different types"));
1223 index_type
= value_type (low
);
1224 name
= "std::ops::Range";
1228 /* If we don't have an index type, just allocate this on the
1229 arch. Here any type will do. */
1230 temp_type
= (index_type
== NULL
1231 ? language_bool_type (exp
->language_defn
, exp
->gdbarch
)
1233 /* It would be nicer to cache the range type. */
1234 range_type
= rust_composite_type (temp_type
, name
,
1235 low
== NULL
? NULL
: "start", index_type
,
1236 high
== NULL
? NULL
: "end", index_type
);
1238 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1239 return value_zero (range_type
, lval_memory
);
1241 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (range_type
));
1242 addr
= value_as_long (addrval
);
1243 result
= value_at_lazy (range_type
, addr
);
1247 struct value
*start
= value_struct_elt (&result
, NULL
, "start", NULL
,
1250 value_assign (start
, low
);
1255 struct value
*end
= value_struct_elt (&result
, NULL
, "end", NULL
,
1258 value_assign (end
, high
);
1261 result
= value_at_lazy (range_type
, addr
);
1265 /* A helper function to compute the range and kind given a range
1266 value. TYPE is the type of the range value. RANGE is the range
1267 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1268 parameters might be filled in, or might not be, depending on the
1269 kind of range this is. KIND will always be set to the appropriate
1270 value describing the kind of range, and this can be used to
1271 determine whether LOW or HIGH are valid. */
1274 rust_compute_range (struct type
*type
, struct value
*range
,
1275 LONGEST
*low
, LONGEST
*high
,
1276 enum range_type
*kind
)
1282 *kind
= BOTH_BOUND_DEFAULT
;
1284 if (TYPE_NFIELDS (type
) == 0)
1288 if (strcmp (TYPE_FIELD_NAME (type
, 0), "start") == 0)
1290 *kind
= HIGH_BOUND_DEFAULT
;
1291 *low
= value_as_long (value_field (range
, 0));
1294 if (TYPE_NFIELDS (type
) > i
1295 && strcmp (TYPE_FIELD_NAME (type
, i
), "end") == 0)
1297 *kind
= (*kind
== BOTH_BOUND_DEFAULT
1298 ? LOW_BOUND_DEFAULT
: NONE_BOUND_DEFAULT
);
1299 *high
= value_as_long (value_field (range
, i
));
1303 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1305 static struct value
*
1306 rust_subscript (struct expression
*exp
, int *pos
, enum noside noside
,
1309 struct value
*lhs
, *rhs
, *result
;
1310 struct type
*rhstype
;
1311 LONGEST low
, high
, high_bound
;
1312 /* Initialized to appease the compiler. */
1313 enum range_type kind
= BOTH_BOUND_DEFAULT
;
1317 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1318 rhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1320 if (noside
== EVAL_SKIP
)
1323 rhstype
= check_typedef (value_type (rhs
));
1324 if (rust_range_type_p (rhstype
))
1327 error (_("Can't take slice of array without '&'"));
1328 rust_compute_range (rhstype
, rhs
, &low
, &high
, &kind
);
1332 low
= value_as_long (rhs
);
1334 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1336 struct type
*type
= check_typedef (value_type (lhs
));
1338 result
= value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (lhs
));
1344 struct type
*type
= check_typedef (value_type (lhs
));
1346 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
1349 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1350 error (_("Can't compute array bounds"));
1352 error (_("Found array with non-zero lower bound"));
1355 else if (rust_slice_type_p (type
))
1359 base
= value_struct_elt (&lhs
, NULL
, "data_ptr", NULL
, "slice");
1360 len
= value_struct_elt (&lhs
, NULL
, "length", NULL
, "slice");
1362 high_bound
= value_as_long (len
);
1365 error (_("Cannot subscript non-array type"));
1368 && (kind
== BOTH_BOUND_DEFAULT
|| kind
== LOW_BOUND_DEFAULT
))
1371 error (_("Index less than zero"));
1372 if (low
> high_bound
)
1373 error (_("Index greater than length"));
1375 result
= value_subscript (base
, low
);
1382 struct type
*usize
, *slice
;
1384 struct value
*addrval
, *tem
;
1386 if (kind
== BOTH_BOUND_DEFAULT
|| kind
== HIGH_BOUND_DEFAULT
)
1389 error (_("High index less than zero"));
1391 error (_("Low index greater than high index"));
1392 if (high
> high_bound
)
1393 error (_("High index greater than length"));
1395 usize
= language_lookup_primitive_type (exp
->language_defn
,
1398 slice
= rust_slice_type ("&[*gdb*]", value_type (result
),
1401 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (slice
));
1402 addr
= value_as_long (addrval
);
1403 tem
= value_at_lazy (slice
, addr
);
1405 value_assign (value_field (tem
, 0), value_addr (result
));
1406 value_assign (value_field (tem
, 1),
1407 value_from_longest (usize
, high
- low
));
1409 result
= value_at_lazy (slice
, addr
);
1412 result
= value_addr (result
);
1418 /* evaluate_exp implementation for Rust. */
1420 static struct value
*
1421 rust_evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
1422 int *pos
, enum noside noside
)
1424 struct value
*result
;
1426 switch (exp
->elts
[*pos
].opcode
)
1428 case UNOP_COMPLEMENT
:
1430 struct value
*value
;
1433 value
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1434 if (noside
== EVAL_SKIP
)
1436 /* Preserving the type is enough. */
1439 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_BOOL
)
1440 result
= value_from_longest (value_type (value
),
1441 value_logical_not (value
));
1443 result
= value_complement (value
);
1447 case BINOP_SUBSCRIPT
:
1448 result
= rust_subscript (exp
, pos
, noside
, 0);
1452 result
= rust_evaluate_funcall (exp
, pos
, noside
);
1458 struct type
*type
= exp
->elts
[pc
+ 1].type
;
1459 int arglen
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1462 struct value
*addrval
= NULL
;
1466 if (noside
== EVAL_NORMAL
)
1468 addrval
= value_allocate_space_in_inferior (TYPE_LENGTH (type
));
1469 addr
= value_as_long (addrval
);
1470 result
= value_at_lazy (type
, addr
);
1473 if (arglen
> 0 && exp
->elts
[*pos
].opcode
== OP_OTHERS
)
1478 init
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1479 if (noside
== EVAL_NORMAL
)
1481 /* This isn't quite right but will do for the time
1482 being, seeing that we can't implement the Copy
1484 value_assign (result
, init
);
1490 gdb_assert (arglen
% 2 == 0);
1491 for (i
= 0; i
< arglen
; i
+= 2)
1494 const char *fieldname
;
1495 struct value
*value
, *field
;
1497 gdb_assert (exp
->elts
[*pos
].opcode
== OP_NAME
);
1499 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1501 fieldname
= &exp
->elts
[*pos
].string
;
1502 *pos
+= 2 + BYTES_TO_EXP_ELEM (len
+ 1);
1504 value
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1505 if (noside
== EVAL_NORMAL
)
1507 field
= value_struct_elt (&result
, NULL
, fieldname
, NULL
,
1509 value_assign (field
, value
);
1513 if (noside
== EVAL_SKIP
)
1514 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
,
1516 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1517 result
= allocate_value (type
);
1519 result
= value_at_lazy (type
, addr
);
1528 struct value
*ncopies
;
1530 elt
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1531 ncopies
= rust_evaluate_subexp (NULL
, exp
, pos
, noside
);
1532 copies
= value_as_long (ncopies
);
1534 error (_("Array with negative number of elements"));
1536 if (noside
== EVAL_NORMAL
)
1540 struct value
**eltvec
= XNEWVEC (struct value
*, copies
);
1541 struct cleanup
*cleanup
= make_cleanup (xfree
, eltvec
);
1543 for (i
= 0; i
< copies
; ++i
)
1545 result
= value_array (0, copies
- 1, eltvec
);
1547 do_cleanups (cleanup
);
1551 struct type
*arraytype
1552 = lookup_array_range_type (value_type (elt
), 0, copies
- 1);
1553 result
= allocate_value (arraytype
);
1558 case STRUCTOP_ANONYMOUS
:
1560 /* Anonymous field access, i.e. foo.1. */
1562 int pc
, field_number
, nfields
;
1563 struct type
*type
, *variant_type
;
1564 struct disr_info disr
;
1567 field_number
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1569 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1571 type
= value_type (lhs
);
1572 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1574 struct cleanup
*cleanup
;
1576 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1577 value_embedded_offset (lhs
),
1578 value_address (lhs
), lhs
);
1580 cleanup
= make_cleanup (xfree
, disr
.name
);
1582 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1584 variant_type
= NULL
;
1589 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1590 nfields
= TYPE_NFIELDS (variant_type
);
1593 if (!disr
.is_encoded
)
1596 if (field_number
>= nfields
|| field_number
< 0)
1597 error(_("Cannot access field %d of variant %s, \
1598 there are only %d fields"),
1599 disr
.is_encoded
? field_number
: field_number
- 1,
1601 disr
.is_encoded
? nfields
: nfields
- 1);
1603 if (!(disr
.is_encoded
1604 ? rust_tuple_struct_type_p (variant_type
)
1605 : rust_tuple_variant_type_p (variant_type
)))
1606 error(_("Variant %s is not a tuple variant"), disr
.name
);
1608 result
= value_primitive_field (lhs
, 0, field_number
,
1610 do_cleanups (cleanup
);
1612 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1614 /* Tuples and tuple structs */
1615 nfields
= TYPE_NFIELDS(type
);
1617 if (field_number
>= nfields
|| field_number
< 0)
1618 error(_("Cannot access field %d of %s, there are only %d fields"),
1619 field_number
, TYPE_TAG_NAME (type
), nfields
);
1621 /* Tuples are tuple structs too. */
1622 if (!rust_tuple_struct_type_p (type
))
1623 error(_("Attempting to access anonymous field %d of %s, which is \
1624 not a tuple, tuple struct, or tuple-like variant"),
1625 field_number
, TYPE_TAG_NAME (type
));
1627 result
= value_primitive_field (lhs
, 0, field_number
, type
);
1630 error(_("Anonymous field access is only allowed on tuples, \
1631 tuple structs, and tuple-like enum variants"));
1635 case STRUCTOP_STRUCT
:
1642 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1643 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1644 lhs
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1646 type
= value_type (lhs
);
1648 if (TYPE_CODE (type
) == TYPE_CODE_UNION
)
1651 struct disr_info disr
;
1652 struct cleanup
* cleanup
;
1653 struct type
* variant_type
;
1656 field_name
= &exp
->elts
[pc
+ 2].string
;
1658 disr
= rust_get_disr_info (type
, value_contents (lhs
),
1659 value_embedded_offset (lhs
),
1660 value_address (lhs
), lhs
);
1662 cleanup
= make_cleanup (xfree
, disr
.name
);
1664 if (disr
.is_encoded
&& disr
.field_no
== RUST_ENCODED_ENUM_HIDDEN
)
1665 error(_("Could not find field %s of struct variant %s"),
1666 field_name
, disr
.name
);
1668 variant_type
= TYPE_FIELD_TYPE (type
, disr
.field_no
);
1670 if (variant_type
== NULL
1671 || rust_tuple_variant_type_p (variant_type
))
1672 error(_("Attempting to access named field %s of tuple variant %s, \
1673 which has only anonymous fields"),
1674 field_name
, disr
.name
);
1676 start
= disr
.is_encoded
? 0 : 1;
1677 for (i
= start
; i
< TYPE_NFIELDS (variant_type
); i
++)
1679 if (strcmp (TYPE_FIELD_NAME (variant_type
, i
),
1681 result
= value_primitive_field (lhs
, 0, i
, variant_type
);
1686 if (i
== TYPE_NFIELDS (variant_type
))
1687 /* We didn't find it. */
1688 error(_("Could not find field %s of struct variant %s"),
1689 field_name
, disr
.name
);
1691 do_cleanups (cleanup
);
1696 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1702 result
= rust_range (exp
, pos
, noside
);
1706 /* We might have &array[range], in which case we need to make a
1708 if (exp
->elts
[*pos
+ 1].opcode
== BINOP_SUBSCRIPT
)
1711 result
= rust_subscript (exp
, pos
, noside
, 1);
1716 result
= evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1723 /* operator_length implementation for Rust. */
1726 rust_operator_length (const struct expression
*exp
, int pc
, int *oplenp
,
1732 switch (exp
->elts
[pc
- 1].opcode
)
1735 /* We handle aggregate as a type and argument count. The first
1736 argument might be OP_OTHERS. After that the arguments
1737 alternate: first an OP_NAME, then an expression. */
1739 args
= longest_to_int (exp
->elts
[pc
- 2].longconst
);
1747 case STRUCTOP_ANONYMOUS
:
1758 operator_length_standard (exp
, pc
, oplenp
, argsp
);
1766 /* op_name implementation for Rust. */
1769 rust_op_name (enum exp_opcode opcode
)
1774 return "OP_AGGREGATE";
1778 return op_name_standard (opcode
);
1782 /* dump_subexp_body implementation for Rust. */
1785 rust_dump_subexp_body (struct expression
*exp
, struct ui_file
*stream
,
1788 switch (exp
->elts
[elt
].opcode
)
1792 int length
= longest_to_int (exp
->elts
[elt
+ 2].longconst
);
1795 fprintf_filtered (stream
, "Type @");
1796 gdb_print_host_address (exp
->elts
[elt
+ 1].type
, stream
);
1797 fprintf_filtered (stream
, " (");
1798 type_print (exp
->elts
[elt
+ 1].type
, NULL
, stream
, 0);
1799 fprintf_filtered (stream
, "), length %d", length
);
1802 for (i
= 0; i
< length
; ++i
)
1803 elt
= dump_subexp (exp
, stream
, elt
);
1810 LONGEST len
= exp
->elts
[elt
+ 1].longconst
;
1812 fprintf_filtered (stream
, "%s: %s",
1813 (exp
->elts
[elt
].opcode
== OP_STRING
1814 ? "string" : "name"),
1815 &exp
->elts
[elt
+ 2].string
);
1816 elt
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1821 elt
= dump_subexp (exp
, stream
, elt
+ 1);
1824 case STRUCTOP_ANONYMOUS
:
1828 field_number
= longest_to_int (exp
->elts
[elt
].longconst
);
1830 fprintf_filtered (stream
, "Field number: %d", field_number
);
1831 elt
= dump_subexp (exp
, stream
, elt
+ 2);
1839 elt
= dump_subexp_body_standard (exp
, stream
, elt
);
1846 /* print_subexp implementation for Rust. */
1849 rust_print_subexp (struct expression
*exp
, int *pos
, struct ui_file
*stream
,
1850 enum precedence prec
)
1852 switch (exp
->elts
[*pos
].opcode
)
1856 int length
= longest_to_int (exp
->elts
[*pos
+ 2].longconst
);
1859 type_print (exp
->elts
[*pos
+ 1].type
, "", stream
, 0);
1860 fputs_filtered (" { ", stream
);
1863 for (i
= 0; i
< length
; ++i
)
1865 rust_print_subexp (exp
, pos
, stream
, prec
);
1866 fputs_filtered (", ", stream
);
1868 fputs_filtered (" }", stream
);
1874 LONGEST len
= exp
->elts
[*pos
+ 1].longconst
;
1876 fputs_filtered (&exp
->elts
[*pos
+ 2].string
, stream
);
1877 *pos
+= 4 + BYTES_TO_EXP_ELEM (len
+ 1);
1883 fputs_filtered ("<<others>> (", stream
);
1885 rust_print_subexp (exp
, pos
, stream
, prec
);
1886 fputs_filtered (")", stream
);
1890 case STRUCTOP_ANONYMOUS
:
1892 int tem
= longest_to_int (exp
->elts
[*pos
+ 1].longconst
);
1895 print_subexp (exp
, pos
, stream
, PREC_SUFFIX
);
1896 fprintf_filtered (stream
, ".%d", tem
);
1902 fprintf_filtered (stream
, "[");
1903 rust_print_subexp (exp
, pos
, stream
, prec
);
1904 fprintf_filtered (stream
, "; ");
1905 rust_print_subexp (exp
, pos
, stream
, prec
);
1906 fprintf_filtered (stream
, "]");
1910 print_subexp_standard (exp
, pos
, stream
, prec
);
1915 /* operator_check implementation for Rust. */
1918 rust_operator_check (struct expression
*exp
, int pos
,
1919 int (*objfile_func
) (struct objfile
*objfile
,
1923 switch (exp
->elts
[pos
].opcode
)
1927 struct type
*type
= exp
->elts
[pos
+ 1].type
;
1928 struct objfile
*objfile
= TYPE_OBJFILE (type
);
1930 if (objfile
!= NULL
&& (*objfile_func
) (objfile
, data
))
1941 return operator_check_standard (exp
, pos
, objfile_func
, data
);
1949 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1951 static struct block_symbol
1952 rust_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
1954 const struct block
*block
,
1955 const domain_enum domain
)
1957 struct block_symbol result
= {NULL
, NULL
};
1959 if (symbol_lookup_debug
)
1961 fprintf_unfiltered (gdb_stdlog
,
1962 "rust_lookup_symbol_non_local"
1963 " (%s, %s (scope %s), %s)\n",
1964 name
, host_address_to_string (block
),
1965 block_scope (block
), domain_name (domain
));
1968 /* Look up bare names in the block's scope. */
1969 if (name
[cp_find_first_component (name
)] == '\0')
1971 const char *scope
= block_scope (block
);
1973 if (scope
[0] != '\0')
1975 char *scopedname
= concat (scope
, "::", name
, (char *) NULL
);
1976 struct cleanup
*cleanup
= make_cleanup (xfree
, scopedname
);
1978 result
= lookup_symbol_in_static_block (scopedname
, block
,
1980 if (result
.symbol
== NULL
)
1981 result
= lookup_global_symbol (scopedname
, block
, domain
);
1982 do_cleanups (cleanup
);
1990 static const struct exp_descriptor exp_descriptor_rust
=
1993 rust_operator_length
,
1994 rust_operator_check
,
1996 rust_dump_subexp_body
,
1997 rust_evaluate_subexp
2000 static const struct language_defn rust_language_defn
=
2009 &exp_descriptor_rust
,
2013 rust_printchar
, /* Print a character constant */
2014 rust_printstr
, /* Function to print string constant */
2015 rust_emitchar
, /* Print a single char */
2016 rust_print_type
, /* Print a type using appropriate syntax */
2017 rust_print_typedef
, /* Print a typedef using appropriate syntax */
2018 rust_val_print
, /* Print a value using appropriate syntax */
2019 c_value_print
, /* Print a top-level value */
2020 default_read_var_value
, /* la_read_var_value */
2021 NULL
, /* Language specific skip_trampoline */
2022 NULL
, /* name_of_this */
2023 rust_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
2024 basic_lookup_transparent_type
,/* lookup_transparent_type */
2025 gdb_demangle
, /* Language specific symbol demangler */
2026 NULL
, /* Language specific
2027 class_name_from_physname */
2028 c_op_print_tab
, /* expression operators for printing */
2029 1, /* c-style arrays */
2030 0, /* String lower bound */
2031 default_word_break_characters
,
2032 default_make_symbol_completion_list
,
2033 rust_language_arch_info
,
2034 default_print_array_index
,
2035 default_pass_by_reference
,
2037 NULL
, /* la_get_symbol_name_cmp */
2038 iterate_over_symbols
,
2039 &default_varobj_ops
,
2046 _initialize_rust_language (void)
2048 add_language (&rust_language_defn
);