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 fputs_filtered (TYPE_NAME (type), stream);
736 type = check_typedef (type);
737 switch (TYPE_CODE (type))
740 /* Delegate varargs to the C printer. */
741 if (TYPE_VARARGS (type))
744 fputs_filtered ("fn ", stream);
745 if (varstring != NULL)
746 fputs_filtered (varstring, stream);
747 fputs_filtered ("(", stream);
748 for (i = 0; i < TYPE_NFIELDS (type); ++i)
752 fputs_filtered (", ", stream);
753 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
756 fputs_filtered (") -> ", stream);
757 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
760 case TYPE_CODE_ARRAY:
762 LONGEST low_bound, high_bound;
764 fputs_filtered ("[", stream);
765 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
766 stream, show - 1, level, flags);
767 fputs_filtered ("; ", stream);
769 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
770 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
771 fprintf_filtered (stream, "variable length");
772 else if (get_array_bounds (type, &low_bound, &high_bound))
773 fprintf_filtered (stream, "%s",
774 plongest (high_bound - low_bound + 1));
775 fputs_filtered ("]", stream);
779 case TYPE_CODE_STRUCT:
783 /* Print a tuple type simply. */
784 if (rust_tuple_type_p (type))
786 fputs_filtered (TYPE_TAG_NAME (type), stream);
790 /* If we see a base class, delegate to C. */
791 if (TYPE_N_BASECLASSES (type) > 0)
794 fputs_filtered ("struct ", stream);
795 if (TYPE_TAG_NAME (type) != NULL)
796 fputs_filtered (TYPE_TAG_NAME (type), stream);
798 is_tuple_struct = rust_tuple_struct_type_p (type);
800 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
802 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
804 for (i = 0; i < TYPE_NFIELDS (type); ++i)
809 if (field_is_static (&TYPE_FIELD (type, i)))
812 /* We'd like to print "pub" here as needed, but rustc
813 doesn't emit the debuginfo, and our types don't have
814 cplus_struct_type attached. */
816 /* For a tuple struct we print the type but nothing
818 print_spaces_filtered (level + 2, stream);
819 if (!is_tuple_struct)
820 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
822 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
823 stream, show - 1, level + 2,
825 fputs_filtered (",\n", stream);
828 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
836 fputs_filtered ("enum ", stream);
837 if (TYPE_TAG_NAME (type) != NULL)
839 fputs_filtered (TYPE_TAG_NAME (type), stream);
840 fputs_filtered (" ", stream);
841 len = strlen (TYPE_TAG_NAME (type));
843 fputs_filtered ("{\n", stream);
845 for (i = 0; i < TYPE_NFIELDS (type); ++i)
847 const char *name = TYPE_FIELD_NAME (type, i);
852 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
854 && name[len + 1] == ':')
856 fprintfi_filtered (level + 2, stream, "%s,\n", name);
859 fputs_filtered ("}", stream);
863 case TYPE_CODE_UNION:
867 /* Skip the discriminant field. */
870 fputs_filtered ("enum ", stream);
871 if (TYPE_TAG_NAME (type) != NULL)
873 fputs_filtered (TYPE_TAG_NAME (type), stream);
874 fputs_filtered (" ", stream);
875 len = strlen (TYPE_TAG_NAME (type));
877 fputs_filtered ("{\n", stream);
879 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
880 strlen (RUST_ENUM_PREFIX)) == 0)
882 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
883 if (zero_field != NULL && strlen (zero_field) > 1)
885 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
886 /* There is no explicit discriminant field, skip nothing. */
891 for (i = 0; i < TYPE_NFIELDS (type); ++i)
893 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
895 = rust_last_path_segment (TYPE_NAME (variant_type));
897 fprintfi_filtered (level + 2, stream, "%s", name);
899 if (TYPE_NFIELDS (variant_type) > skip_to)
902 int is_tuple = rust_tuple_variant_type_p (variant_type);
905 fputs_filtered (is_tuple ? "(" : "{", stream);
906 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
911 fputs_filtered (", ", stream);
914 fprintf_filtered (stream, "%s: ",
915 TYPE_FIELD_NAME (variant_type, j));
917 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
918 stream, show - 1, level + 2,
921 fputs_filtered (is_tuple ? ")" : "}", stream);
924 fputs_filtered (",\n", stream);
927 fputs_filtered ("}", stream);
933 c_print_type (type, varstring, stream, show, level, flags);
939 /* Compute the alignment of the type T. */
942 rust_type_alignment (struct type *t)
944 t = check_typedef (t);
945 switch (TYPE_CODE (t))
948 error (_("Could not compute alignment of type"));
957 return TYPE_LENGTH (t);
959 case TYPE_CODE_ARRAY:
960 case TYPE_CODE_COMPLEX:
961 return rust_type_alignment (TYPE_TARGET_TYPE (t));
963 case TYPE_CODE_STRUCT:
964 case TYPE_CODE_UNION:
969 for (i = 0; i < TYPE_NFIELDS (t); ++i)
971 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
980 /* Like arch_composite_type, but uses TYPE to decide how to allocate
981 -- either on an obstack or on a gdbarch. */
984 rust_composite_type (struct type *original,
986 const char *field1, struct type *type1,
987 const char *field2, struct type *type2)
989 struct type *result = alloc_type_copy (original);
990 int i, nfields, bitpos;
998 TYPE_CODE (result) = TYPE_CODE_STRUCT;
999 TYPE_NAME (result) = name;
1000 TYPE_TAG_NAME (result) = name;
1002 TYPE_NFIELDS (result) = nfields;
1003 TYPE_FIELDS (result)
1004 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1010 struct field *field = &TYPE_FIELD (result, i);
1012 SET_FIELD_BITPOS (*field, bitpos);
1013 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1015 FIELD_NAME (*field) = field1;
1016 FIELD_TYPE (*field) = type1;
1021 struct field *field = &TYPE_FIELD (result, i);
1022 int align = rust_type_alignment (type2);
1028 align *= TARGET_CHAR_BIT;
1029 delta = bitpos % align;
1031 bitpos += align - delta;
1033 SET_FIELD_BITPOS (*field, bitpos);
1035 FIELD_NAME (*field) = field2;
1036 FIELD_TYPE (*field) = type2;
1041 TYPE_LENGTH (result)
1042 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1043 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1047 /* See rust-lang.h. */
1050 rust_slice_type (const char *name, struct type *elt_type,
1051 struct type *usize_type)
1055 elt_type = lookup_pointer_type (elt_type);
1056 type = rust_composite_type (elt_type, name,
1057 "data_ptr", elt_type,
1058 "length", usize_type);
1063 enum rust_primitive_types
1065 rust_primitive_bool,
1066 rust_primitive_char,
1075 rust_primitive_isize,
1076 rust_primitive_usize,
1079 rust_primitive_unit,
1081 nr_rust_primitive_types
1084 /* la_language_arch_info implementation for Rust. */
1087 rust_language_arch_info (struct gdbarch *gdbarch,
1088 struct language_arch_info *lai)
1090 const struct builtin_type *builtin = builtin_type (gdbarch);
1092 struct type **types;
1093 unsigned int length;
1095 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1098 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1099 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1100 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1101 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1102 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1103 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1104 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1105 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1106 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1107 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1109 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1110 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1111 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1113 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32", NULL);
1114 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64", NULL);
1116 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1118 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1119 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1120 types[rust_primitive_usize]);
1122 lai->primitive_type_vector = types;
1123 lai->bool_type_default = types[rust_primitive_bool];
1124 lai->string_char_type = types[rust_primitive_u8];
1129 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1131 static struct value *
1132 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1135 int num_args = exp->elts[*pos + 1].longconst;
1138 struct value *function, *result, *arg0;
1139 struct value **args;
1140 struct cleanup *cleanup;
1141 struct type *type, *fn_type;
1142 const struct block *block;
1143 struct block_symbol sym;
1145 /* For an ordinary function call we can simply defer to the
1146 generic implementation. */
1147 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1148 return evaluate_subexp_standard (NULL, exp, pos, noside);
1150 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1152 method = &exp->elts[*pos + 1].string;
1153 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1155 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1156 type in order to look up the method. */
1157 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1159 if (noside == EVAL_SKIP)
1161 for (i = 0; i < num_args; ++i)
1162 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1166 args = XNEWVEC (struct value *, num_args + 1);
1167 cleanup = make_cleanup (xfree, args);
1170 /* We don't yet implement real Deref semantics. */
1171 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1172 args[0] = value_ind (args[0]);
1174 type = value_type (args[0]);
1175 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1176 && TYPE_CODE (type) != TYPE_CODE_UNION
1177 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1178 || rust_tuple_type_p (type))
1179 error (_("Method calls only supported on struct or enum types"));
1180 if (TYPE_TAG_NAME (type) == NULL)
1181 error (_("Method call on nameless type"));
1183 name = concat (TYPE_TAG_NAME (type), "::", method, (char *) NULL);
1184 make_cleanup (xfree, name);
1186 block = get_selected_block (0);
1187 sym = lookup_symbol (name, block, VAR_DOMAIN, NULL);
1188 if (sym.symbol == NULL)
1189 error (_("Could not find function named '%s'"), name);
1191 fn_type = SYMBOL_TYPE (sym.symbol);
1192 if (TYPE_NFIELDS (fn_type) == 0)
1193 error (_("Function '%s' takes no arguments"), name);
1195 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1196 args[0] = value_addr (args[0]);
1198 function = address_of_variable (sym.symbol, block);
1200 for (i = 0; i < num_args; ++i)
1201 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1203 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1204 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1206 result = call_function_by_hand (function, num_args + 1, args);
1207 do_cleanups (cleanup);
1211 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1213 static struct value *
1214 rust_range (struct expression *exp, int *pos, enum noside noside)
1216 enum range_type kind;
1217 struct value *low = NULL, *high = NULL;
1218 struct value *addrval, *result;
1220 struct type *range_type;
1221 struct type *index_type;
1222 struct type *temp_type;
1225 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1228 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1229 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1230 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1231 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1233 if (noside == EVAL_SKIP)
1234 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1241 name = "std::ops::RangeFull";
1245 index_type = value_type (high);
1246 name = "std::ops::RangeTo";
1253 index_type = value_type (low);
1254 name = "std::ops::RangeFrom";
1258 if (!types_equal (value_type (low), value_type (high)))
1259 error (_("Range expression with different types"));
1260 index_type = value_type (low);
1261 name = "std::ops::Range";
1265 /* If we don't have an index type, just allocate this on the
1266 arch. Here any type will do. */
1267 temp_type = (index_type == NULL
1268 ? language_bool_type (exp->language_defn, exp->gdbarch)
1270 /* It would be nicer to cache the range type. */
1271 range_type = rust_composite_type (temp_type, name,
1272 low == NULL ? NULL : "start", index_type,
1273 high == NULL ? NULL : "end", index_type);
1275 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1276 return value_zero (range_type, lval_memory);
1278 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1279 addr = value_as_long (addrval);
1280 result = value_at_lazy (range_type, addr);
1284 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1287 value_assign (start, low);
1292 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1295 value_assign (end, high);
1298 result = value_at_lazy (range_type, addr);
1302 /* A helper function to compute the range and kind given a range
1303 value. TYPE is the type of the range value. RANGE is the range
1304 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1305 parameters might be filled in, or might not be, depending on the
1306 kind of range this is. KIND will always be set to the appropriate
1307 value describing the kind of range, and this can be used to
1308 determine whether LOW or HIGH are valid. */
1311 rust_compute_range (struct type *type, struct value *range,
1312 LONGEST *low, LONGEST *high,
1313 enum range_type *kind)
1319 *kind = BOTH_BOUND_DEFAULT;
1321 if (TYPE_NFIELDS (type) == 0)
1325 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1327 *kind = HIGH_BOUND_DEFAULT;
1328 *low = value_as_long (value_field (range, 0));
1331 if (TYPE_NFIELDS (type) > i
1332 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1334 *kind = (*kind == BOTH_BOUND_DEFAULT
1335 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1336 *high = value_as_long (value_field (range, i));
1340 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1342 static struct value *
1343 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1346 struct value *lhs, *rhs, *result;
1347 struct type *rhstype;
1348 LONGEST low, high_bound;
1349 /* Initialized to appease the compiler. */
1350 enum range_type kind = BOTH_BOUND_DEFAULT;
1355 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1356 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1358 if (noside == EVAL_SKIP)
1361 rhstype = check_typedef (value_type (rhs));
1362 if (rust_range_type_p (rhstype))
1365 error (_("Can't take slice of array without '&'"));
1366 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1370 low = value_as_long (rhs);
1372 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1374 struct type *type = check_typedef (value_type (lhs));
1376 result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
1382 struct type *type = check_typedef (value_type (lhs));
1384 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1387 if (!get_array_bounds (type, &low_bound, &high_bound))
1388 error (_("Can't compute array bounds"));
1390 error (_("Found array with non-zero lower bound"));
1393 else if (rust_slice_type_p (type))
1397 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1398 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1400 high_bound = value_as_long (len);
1403 error (_("Cannot subscript non-array type"));
1406 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1409 error (_("Index less than zero"));
1410 if (low > high_bound)
1411 error (_("Index greater than length"));
1413 result = value_subscript (base, low);
1420 struct type *usize, *slice;
1422 struct value *addrval, *tem;
1424 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1427 error (_("High index less than zero"));
1429 error (_("Low index greater than high index"));
1430 if (high > high_bound)
1431 error (_("High index greater than length"));
1433 usize = language_lookup_primitive_type (exp->language_defn,
1436 slice = rust_slice_type ("&[*gdb*]", value_type (result),
1439 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1440 addr = value_as_long (addrval);
1441 tem = value_at_lazy (slice, addr);
1443 value_assign (value_field (tem, 0), value_addr (result));
1444 value_assign (value_field (tem, 1),
1445 value_from_longest (usize, high - low));
1447 result = value_at_lazy (slice, addr);
1450 result = value_addr (result);
1456 /* evaluate_exp implementation for Rust. */
1458 static struct value *
1459 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1460 int *pos, enum noside noside)
1462 struct value *result;
1464 switch (exp->elts[*pos].opcode)
1466 case UNOP_COMPLEMENT:
1468 struct value *value;
1471 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1472 if (noside == EVAL_SKIP)
1474 /* Preserving the type is enough. */
1477 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1478 result = value_from_longest (value_type (value),
1479 value_logical_not (value));
1481 result = value_complement (value);
1485 case BINOP_SUBSCRIPT:
1486 result = rust_subscript (exp, pos, noside, 0);
1490 result = rust_evaluate_funcall (exp, pos, noside);
1496 struct type *type = exp->elts[pc + 1].type;
1497 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1500 struct value *addrval = NULL;
1504 if (noside == EVAL_NORMAL)
1506 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1507 addr = value_as_long (addrval);
1508 result = value_at_lazy (type, addr);
1511 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1516 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1517 if (noside == EVAL_NORMAL)
1519 /* This isn't quite right but will do for the time
1520 being, seeing that we can't implement the Copy
1522 value_assign (result, init);
1528 gdb_assert (arglen % 2 == 0);
1529 for (i = 0; i < arglen; i += 2)
1532 const char *fieldname;
1533 struct value *value, *field;
1535 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1537 len = longest_to_int (exp->elts[*pos].longconst);
1539 fieldname = &exp->elts[*pos].string;
1540 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1542 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1543 if (noside == EVAL_NORMAL)
1545 field = value_struct_elt (&result, NULL, fieldname, NULL,
1547 value_assign (field, value);
1551 if (noside == EVAL_SKIP)
1552 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1554 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1555 result = allocate_value (type);
1557 result = value_at_lazy (type, addr);
1566 struct value *ncopies;
1568 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1569 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1570 copies = value_as_long (ncopies);
1572 error (_("Array with negative number of elements"));
1574 if (noside == EVAL_NORMAL)
1578 struct value **eltvec = XNEWVEC (struct value *, copies);
1579 struct cleanup *cleanup = make_cleanup (xfree, eltvec);
1581 for (i = 0; i < copies; ++i)
1583 result = value_array (0, copies - 1, eltvec);
1585 do_cleanups (cleanup);
1589 struct type *arraytype
1590 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1591 result = allocate_value (arraytype);
1596 case STRUCTOP_ANONYMOUS:
1598 /* Anonymous field access, i.e. foo.1. */
1600 int pc, field_number, nfields;
1601 struct type *type, *variant_type;
1602 struct disr_info disr;
1605 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1607 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1609 type = value_type (lhs);
1610 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1612 struct cleanup *cleanup;
1614 disr = rust_get_disr_info (type, value_contents (lhs),
1615 value_embedded_offset (lhs),
1616 value_address (lhs), lhs);
1618 cleanup = make_cleanup (xfree, disr.name);
1620 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1622 variant_type = NULL;
1627 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1628 nfields = TYPE_NFIELDS (variant_type);
1631 if (!disr.is_encoded)
1634 if (field_number >= nfields || field_number < 0)
1635 error(_("Cannot access field %d of variant %s, \
1636 there are only %d fields"),
1637 disr.is_encoded ? field_number : field_number - 1,
1639 disr.is_encoded ? nfields : nfields - 1);
1641 if (!(disr.is_encoded
1642 ? rust_tuple_struct_type_p (variant_type)
1643 : rust_tuple_variant_type_p (variant_type)))
1644 error(_("Variant %s is not a tuple variant"), disr.name);
1646 result = value_primitive_field (lhs, 0, field_number,
1648 do_cleanups (cleanup);
1650 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1652 /* Tuples and tuple structs */
1653 nfields = TYPE_NFIELDS(type);
1655 if (field_number >= nfields || field_number < 0)
1656 error(_("Cannot access field %d of %s, there are only %d fields"),
1657 field_number, TYPE_TAG_NAME (type), nfields);
1659 /* Tuples are tuple structs too. */
1660 if (!rust_tuple_struct_type_p (type))
1661 error(_("Attempting to access anonymous field %d of %s, which is \
1662 not a tuple, tuple struct, or tuple-like variant"),
1663 field_number, TYPE_TAG_NAME (type));
1665 result = value_primitive_field (lhs, 0, field_number, type);
1668 error(_("Anonymous field access is only allowed on tuples, \
1669 tuple structs, and tuple-like enum variants"));
1673 case STRUCTOP_STRUCT:
1680 tem = longest_to_int (exp->elts[pc + 1].longconst);
1681 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1682 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1684 type = value_type (lhs);
1686 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1689 struct disr_info disr;
1690 struct cleanup* cleanup;
1691 struct type* variant_type;
1694 field_name = &exp->elts[pc + 2].string;
1696 disr = rust_get_disr_info (type, value_contents (lhs),
1697 value_embedded_offset (lhs),
1698 value_address (lhs), lhs);
1700 cleanup = make_cleanup (xfree, disr.name);
1702 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1703 error(_("Could not find field %s of struct variant %s"),
1704 field_name, disr.name);
1706 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1708 if (variant_type == NULL
1709 || rust_tuple_variant_type_p (variant_type))
1710 error(_("Attempting to access named field %s of tuple variant %s, \
1711 which has only anonymous fields"),
1712 field_name, disr.name);
1714 start = disr.is_encoded ? 0 : 1;
1715 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1717 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1719 result = value_primitive_field (lhs, 0, i, variant_type);
1724 if (i == TYPE_NFIELDS (variant_type))
1725 /* We didn't find it. */
1726 error(_("Could not find field %s of struct variant %s"),
1727 field_name, disr.name);
1729 do_cleanups (cleanup);
1734 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1740 result = rust_range (exp, pos, noside);
1744 /* We might have &array[range], in which case we need to make a
1746 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1749 result = rust_subscript (exp, pos, noside, 1);
1754 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1761 /* operator_length implementation for Rust. */
1764 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1770 switch (exp->elts[pc - 1].opcode)
1773 /* We handle aggregate as a type and argument count. The first
1774 argument might be OP_OTHERS. After that the arguments
1775 alternate: first an OP_NAME, then an expression. */
1777 args = longest_to_int (exp->elts[pc - 2].longconst);
1785 case STRUCTOP_ANONYMOUS:
1796 operator_length_standard (exp, pc, oplenp, argsp);
1804 /* op_name implementation for Rust. */
1807 rust_op_name (enum exp_opcode opcode)
1812 return "OP_AGGREGATE";
1816 return op_name_standard (opcode);
1820 /* dump_subexp_body implementation for Rust. */
1823 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1826 switch (exp->elts[elt].opcode)
1830 int length = longest_to_int (exp->elts[elt + 2].longconst);
1833 fprintf_filtered (stream, "Type @");
1834 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1835 fprintf_filtered (stream, " (");
1836 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1837 fprintf_filtered (stream, "), length %d", length);
1840 for (i = 0; i < length; ++i)
1841 elt = dump_subexp (exp, stream, elt);
1848 LONGEST len = exp->elts[elt + 1].longconst;
1850 fprintf_filtered (stream, "%s: %s",
1851 (exp->elts[elt].opcode == OP_STRING
1852 ? "string" : "name"),
1853 &exp->elts[elt + 2].string);
1854 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1859 elt = dump_subexp (exp, stream, elt + 1);
1862 case STRUCTOP_ANONYMOUS:
1866 field_number = longest_to_int (exp->elts[elt].longconst);
1868 fprintf_filtered (stream, "Field number: %d", field_number);
1869 elt = dump_subexp (exp, stream, elt + 2);
1877 elt = dump_subexp_body_standard (exp, stream, elt);
1884 /* print_subexp implementation for Rust. */
1887 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1888 enum precedence prec)
1890 switch (exp->elts[*pos].opcode)
1894 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1897 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1898 fputs_filtered (" { ", stream);
1901 for (i = 0; i < length; ++i)
1903 rust_print_subexp (exp, pos, stream, prec);
1904 fputs_filtered (", ", stream);
1906 fputs_filtered (" }", stream);
1912 LONGEST len = exp->elts[*pos + 1].longconst;
1914 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1915 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1921 fputs_filtered ("<<others>> (", stream);
1923 rust_print_subexp (exp, pos, stream, prec);
1924 fputs_filtered (")", stream);
1928 case STRUCTOP_ANONYMOUS:
1930 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1933 print_subexp (exp, pos, stream, PREC_SUFFIX);
1934 fprintf_filtered (stream, ".%d", tem);
1940 fprintf_filtered (stream, "[");
1941 rust_print_subexp (exp, pos, stream, prec);
1942 fprintf_filtered (stream, "; ");
1943 rust_print_subexp (exp, pos, stream, prec);
1944 fprintf_filtered (stream, "]");
1948 print_subexp_standard (exp, pos, stream, prec);
1953 /* operator_check implementation for Rust. */
1956 rust_operator_check (struct expression *exp, int pos,
1957 int (*objfile_func) (struct objfile *objfile,
1961 switch (exp->elts[pos].opcode)
1965 struct type *type = exp->elts[pos + 1].type;
1966 struct objfile *objfile = TYPE_OBJFILE (type);
1968 if (objfile != NULL && (*objfile_func) (objfile, data))
1979 return operator_check_standard (exp, pos, objfile_func, data);
1987 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1989 static struct block_symbol
1990 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
1992 const struct block *block,
1993 const domain_enum domain)
1995 struct block_symbol result = {NULL, NULL};
1997 if (symbol_lookup_debug)
1999 fprintf_unfiltered (gdb_stdlog,
2000 "rust_lookup_symbol_non_local"
2001 " (%s, %s (scope %s), %s)\n",
2002 name, host_address_to_string (block),
2003 block_scope (block), domain_name (domain));
2006 /* Look up bare names in the block's scope. */
2007 if (name[cp_find_first_component (name)] == '\0')
2009 const char *scope = block_scope (block);
2011 if (scope[0] != '\0')
2013 char *scopedname = concat (scope, "::", name, (char *) NULL);
2014 struct cleanup *cleanup = make_cleanup (xfree, scopedname);
2016 result = lookup_symbol_in_static_block (scopedname, block,
2018 if (result.symbol == NULL)
2019 result = lookup_global_symbol (scopedname, block, domain);
2020 do_cleanups (cleanup);
2028 /* la_sniff_from_mangled_name for Rust. */
2031 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2033 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2034 return *demangled != NULL;
2039 static const struct exp_descriptor exp_descriptor_rust =
2042 rust_operator_length,
2043 rust_operator_check,
2045 rust_dump_subexp_body,
2046 rust_evaluate_subexp
2049 static const char *rust_extensions[] =
2054 static const struct language_defn rust_language_defn =
2064 &exp_descriptor_rust,
2068 rust_printchar, /* Print a character constant */
2069 rust_printstr, /* Function to print string constant */
2070 rust_emitchar, /* Print a single char */
2071 rust_print_type, /* Print a type using appropriate syntax */
2072 rust_print_typedef, /* Print a typedef using appropriate syntax */
2073 rust_val_print, /* Print a value using appropriate syntax */
2074 c_value_print, /* Print a top-level value */
2075 default_read_var_value, /* la_read_var_value */
2076 NULL, /* Language specific skip_trampoline */
2077 NULL, /* name_of_this */
2078 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2079 basic_lookup_transparent_type,/* lookup_transparent_type */
2080 gdb_demangle, /* Language specific symbol demangler */
2081 rust_sniff_from_mangled_name,
2082 NULL, /* Language specific
2083 class_name_from_physname */
2084 c_op_print_tab, /* expression operators for printing */
2085 1, /* c-style arrays */
2086 0, /* String lower bound */
2087 default_word_break_characters,
2088 default_make_symbol_completion_list,
2089 rust_language_arch_info,
2090 default_print_array_index,
2091 default_pass_by_reference,
2093 NULL, /* la_get_symbol_name_cmp */
2094 iterate_over_symbols,
2095 &default_varobj_ops,
2102 _initialize_rust_language (void)
2104 add_language (&rust_language_defn);