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 *tail, *token, *name, *saveptr = NULL;
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 for (token = strtok_r (tail, "$", &saveptr);
150 token = strtok_r (NULL, "$", &saveptr))
152 if (sscanf (token, "%lu", &fieldno) != 1)
154 /* We have reached the enum name, which cannot start
158 if (fieldno >= TYPE_NFIELDS (member_type))
159 error (_("%s refers to field after end of member type"),
162 embedded_offset += TYPE_FIELD_BITPOS (member_type, fieldno) / 8;
163 member_type = TYPE_FIELD_TYPE (member_type, fieldno);
167 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
168 value = unpack_long (member_type, valaddr + embedded_offset);
172 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
173 ret.name = concat (TYPE_NAME (type), "::", token, (char *) NULL);
177 ret.field_no = RUST_ENCODED_ENUM_REAL;
178 ret.name = concat (TYPE_NAME (type), "::",
179 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))),
183 do_cleanups (cleanup);
187 disr_type = TYPE_FIELD_TYPE (type, 0);
189 if (TYPE_NFIELDS (disr_type) == 0)
191 /* This is a bounds check and should never be hit unless Rust
192 has changed its debuginfo format. */
193 error (_("Could not find enum discriminant field"));
196 if (strcmp (TYPE_FIELD_NAME (disr_type, 0), "RUST$ENUM$DISR") != 0)
197 error (_("Rust debug format has changed"));
199 temp_file = mem_fileopen ();
200 cleanup = make_cleanup_ui_file_delete (temp_file);
201 /* The first value of the first field (or any field)
202 is the discriminant value. */
203 c_val_print (TYPE_FIELD_TYPE (disr_type, 0), valaddr,
204 (embedded_offset + TYPE_FIELD_BITPOS (type, 0) / 8
205 + TYPE_FIELD_BITPOS (disr_type, 0) / 8),
209 ret.name = ui_file_xstrdup (temp_file, NULL);
210 name_segment = rust_last_path_segment (ret.name);
211 if (name_segment != NULL)
213 for (i = 0; i < TYPE_NFIELDS (type); ++i)
215 /* Sadly, the discriminant value paths do not match the type
216 field name paths ('core::option::Option::Some' vs
217 'core::option::Some'). However, enum variant names are
218 unique in the last path segment and the generics are not
219 part of this path, so we can just compare those. This is
220 hackish and would be better fixed by improving rustc's
221 metadata for enums. */
222 const char *field_type = TYPE_NAME (TYPE_FIELD_TYPE (type, i));
224 if (field_type != NULL
225 && strcmp (name_segment,
226 rust_last_path_segment (field_type)) == 0)
234 if (ret.field_no == -1 && ret.name != NULL)
236 /* Somehow the discriminant wasn't found. */
237 make_cleanup (xfree, ret.name);
238 error (_("Could not find variant of %s with discriminant %s"),
239 TYPE_TAG_NAME (type), ret.name);
242 do_cleanups (cleanup);
246 /* See rust-lang.h. */
249 rust_tuple_type_p (struct type *type)
251 /* The current implementation is a bit of a hack, but there's
252 nothing else in the debuginfo to distinguish a tuple from a
254 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
255 && TYPE_TAG_NAME (type) != NULL
256 && TYPE_TAG_NAME (type)[0] == '(');
260 /* Return true if all non-static fields of a structlike type are in a
261 sequence like __0, __1, __2. OFFSET lets us skip fields. */
264 rust_underscore_fields (struct type *type, int offset)
270 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
272 for (i = 0; i < TYPE_NFIELDS (type); ++i)
274 if (!field_is_static (&TYPE_FIELD (type, i)))
282 xsnprintf (buf, sizeof (buf), "__%d", field_number);
283 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
292 /* See rust-lang.h. */
295 rust_tuple_struct_type_p (struct type *type)
297 /* This is just an approximation until DWARF can represent Rust more
298 precisely. We exclude zero-length structs because they may not
299 be tuple structs, and there's no way to tell. */
300 return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type, 0);
303 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
306 rust_tuple_variant_type_p (struct type *type)
308 /* First field is discriminant */
309 return rust_underscore_fields (type, 1);
312 /* Return true if TYPE is a slice type, otherwise false. */
315 rust_slice_type_p (struct type *type)
317 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
318 && TYPE_TAG_NAME (type) != NULL
319 && strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0);
322 /* Return true if TYPE is a range type, otherwise false. */
325 rust_range_type_p (struct type *type)
329 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
330 || TYPE_NFIELDS (type) > 2
331 || TYPE_TAG_NAME (type) == NULL
332 || strstr (TYPE_TAG_NAME (type), "::Range") == NULL)
335 if (TYPE_NFIELDS (type) == 0)
339 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
341 if (TYPE_NFIELDS (type) == 1)
345 else if (TYPE_NFIELDS (type) == 2)
347 /* First field had to be "start". */
351 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
354 /* Return true if TYPE seems to be the type "u8", otherwise false. */
357 rust_u8_type_p (struct type *type)
359 return (TYPE_CODE (type) == TYPE_CODE_INT
360 && TYPE_UNSIGNED (type)
361 && TYPE_LENGTH (type) == 1);
364 /* Return true if TYPE is a Rust character type. */
367 rust_chartype_p (struct type *type)
369 return (TYPE_CODE (type) == TYPE_CODE_CHAR
370 && TYPE_LENGTH (type) == 4
371 && TYPE_UNSIGNED (type));
376 /* la_emitchar implementation for Rust. */
379 rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
381 if (!rust_chartype_p (type))
382 generic_emit_char (c, type, stream, quoter,
383 target_charset (get_type_arch (type)));
384 else if (c == '\\' || c == quoter)
385 fprintf_filtered (stream, "\\%c", c);
387 fputs_filtered ("\\n", stream);
389 fputs_filtered ("\\r", stream);
391 fputs_filtered ("\\t", stream);
393 fputs_filtered ("\\0", stream);
394 else if (c >= 32 && c <= 127 && isprint (c))
395 fputc_filtered (c, stream);
397 fprintf_filtered (stream, "\\x%02x", c);
399 fprintf_filtered (stream, "\\u{%06x}", c);
402 /* la_printchar implementation for Rust. */
405 rust_printchar (int c, struct type *type, struct ui_file *stream)
407 fputs_filtered ("'", stream);
408 LA_EMIT_CHAR (c, type, stream, '\'');
409 fputs_filtered ("'", stream);
412 /* la_printstr implementation for Rust. */
415 rust_printstr (struct ui_file *stream, struct type *type,
416 const gdb_byte *string, unsigned int length,
417 const char *user_encoding, int force_ellipses,
418 const struct value_print_options *options)
420 /* Rust always uses UTF-8, but let the caller override this if need
422 const char *encoding = user_encoding;
423 if (user_encoding == NULL || !*user_encoding)
425 /* In Rust strings, characters are "u8". */
426 if (rust_u8_type_p (type))
430 /* This is probably some C string, so let's let C deal with
432 c_printstr (stream, type, string, length, user_encoding,
433 force_ellipses, options);
438 /* This is not ideal as it doesn't use our character printer. */
439 generic_printstr (stream, type, string, length, encoding, force_ellipses,
445 static const struct generic_val_print_decorations rust_decorations =
447 /* Complex isn't used in Rust, but we provide C-ish values just in
459 /* la_val_print implementation for Rust. */
462 rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
463 CORE_ADDR address, struct ui_file *stream, int recurse,
464 const struct value *val,
465 const struct value_print_options *options)
467 type = check_typedef (type);
468 switch (TYPE_CODE (type))
472 LONGEST low_bound, high_bound;
474 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
475 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
476 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
478 /* We have a pointer to a byte string, so just print
480 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
482 struct gdbarch *arch = get_type_arch (type);
483 int unit_size = gdbarch_addressable_memory_unit_size (arch);
485 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
486 if (options->addressprint)
488 fputs_filtered (paddress (arch, addr), stream);
489 fputs_filtered (" ", stream);
492 fputs_filtered ("b", stream);
493 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
494 high_bound - low_bound + 1, stream,
501 case TYPE_CODE_METHODPTR:
502 case TYPE_CODE_MEMBERPTR:
503 c_val_print (type, valaddr, embedded_offset, address, stream,
504 recurse, val, options);
508 /* Recognize the unit type. */
509 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
510 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
512 fputs_filtered ("()", stream);
517 case TYPE_CODE_STRING:
519 struct gdbarch *arch = get_type_arch (type);
520 int unit_size = gdbarch_addressable_memory_unit_size (arch);
521 LONGEST low_bound, high_bound;
523 if (!get_array_bounds (type, &low_bound, &high_bound))
524 error (_("Could not determine the array bounds"));
526 /* If we see a plain TYPE_CODE_STRING, then we're printing a
527 byte string, hence the choice of "ASCII" as the
529 fputs_filtered ("b", stream);
530 rust_printstr (stream, TYPE_TARGET_TYPE (type),
531 valaddr + embedded_offset * unit_size,
532 high_bound - low_bound + 1, "ASCII", 0, options);
536 case TYPE_CODE_ARRAY:
538 LONGEST low_bound, high_bound;
540 if (get_array_bounds (type, &low_bound, &high_bound)
541 && high_bound - low_bound + 1 == 0)
542 fputs_filtered ("[]", stream);
548 case TYPE_CODE_UNION:
550 int j, nfields, first_field, is_tuple, start;
551 struct type *variant_type;
552 struct disr_info disr;
553 struct value_print_options opts;
554 struct cleanup *cleanup;
559 disr = rust_get_disr_info (type, valaddr, embedded_offset, address,
561 cleanup = make_cleanup (xfree, disr.name);
563 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
565 fprintf_filtered (stream, "%s", disr.name);
570 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
571 nfields = TYPE_NFIELDS (variant_type);
573 is_tuple = (disr.is_encoded
574 ? rust_tuple_struct_type_p (variant_type)
575 : rust_tuple_variant_type_p (variant_type));
576 start = disr.is_encoded ? 0 : 1;
580 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
582 fprintf_filtered (stream, "%s(", disr.name);
585 /* struct variant. */
586 fprintf_filtered (stream, "%s{", disr.name);
591 /* In case of a nullary variant like 'None', just output
593 fprintf_filtered (stream, "%s", disr.name);
597 for (j = start; j < TYPE_NFIELDS (variant_type); j++)
600 fputs_filtered (", ", stream);
604 fprintf_filtered (stream, "%s: ",
605 TYPE_FIELD_NAME (variant_type, j));
607 val_print (TYPE_FIELD_TYPE (variant_type, j),
610 + TYPE_FIELD_BITPOS (type, disr.field_no) / 8
611 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
613 stream, recurse + 1, val, &opts,
618 fputs_filtered (")", stream);
620 fputs_filtered ("}", stream);
623 do_cleanups (cleanup);
627 case TYPE_CODE_STRUCT:
631 int is_tuple = rust_tuple_type_p (type);
632 int is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
633 struct value_print_options opts;
637 if (TYPE_TAG_NAME (type) != NULL)
638 fprintf_filtered (stream, "%s", TYPE_TAG_NAME (type));
640 if (TYPE_NFIELDS (type) == 0)
643 if (TYPE_TAG_NAME (type) != NULL)
644 fputs_filtered (" ", stream);
647 if (is_tuple || is_tuple_struct)
648 fputs_filtered ("(", stream);
650 fputs_filtered ("{", stream);
656 for (i = 0; i < TYPE_NFIELDS (type); ++i)
658 if (field_is_static (&TYPE_FIELD (type, i)))
662 fputs_filtered (",", stream);
664 if (options->prettyformat)
666 fputs_filtered ("\n", stream);
667 print_spaces_filtered (2 + 2 * recurse, stream);
669 else if (!first_field)
670 fputs_filtered (" ", stream);
674 if (!is_tuple && !is_tuple_struct)
676 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
677 fputs_filtered (": ", stream);
680 val_print (TYPE_FIELD_TYPE (type, i),
682 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
684 stream, recurse + 1, val, &opts,
688 if (options->prettyformat)
690 fputs_filtered ("\n", stream);
691 print_spaces_filtered (2 * recurse, stream);
694 if (is_tuple || is_tuple_struct)
695 fputs_filtered (")", stream);
697 fputs_filtered ("}", stream);
703 /* Nothing special yet. */
704 generic_val_print (type, valaddr, embedded_offset, address, stream,
705 recurse, val, options, &rust_decorations);
711 /* la_print_typedef implementation for Rust. */
714 rust_print_typedef (struct type *type,
715 struct symbol *new_symbol,
716 struct ui_file *stream)
718 type = check_typedef (type);
719 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
720 type_print (type, "", stream, 0);
721 fprintf_filtered (stream, ";\n");
724 /* la_print_type implementation for Rust. */
727 rust_print_type (struct type *type, const char *varstring,
728 struct ui_file *stream, int show, int level,
729 const struct type_print_options *flags)
735 && TYPE_NAME (type) != NULL)
737 /* Rust calls the unit type "void" in its debuginfo,
738 but we don't want to print it as that. */
739 if (TYPE_CODE (type) == TYPE_CODE_VOID)
740 fputs_filtered ("()", stream);
742 fputs_filtered (TYPE_NAME (type), stream);
746 type = check_typedef (type);
747 switch (TYPE_CODE (type))
750 fputs_filtered ("()", stream);
754 /* Delegate varargs to the C printer. */
755 if (TYPE_VARARGS (type))
758 fputs_filtered ("fn ", stream);
759 if (varstring != NULL)
760 fputs_filtered (varstring, stream);
761 fputs_filtered ("(", stream);
762 for (i = 0; i < TYPE_NFIELDS (type); ++i)
766 fputs_filtered (", ", stream);
767 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
770 fputs_filtered (")", stream);
771 /* If it returns unit, we can omit the return type. */
772 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
774 fputs_filtered (" -> ", stream);
775 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
779 case TYPE_CODE_ARRAY:
781 LONGEST low_bound, high_bound;
783 fputs_filtered ("[", stream);
784 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
785 stream, show - 1, level, flags);
786 fputs_filtered ("; ", stream);
788 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
789 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
790 fprintf_filtered (stream, "variable length");
791 else if (get_array_bounds (type, &low_bound, &high_bound))
792 fprintf_filtered (stream, "%s",
793 plongest (high_bound - low_bound + 1));
794 fputs_filtered ("]", stream);
798 case TYPE_CODE_STRUCT:
802 /* Print a tuple type simply. */
803 if (rust_tuple_type_p (type))
805 fputs_filtered (TYPE_TAG_NAME (type), stream);
809 /* If we see a base class, delegate to C. */
810 if (TYPE_N_BASECLASSES (type) > 0)
813 fputs_filtered ("struct ", stream);
814 if (TYPE_TAG_NAME (type) != NULL)
815 fputs_filtered (TYPE_TAG_NAME (type), stream);
817 is_tuple_struct = rust_tuple_struct_type_p (type);
819 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
821 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
823 for (i = 0; i < TYPE_NFIELDS (type); ++i)
828 if (field_is_static (&TYPE_FIELD (type, i)))
831 /* We'd like to print "pub" here as needed, but rustc
832 doesn't emit the debuginfo, and our types don't have
833 cplus_struct_type attached. */
835 /* For a tuple struct we print the type but nothing
837 print_spaces_filtered (level + 2, stream);
838 if (!is_tuple_struct)
839 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
841 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
842 stream, show - 1, level + 2,
844 fputs_filtered (",\n", stream);
847 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
855 fputs_filtered ("enum ", stream);
856 if (TYPE_TAG_NAME (type) != NULL)
858 fputs_filtered (TYPE_TAG_NAME (type), stream);
859 fputs_filtered (" ", stream);
860 len = strlen (TYPE_TAG_NAME (type));
862 fputs_filtered ("{\n", stream);
864 for (i = 0; i < TYPE_NFIELDS (type); ++i)
866 const char *name = TYPE_FIELD_NAME (type, i);
871 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
873 && name[len + 1] == ':')
875 fprintfi_filtered (level + 2, stream, "%s,\n", name);
878 fputs_filtered ("}", stream);
882 case TYPE_CODE_UNION:
886 /* Skip the discriminant field. */
889 fputs_filtered ("enum ", stream);
890 if (TYPE_TAG_NAME (type) != NULL)
892 fputs_filtered (TYPE_TAG_NAME (type), stream);
893 fputs_filtered (" ", stream);
895 fputs_filtered ("{\n", stream);
897 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
898 strlen (RUST_ENUM_PREFIX)) == 0)
900 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
901 if (zero_field != NULL && strlen (zero_field) > 1)
903 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
904 /* There is no explicit discriminant field, skip nothing. */
909 for (i = 0; i < TYPE_NFIELDS (type); ++i)
911 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
913 = rust_last_path_segment (TYPE_NAME (variant_type));
915 fprintfi_filtered (level + 2, stream, "%s", name);
917 if (TYPE_NFIELDS (variant_type) > skip_to)
920 int is_tuple = rust_tuple_variant_type_p (variant_type);
923 fputs_filtered (is_tuple ? "(" : "{", stream);
924 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
929 fputs_filtered (", ", stream);
932 fprintf_filtered (stream, "%s: ",
933 TYPE_FIELD_NAME (variant_type, j));
935 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
936 stream, show - 1, level + 2,
939 fputs_filtered (is_tuple ? ")" : "}", stream);
942 fputs_filtered (",\n", stream);
945 fputs_filtered ("}", stream);
951 c_print_type (type, varstring, stream, show, level, flags);
957 /* Compute the alignment of the type T. */
960 rust_type_alignment (struct type *t)
962 t = check_typedef (t);
963 switch (TYPE_CODE (t))
966 error (_("Could not compute alignment of type"));
975 return TYPE_LENGTH (t);
977 case TYPE_CODE_ARRAY:
978 case TYPE_CODE_COMPLEX:
979 return rust_type_alignment (TYPE_TARGET_TYPE (t));
981 case TYPE_CODE_STRUCT:
982 case TYPE_CODE_UNION:
987 for (i = 0; i < TYPE_NFIELDS (t); ++i)
989 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
998 /* Like arch_composite_type, but uses TYPE to decide how to allocate
999 -- either on an obstack or on a gdbarch. */
1001 static struct type *
1002 rust_composite_type (struct type *original,
1004 const char *field1, struct type *type1,
1005 const char *field2, struct type *type2)
1007 struct type *result = alloc_type_copy (original);
1008 int i, nfields, bitpos;
1016 TYPE_CODE (result) = TYPE_CODE_STRUCT;
1017 TYPE_NAME (result) = name;
1018 TYPE_TAG_NAME (result) = name;
1020 TYPE_NFIELDS (result) = nfields;
1021 TYPE_FIELDS (result)
1022 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1028 struct field *field = &TYPE_FIELD (result, i);
1030 SET_FIELD_BITPOS (*field, bitpos);
1031 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1033 FIELD_NAME (*field) = field1;
1034 FIELD_TYPE (*field) = type1;
1039 struct field *field = &TYPE_FIELD (result, i);
1040 int align = rust_type_alignment (type2);
1046 align *= TARGET_CHAR_BIT;
1047 delta = bitpos % align;
1049 bitpos += align - delta;
1051 SET_FIELD_BITPOS (*field, bitpos);
1053 FIELD_NAME (*field) = field2;
1054 FIELD_TYPE (*field) = type2;
1059 TYPE_LENGTH (result)
1060 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1061 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1065 /* See rust-lang.h. */
1068 rust_slice_type (const char *name, struct type *elt_type,
1069 struct type *usize_type)
1073 elt_type = lookup_pointer_type (elt_type);
1074 type = rust_composite_type (elt_type, name,
1075 "data_ptr", elt_type,
1076 "length", usize_type);
1081 enum rust_primitive_types
1083 rust_primitive_bool,
1084 rust_primitive_char,
1093 rust_primitive_isize,
1094 rust_primitive_usize,
1097 rust_primitive_unit,
1099 nr_rust_primitive_types
1102 /* la_language_arch_info implementation for Rust. */
1105 rust_language_arch_info (struct gdbarch *gdbarch,
1106 struct language_arch_info *lai)
1108 const struct builtin_type *builtin = builtin_type (gdbarch);
1110 struct type **types;
1111 unsigned int length;
1113 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1116 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1117 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1118 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1119 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1120 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1121 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1122 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1123 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1124 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1125 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1127 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1128 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1129 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1131 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1132 floatformats_ieee_single);
1133 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1134 floatformats_ieee_double);
1136 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1138 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1139 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1140 types[rust_primitive_usize]);
1142 lai->primitive_type_vector = types;
1143 lai->bool_type_default = types[rust_primitive_bool];
1144 lai->string_char_type = types[rust_primitive_u8];
1149 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1151 static struct value *
1152 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1155 int num_args = exp->elts[*pos + 1].longconst;
1158 struct value *function, *result, *arg0;
1159 struct value **args;
1160 struct cleanup *cleanup;
1161 struct type *type, *fn_type;
1162 const struct block *block;
1163 struct block_symbol sym;
1165 /* For an ordinary function call we can simply defer to the
1166 generic implementation. */
1167 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1168 return evaluate_subexp_standard (NULL, exp, pos, noside);
1170 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1172 method = &exp->elts[*pos + 1].string;
1173 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1175 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1176 type in order to look up the method. */
1177 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1179 if (noside == EVAL_SKIP)
1181 for (i = 0; i < num_args; ++i)
1182 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1186 args = XNEWVEC (struct value *, num_args + 1);
1187 cleanup = make_cleanup (xfree, args);
1190 /* We don't yet implement real Deref semantics. */
1191 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1192 args[0] = value_ind (args[0]);
1194 type = value_type (args[0]);
1195 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1196 && TYPE_CODE (type) != TYPE_CODE_UNION
1197 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1198 || rust_tuple_type_p (type))
1199 error (_("Method calls only supported on struct or enum types"));
1200 if (TYPE_TAG_NAME (type) == NULL)
1201 error (_("Method call on nameless type"));
1203 name = concat (TYPE_TAG_NAME (type), "::", method, (char *) NULL);
1204 make_cleanup (xfree, name);
1206 block = get_selected_block (0);
1207 sym = lookup_symbol (name, block, VAR_DOMAIN, NULL);
1208 if (sym.symbol == NULL)
1209 error (_("Could not find function named '%s'"), name);
1211 fn_type = SYMBOL_TYPE (sym.symbol);
1212 if (TYPE_NFIELDS (fn_type) == 0)
1213 error (_("Function '%s' takes no arguments"), name);
1215 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1216 args[0] = value_addr (args[0]);
1218 function = address_of_variable (sym.symbol, block);
1220 for (i = 0; i < num_args; ++i)
1221 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1223 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1224 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1226 result = call_function_by_hand (function, num_args + 1, args);
1227 do_cleanups (cleanup);
1231 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1233 static struct value *
1234 rust_range (struct expression *exp, int *pos, enum noside noside)
1236 enum range_type kind;
1237 struct value *low = NULL, *high = NULL;
1238 struct value *addrval, *result;
1240 struct type *range_type;
1241 struct type *index_type;
1242 struct type *temp_type;
1245 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1248 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1249 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1250 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1251 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1253 if (noside == EVAL_SKIP)
1254 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1261 name = "std::ops::RangeFull";
1265 index_type = value_type (high);
1266 name = "std::ops::RangeTo";
1273 index_type = value_type (low);
1274 name = "std::ops::RangeFrom";
1278 if (!types_equal (value_type (low), value_type (high)))
1279 error (_("Range expression with different types"));
1280 index_type = value_type (low);
1281 name = "std::ops::Range";
1285 /* If we don't have an index type, just allocate this on the
1286 arch. Here any type will do. */
1287 temp_type = (index_type == NULL
1288 ? language_bool_type (exp->language_defn, exp->gdbarch)
1290 /* It would be nicer to cache the range type. */
1291 range_type = rust_composite_type (temp_type, name,
1292 low == NULL ? NULL : "start", index_type,
1293 high == NULL ? NULL : "end", index_type);
1295 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1296 return value_zero (range_type, lval_memory);
1298 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1299 addr = value_as_long (addrval);
1300 result = value_at_lazy (range_type, addr);
1304 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1307 value_assign (start, low);
1312 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1315 value_assign (end, high);
1318 result = value_at_lazy (range_type, addr);
1322 /* A helper function to compute the range and kind given a range
1323 value. TYPE is the type of the range value. RANGE is the range
1324 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1325 parameters might be filled in, or might not be, depending on the
1326 kind of range this is. KIND will always be set to the appropriate
1327 value describing the kind of range, and this can be used to
1328 determine whether LOW or HIGH are valid. */
1331 rust_compute_range (struct type *type, struct value *range,
1332 LONGEST *low, LONGEST *high,
1333 enum range_type *kind)
1339 *kind = BOTH_BOUND_DEFAULT;
1341 if (TYPE_NFIELDS (type) == 0)
1345 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1347 *kind = HIGH_BOUND_DEFAULT;
1348 *low = value_as_long (value_field (range, 0));
1351 if (TYPE_NFIELDS (type) > i
1352 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1354 *kind = (*kind == BOTH_BOUND_DEFAULT
1355 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1356 *high = value_as_long (value_field (range, i));
1360 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1362 static struct value *
1363 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1366 struct value *lhs, *rhs, *result;
1367 struct type *rhstype;
1368 LONGEST low, high_bound;
1369 /* Initialized to appease the compiler. */
1370 enum range_type kind = BOTH_BOUND_DEFAULT;
1375 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1376 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1378 if (noside == EVAL_SKIP)
1381 rhstype = check_typedef (value_type (rhs));
1382 if (rust_range_type_p (rhstype))
1385 error (_("Can't take slice of array without '&'"));
1386 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1390 low = value_as_long (rhs);
1392 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1394 struct type *type = check_typedef (value_type (lhs));
1396 result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
1402 struct type *type = check_typedef (value_type (lhs));
1404 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1407 if (!get_array_bounds (type, &low_bound, &high_bound))
1408 error (_("Can't compute array bounds"));
1410 error (_("Found array with non-zero lower bound"));
1413 else if (rust_slice_type_p (type))
1417 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1418 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1420 high_bound = value_as_long (len);
1422 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1426 high_bound = LONGEST_MAX;
1429 error (_("Cannot subscript non-array type"));
1432 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1435 error (_("Index less than zero"));
1436 if (low > high_bound)
1437 error (_("Index greater than length"));
1439 result = value_subscript (base, low);
1446 struct type *usize, *slice;
1448 struct value *addrval, *tem;
1450 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1453 error (_("High index less than zero"));
1455 error (_("Low index greater than high index"));
1456 if (high > high_bound)
1457 error (_("High index greater than length"));
1459 usize = language_lookup_primitive_type (exp->language_defn,
1462 slice = rust_slice_type ("&[*gdb*]", value_type (result),
1465 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1466 addr = value_as_long (addrval);
1467 tem = value_at_lazy (slice, addr);
1469 value_assign (value_field (tem, 0), value_addr (result));
1470 value_assign (value_field (tem, 1),
1471 value_from_longest (usize, high - low));
1473 result = value_at_lazy (slice, addr);
1476 result = value_addr (result);
1482 /* evaluate_exp implementation for Rust. */
1484 static struct value *
1485 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1486 int *pos, enum noside noside)
1488 struct value *result;
1490 switch (exp->elts[*pos].opcode)
1492 case UNOP_COMPLEMENT:
1494 struct value *value;
1497 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1498 if (noside == EVAL_SKIP)
1500 /* Preserving the type is enough. */
1503 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1504 result = value_from_longest (value_type (value),
1505 value_logical_not (value));
1507 result = value_complement (value);
1511 case BINOP_SUBSCRIPT:
1512 result = rust_subscript (exp, pos, noside, 0);
1516 result = rust_evaluate_funcall (exp, pos, noside);
1522 struct type *type = exp->elts[pc + 1].type;
1523 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1526 struct value *addrval = NULL;
1530 if (noside == EVAL_NORMAL)
1532 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1533 addr = value_as_long (addrval);
1534 result = value_at_lazy (type, addr);
1537 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1542 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1543 if (noside == EVAL_NORMAL)
1545 /* This isn't quite right but will do for the time
1546 being, seeing that we can't implement the Copy
1548 value_assign (result, init);
1554 gdb_assert (arglen % 2 == 0);
1555 for (i = 0; i < arglen; i += 2)
1558 const char *fieldname;
1559 struct value *value, *field;
1561 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1563 len = longest_to_int (exp->elts[*pos].longconst);
1565 fieldname = &exp->elts[*pos].string;
1566 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1568 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1569 if (noside == EVAL_NORMAL)
1571 field = value_struct_elt (&result, NULL, fieldname, NULL,
1573 value_assign (field, value);
1577 if (noside == EVAL_SKIP)
1578 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1580 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1581 result = allocate_value (type);
1583 result = value_at_lazy (type, addr);
1592 struct value *ncopies;
1594 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1595 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1596 copies = value_as_long (ncopies);
1598 error (_("Array with negative number of elements"));
1600 if (noside == EVAL_NORMAL)
1604 struct value **eltvec = XNEWVEC (struct value *, copies);
1605 struct cleanup *cleanup = make_cleanup (xfree, eltvec);
1607 for (i = 0; i < copies; ++i)
1609 result = value_array (0, copies - 1, eltvec);
1611 do_cleanups (cleanup);
1615 struct type *arraytype
1616 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1617 result = allocate_value (arraytype);
1622 case STRUCTOP_ANONYMOUS:
1624 /* Anonymous field access, i.e. foo.1. */
1626 int pc, field_number, nfields;
1627 struct type *type, *variant_type;
1628 struct disr_info disr;
1631 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1633 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1635 type = value_type (lhs);
1636 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1638 struct cleanup *cleanup;
1640 disr = rust_get_disr_info (type, value_contents (lhs),
1641 value_embedded_offset (lhs),
1642 value_address (lhs), lhs);
1644 cleanup = make_cleanup (xfree, disr.name);
1646 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1648 variant_type = NULL;
1653 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1654 nfields = TYPE_NFIELDS (variant_type);
1657 if (!disr.is_encoded)
1660 if (field_number >= nfields || field_number < 0)
1661 error(_("Cannot access field %d of variant %s, \
1662 there are only %d fields"),
1663 disr.is_encoded ? field_number : field_number - 1,
1665 disr.is_encoded ? nfields : nfields - 1);
1667 if (!(disr.is_encoded
1668 ? rust_tuple_struct_type_p (variant_type)
1669 : rust_tuple_variant_type_p (variant_type)))
1670 error(_("Variant %s is not a tuple variant"), disr.name);
1672 result = value_primitive_field (lhs, 0, field_number,
1674 do_cleanups (cleanup);
1676 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1678 /* Tuples and tuple structs */
1679 nfields = TYPE_NFIELDS(type);
1681 if (field_number >= nfields || field_number < 0)
1682 error(_("Cannot access field %d of %s, there are only %d fields"),
1683 field_number, TYPE_TAG_NAME (type), nfields);
1685 /* Tuples are tuple structs too. */
1686 if (!rust_tuple_struct_type_p (type))
1687 error(_("Attempting to access anonymous field %d of %s, which is \
1688 not a tuple, tuple struct, or tuple-like variant"),
1689 field_number, TYPE_TAG_NAME (type));
1691 result = value_primitive_field (lhs, 0, field_number, type);
1694 error(_("Anonymous field access is only allowed on tuples, \
1695 tuple structs, and tuple-like enum variants"));
1699 case STRUCTOP_STRUCT:
1706 tem = longest_to_int (exp->elts[pc + 1].longconst);
1707 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1708 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1710 type = value_type (lhs);
1712 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1715 struct disr_info disr;
1716 struct cleanup* cleanup;
1717 struct type* variant_type;
1720 field_name = &exp->elts[pc + 2].string;
1722 disr = rust_get_disr_info (type, value_contents (lhs),
1723 value_embedded_offset (lhs),
1724 value_address (lhs), lhs);
1726 cleanup = make_cleanup (xfree, disr.name);
1728 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1729 error(_("Could not find field %s of struct variant %s"),
1730 field_name, disr.name);
1732 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1734 if (variant_type == NULL
1735 || rust_tuple_variant_type_p (variant_type))
1736 error(_("Attempting to access named field %s of tuple variant %s, \
1737 which has only anonymous fields"),
1738 field_name, disr.name);
1740 start = disr.is_encoded ? 0 : 1;
1741 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1743 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1745 result = value_primitive_field (lhs, 0, i, variant_type);
1750 if (i == TYPE_NFIELDS (variant_type))
1751 /* We didn't find it. */
1752 error(_("Could not find field %s of struct variant %s"),
1753 field_name, disr.name);
1755 do_cleanups (cleanup);
1760 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1766 result = rust_range (exp, pos, noside);
1770 /* We might have &array[range], in which case we need to make a
1772 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1775 result = rust_subscript (exp, pos, noside, 1);
1780 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1787 /* operator_length implementation for Rust. */
1790 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1796 switch (exp->elts[pc - 1].opcode)
1799 /* We handle aggregate as a type and argument count. The first
1800 argument might be OP_OTHERS. After that the arguments
1801 alternate: first an OP_NAME, then an expression. */
1803 args = longest_to_int (exp->elts[pc - 2].longconst);
1811 case STRUCTOP_ANONYMOUS:
1822 operator_length_standard (exp, pc, oplenp, argsp);
1830 /* op_name implementation for Rust. */
1833 rust_op_name (enum exp_opcode opcode)
1838 return "OP_AGGREGATE";
1842 return op_name_standard (opcode);
1846 /* dump_subexp_body implementation for Rust. */
1849 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1852 switch (exp->elts[elt].opcode)
1856 int length = longest_to_int (exp->elts[elt + 2].longconst);
1859 fprintf_filtered (stream, "Type @");
1860 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1861 fprintf_filtered (stream, " (");
1862 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1863 fprintf_filtered (stream, "), length %d", length);
1866 for (i = 0; i < length; ++i)
1867 elt = dump_subexp (exp, stream, elt);
1874 LONGEST len = exp->elts[elt + 1].longconst;
1876 fprintf_filtered (stream, "%s: %s",
1877 (exp->elts[elt].opcode == OP_STRING
1878 ? "string" : "name"),
1879 &exp->elts[elt + 2].string);
1880 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1885 elt = dump_subexp (exp, stream, elt + 1);
1888 case STRUCTOP_ANONYMOUS:
1892 field_number = longest_to_int (exp->elts[elt].longconst);
1894 fprintf_filtered (stream, "Field number: %d", field_number);
1895 elt = dump_subexp (exp, stream, elt + 2);
1903 elt = dump_subexp_body_standard (exp, stream, elt);
1910 /* print_subexp implementation for Rust. */
1913 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1914 enum precedence prec)
1916 switch (exp->elts[*pos].opcode)
1920 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1923 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1924 fputs_filtered (" { ", stream);
1927 for (i = 0; i < length; ++i)
1929 rust_print_subexp (exp, pos, stream, prec);
1930 fputs_filtered (", ", stream);
1932 fputs_filtered (" }", stream);
1938 LONGEST len = exp->elts[*pos + 1].longconst;
1940 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1941 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1947 fputs_filtered ("<<others>> (", stream);
1949 rust_print_subexp (exp, pos, stream, prec);
1950 fputs_filtered (")", stream);
1954 case STRUCTOP_ANONYMOUS:
1956 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1959 print_subexp (exp, pos, stream, PREC_SUFFIX);
1960 fprintf_filtered (stream, ".%d", tem);
1966 fprintf_filtered (stream, "[");
1967 rust_print_subexp (exp, pos, stream, prec);
1968 fprintf_filtered (stream, "; ");
1969 rust_print_subexp (exp, pos, stream, prec);
1970 fprintf_filtered (stream, "]");
1974 print_subexp_standard (exp, pos, stream, prec);
1979 /* operator_check implementation for Rust. */
1982 rust_operator_check (struct expression *exp, int pos,
1983 int (*objfile_func) (struct objfile *objfile,
1987 switch (exp->elts[pos].opcode)
1991 struct type *type = exp->elts[pos + 1].type;
1992 struct objfile *objfile = TYPE_OBJFILE (type);
1994 if (objfile != NULL && (*objfile_func) (objfile, data))
2005 return operator_check_standard (exp, pos, objfile_func, data);
2013 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2015 static struct block_symbol
2016 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2018 const struct block *block,
2019 const domain_enum domain)
2021 struct block_symbol result = {NULL, NULL};
2023 if (symbol_lookup_debug)
2025 fprintf_unfiltered (gdb_stdlog,
2026 "rust_lookup_symbol_non_local"
2027 " (%s, %s (scope %s), %s)\n",
2028 name, host_address_to_string (block),
2029 block_scope (block), domain_name (domain));
2032 /* Look up bare names in the block's scope. */
2033 if (name[cp_find_first_component (name)] == '\0')
2035 const char *scope = block_scope (block);
2037 if (scope[0] != '\0')
2039 char *scopedname = concat (scope, "::", name, (char *) NULL);
2040 struct cleanup *cleanup = make_cleanup (xfree, scopedname);
2042 result = lookup_symbol_in_static_block (scopedname, block,
2044 if (result.symbol == NULL)
2045 result = lookup_global_symbol (scopedname, block, domain);
2046 do_cleanups (cleanup);
2054 /* la_sniff_from_mangled_name for Rust. */
2057 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2059 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2060 return *demangled != NULL;
2065 static const struct exp_descriptor exp_descriptor_rust =
2068 rust_operator_length,
2069 rust_operator_check,
2071 rust_dump_subexp_body,
2072 rust_evaluate_subexp
2075 static const char *rust_extensions[] =
2080 static const struct language_defn rust_language_defn =
2090 &exp_descriptor_rust,
2094 rust_printchar, /* Print a character constant */
2095 rust_printstr, /* Function to print string constant */
2096 rust_emitchar, /* Print a single char */
2097 rust_print_type, /* Print a type using appropriate syntax */
2098 rust_print_typedef, /* Print a typedef using appropriate syntax */
2099 rust_val_print, /* Print a value using appropriate syntax */
2100 c_value_print, /* Print a top-level value */
2101 default_read_var_value, /* la_read_var_value */
2102 NULL, /* Language specific skip_trampoline */
2103 NULL, /* name_of_this */
2104 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2105 basic_lookup_transparent_type,/* lookup_transparent_type */
2106 gdb_demangle, /* Language specific symbol demangler */
2107 rust_sniff_from_mangled_name,
2108 NULL, /* Language specific
2109 class_name_from_physname */
2110 c_op_print_tab, /* expression operators for printing */
2111 1, /* c-style arrays */
2112 0, /* String lower bound */
2113 default_word_break_characters,
2114 default_make_symbol_completion_list,
2115 rust_language_arch_info,
2116 default_print_array_index,
2117 default_pass_by_reference,
2119 NULL, /* la_get_symbol_name_cmp */
2120 iterate_over_symbols,
2121 &default_varobj_ops,
2128 _initialize_rust_language (void)
2130 add_language (&rust_language_defn);