1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "cp-support.h"
31 #include "rust-lang.h"
35 extern initialize_file_ftype _initialize_rust_language;
37 /* Returns the last segment of a Rust path like foo::bar::baz. Will
38 not handle cases where the last segment contains generics. This
39 will return NULL if the last segment cannot be found. */
42 rust_last_path_segment (const char * path)
44 const char *result = strrchr (path, ':');
51 /* Find the Rust crate for BLOCK. If no crate can be found, returns
52 NULL. Otherwise, returns a newly allocated string that the caller
53 is responsible for freeing. */
56 rust_crate_for_block (const struct block *block)
58 const char *scope = block_scope (block);
63 return xstrndup (scope, cp_find_first_component (scope));
66 /* Information about the discriminant/variant of an enum */
70 /* Name of field. Must be freed by caller. */
72 /* Field number in union. Negative on error. For an encoded enum,
73 the "hidden" member will always be field 1, and the "real" member
74 will always be field 0. */
76 /* True if this is an encoded enum that has a single "real" member
77 and a single "hidden" member. */
78 unsigned int is_encoded : 1;
81 /* The prefix of a specially-encoded enum. */
83 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
85 /* The number of the real field. */
87 #define RUST_ENCODED_ENUM_REAL 0
89 /* The number of the hidden field. */
91 #define RUST_ENCODED_ENUM_HIDDEN 1
93 /* Utility function to get discriminant info for a given value. */
95 static struct disr_info
96 rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
97 int embedded_offset, CORE_ADDR address,
98 const struct value *val)
101 struct disr_info ret;
102 struct type *disr_type;
103 struct ui_file *temp_file;
104 struct value_print_options opts;
105 struct cleanup *cleanup;
106 const char *name_segment;
108 get_no_prettyformat_print_options (&opts);
113 if (TYPE_NFIELDS (type) == 0)
114 error (_("Encountered void enum value"));
116 /* If an enum has two values where one is empty and the other holds
117 a pointer that cannot be zero; then the Rust compiler optimizes
118 away the discriminant and instead uses a zero value in the
119 pointer field to indicate the empty variant. */
120 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
121 strlen (RUST_ENUM_PREFIX)) == 0)
124 unsigned long fieldno;
125 struct type *member_type;
130 if (TYPE_NFIELDS (type) != 1)
131 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX);
133 fieldno = strtoul (TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX),
136 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
138 member_type = TYPE_FIELD_TYPE (type, 0);
139 if (fieldno >= TYPE_NFIELDS (member_type))
140 error (_("%s refers to field after end of member type"),
143 embedded_offset += TYPE_FIELD_BITPOS (member_type, fieldno) / 8;
144 value = unpack_long (TYPE_FIELD_TYPE (member_type, fieldno),
145 valaddr + embedded_offset);
148 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
149 ret.name = concat (TYPE_NAME (type), "::", tail + 1, (char *) NULL);
153 ret.field_no = RUST_ENCODED_ENUM_REAL;
154 ret.name = concat (TYPE_NAME (type), "::",
155 rust_last_path_segment (TYPE_NAME (member_type)),
162 disr_type = TYPE_FIELD_TYPE (type, 0);
164 if (TYPE_NFIELDS (disr_type) == 0)
166 /* This is a bounds check and should never be hit unless Rust
167 has changed its debuginfo format. */
168 error (_("Could not find enum discriminant field"));
171 if (strcmp (TYPE_FIELD_NAME (disr_type, 0), "RUST$ENUM$DISR") != 0)
172 error (_("Rust debug format has changed"));
174 temp_file = mem_fileopen ();
175 cleanup = make_cleanup_ui_file_delete (temp_file);
176 /* The first value of the first field (or any field)
177 is the discriminant value. */
178 c_val_print (TYPE_FIELD_TYPE (disr_type, 0), valaddr,
179 (embedded_offset + TYPE_FIELD_BITPOS (type, 0) / 8
180 + TYPE_FIELD_BITPOS (disr_type, 0) / 8),
184 ret.name = ui_file_xstrdup (temp_file, NULL);
185 name_segment = rust_last_path_segment (ret.name);
186 if (name_segment != NULL)
188 for (i = 0; i < TYPE_NFIELDS (type); ++i)
190 /* Sadly, the discriminant value paths do not match the type
191 field name paths ('core::option::Option::Some' vs
192 'core::option::Some'). However, enum variant names are
193 unique in the last path segment and the generics are not
194 part of this path, so we can just compare those. This is
195 hackish and would be better fixed by improving rustc's
196 metadata for enums. */
197 const char *field_type = TYPE_NAME (TYPE_FIELD_TYPE (type, i));
199 if (field_type != NULL
200 && strcmp (name_segment,
201 rust_last_path_segment (field_type)) == 0)
209 if (ret.field_no == -1 && ret.name != NULL)
211 /* Somehow the discriminant wasn't found. */
212 make_cleanup (xfree, ret.name);
213 error (_("Could not find variant of %s with discriminant %s"),
214 TYPE_TAG_NAME (type), ret.name);
217 do_cleanups (cleanup);
221 /* See rust-lang.h. */
224 rust_tuple_type_p (struct type *type)
226 /* The current implementation is a bit of a hack, but there's
227 nothing else in the debuginfo to distinguish a tuple from a
229 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
230 && TYPE_TAG_NAME (type) != NULL
231 && TYPE_TAG_NAME (type)[0] == '(');
235 /* Return true if all non-static fields of a structlike type are in a
236 sequence like __0, __1, __2. OFFSET lets us skip fields. */
239 rust_underscore_fields (struct type *type, int offset)
245 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
247 for (i = 0; i < TYPE_NFIELDS (type); ++i)
249 if (!field_is_static (&TYPE_FIELD (type, i)))
257 xsnprintf (buf, sizeof (buf), "__%d", field_number);
258 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
267 /* See rust-lang.h. */
270 rust_tuple_struct_type_p (struct type *type)
272 return rust_underscore_fields (type, 0);
275 /* Return true if a variant TYPE is a tuple variant, false otherwise. */
278 rust_tuple_variant_type_p (struct type *type)
280 /* First field is discriminant */
281 return rust_underscore_fields (type, 1);
284 /* Return true if TYPE is a slice type, otherwise false. */
287 rust_slice_type_p (struct type *type)
289 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
290 && TYPE_TAG_NAME (type) != NULL
291 && strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0);
294 /* Return true if TYPE is a range type, otherwise false. */
297 rust_range_type_p (struct type *type)
301 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
302 || TYPE_NFIELDS (type) > 2
303 || TYPE_TAG_NAME (type) == NULL
304 || strstr (TYPE_TAG_NAME (type), "::Range") == NULL)
307 if (TYPE_NFIELDS (type) == 0)
311 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
313 if (TYPE_NFIELDS (type) == 1)
317 else if (TYPE_NFIELDS (type) == 2)
319 /* First field had to be "start". */
323 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
326 /* Return true if TYPE seems to be the type "u8", otherwise false. */
329 rust_u8_type_p (struct type *type)
331 return (TYPE_CODE (type) == TYPE_CODE_INT
332 && TYPE_UNSIGNED (type)
333 && TYPE_LENGTH (type) == 1);
336 /* Return true if TYPE is a Rust character type. */
339 rust_chartype_p (struct type *type)
341 return (TYPE_CODE (type) == TYPE_CODE_CHAR
342 && TYPE_LENGTH (type) == 4
343 && TYPE_UNSIGNED (type));
348 /* la_emitchar implementation for Rust. */
351 rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
353 if (!rust_chartype_p (type))
354 generic_emit_char (c, type, stream, quoter,
355 target_charset (get_type_arch (type)));
356 else if (c == '\\' || c == quoter)
357 fprintf_filtered (stream, "\\%c", c);
359 fputs_filtered ("\\n", stream);
361 fputs_filtered ("\\r", stream);
363 fputs_filtered ("\\t", stream);
365 fputs_filtered ("\\0", stream);
366 else if (c >= 32 && c <= 127 && isprint (c))
367 fputc_filtered (c, stream);
369 fprintf_filtered (stream, "\\x%02x", c);
371 fprintf_filtered (stream, "\\u{%06x}", c);
374 /* la_printchar implementation for Rust. */
377 rust_printchar (int c, struct type *type, struct ui_file *stream)
379 fputs_filtered ("'", stream);
380 LA_EMIT_CHAR (c, type, stream, '\'');
381 fputs_filtered ("'", stream);
384 /* la_printstr implementation for Rust. */
387 rust_printstr (struct ui_file *stream, struct type *type,
388 const gdb_byte *string, unsigned int length,
389 const char *user_encoding, int force_ellipses,
390 const struct value_print_options *options)
392 /* Rust always uses UTF-8, but let the caller override this if need
394 const char *encoding = user_encoding;
395 if (user_encoding == NULL || !*user_encoding)
397 /* In Rust strings, characters are "u8". */
398 if (rust_u8_type_p (type))
402 /* This is probably some C string, so let's let C deal with
404 c_printstr (stream, type, string, length, user_encoding,
405 force_ellipses, options);
410 /* This is not ideal as it doesn't use our character printer. */
411 generic_printstr (stream, type, string, length, encoding, force_ellipses,
417 static const struct generic_val_print_decorations rust_decorations =
419 /* Complex isn't used in Rust, but we provide C-ish values just in
431 /* la_val_print implementation for Rust. */
434 rust_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
435 CORE_ADDR address, struct ui_file *stream, int recurse,
436 const struct value *val,
437 const struct value_print_options *options)
439 type = check_typedef (type);
440 switch (TYPE_CODE (type))
444 LONGEST low_bound, high_bound;
446 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
447 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
448 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
450 /* We have a pointer to a byte string, so just print
452 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
454 struct gdbarch *arch = get_type_arch (type);
455 int unit_size = gdbarch_addressable_memory_unit_size (arch);
457 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
458 if (options->addressprint)
460 fputs_filtered (paddress (arch, addr), stream);
461 fputs_filtered (" ", stream);
464 fputs_filtered ("b", stream);
465 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
466 high_bound - low_bound + 1, stream,
473 case TYPE_CODE_METHODPTR:
474 case TYPE_CODE_MEMBERPTR:
475 c_val_print (type, valaddr, embedded_offset, address, stream,
476 recurse, val, options);
480 /* Recognize the unit type. */
481 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
482 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
484 fputs_filtered ("()", stream);
489 case TYPE_CODE_STRING:
491 struct gdbarch *arch = get_type_arch (type);
492 int unit_size = gdbarch_addressable_memory_unit_size (arch);
493 LONGEST low_bound, high_bound;
495 if (!get_array_bounds (type, &low_bound, &high_bound))
496 error (_("Could not determine the array bounds"));
498 /* If we see a plain TYPE_CODE_STRING, then we're printing a
499 byte string, hence the choice of "ASCII" as the
501 fputs_filtered ("b", stream);
502 rust_printstr (stream, TYPE_TARGET_TYPE (type),
503 valaddr + embedded_offset * unit_size,
504 high_bound - low_bound + 1, "ASCII", 0, options);
508 case TYPE_CODE_ARRAY:
510 LONGEST low_bound, high_bound;
512 if (get_array_bounds (type, &low_bound, &high_bound)
513 && high_bound - low_bound + 1 == 0)
514 fputs_filtered ("[]", stream);
520 case TYPE_CODE_UNION:
522 int j, nfields, first_field, is_tuple, start;
523 struct type *variant_type;
524 struct disr_info disr;
525 struct value_print_options opts;
526 struct cleanup *cleanup;
531 disr = rust_get_disr_info (type, valaddr, embedded_offset, address,
533 cleanup = make_cleanup (xfree, disr.name);
535 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
537 fprintf_filtered (stream, "%s", disr.name);
542 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
543 nfields = TYPE_NFIELDS (variant_type);
545 is_tuple = (disr.is_encoded
546 ? rust_tuple_struct_type_p (variant_type)
547 : rust_tuple_variant_type_p (variant_type));
548 start = disr.is_encoded ? 0 : 1;
552 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
554 fprintf_filtered (stream, "%s(", disr.name);
557 /* struct variant. */
558 fprintf_filtered (stream, "%s{", disr.name);
563 /* In case of a nullary variant like 'None', just output
565 fprintf_filtered (stream, "%s", disr.name);
569 for (j = start; j < TYPE_NFIELDS (variant_type); j++)
572 fputs_filtered (", ", stream);
576 fprintf_filtered (stream, "%s: ",
577 TYPE_FIELD_NAME (variant_type, j));
579 val_print (TYPE_FIELD_TYPE (variant_type, j),
582 + TYPE_FIELD_BITPOS (type, disr.field_no) / 8
583 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
585 stream, recurse + 1, val, &opts,
590 fputs_filtered (")", stream);
592 fputs_filtered ("}", stream);
595 do_cleanups (cleanup);
599 case TYPE_CODE_STRUCT:
603 int is_tuple = rust_tuple_type_p (type);
604 int is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
605 struct value_print_options opts;
609 if (TYPE_TAG_NAME (type) != NULL)
610 fprintf_filtered (stream, "%s", TYPE_TAG_NAME (type));
612 if (TYPE_NFIELDS (type) == 0)
615 if (TYPE_TAG_NAME (type) != NULL)
616 fputs_filtered (" ", stream);
619 if (is_tuple || is_tuple_struct)
620 fputs_filtered ("(", stream);
622 fputs_filtered ("{", stream);
628 for (i = 0; i < TYPE_NFIELDS (type); ++i)
630 if (field_is_static (&TYPE_FIELD (type, i)))
634 fputs_filtered (",", stream);
636 if (options->prettyformat)
638 fputs_filtered ("\n", stream);
639 print_spaces_filtered (2 + 2 * recurse, stream);
641 else if (!first_field)
642 fputs_filtered (" ", stream);
646 if (!is_tuple && !is_tuple_struct)
648 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
649 fputs_filtered (": ", stream);
652 val_print (TYPE_FIELD_TYPE (type, i),
654 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
656 stream, recurse + 1, val, &opts,
660 if (options->prettyformat)
662 fputs_filtered ("\n", stream);
663 print_spaces_filtered (2 * recurse, stream);
666 if (is_tuple || is_tuple_struct)
667 fputs_filtered (")", stream);
669 fputs_filtered ("}", stream);
675 /* Nothing special yet. */
676 generic_val_print (type, valaddr, embedded_offset, address, stream,
677 recurse, val, options, &rust_decorations);
683 /* la_print_typedef implementation for Rust. */
686 rust_print_typedef (struct type *type,
687 struct symbol *new_symbol,
688 struct ui_file *stream)
690 type = check_typedef (type);
691 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
692 type_print (type, "", stream, 0);
693 fprintf_filtered (stream, ";\n");
696 /* la_print_type implementation for Rust. */
699 rust_print_type (struct type *type, const char *varstring,
700 struct ui_file *stream, int show, int level,
701 const struct type_print_options *flags)
707 && TYPE_NAME (type) != NULL)
709 fputs_filtered (TYPE_NAME (type), stream);
713 type = check_typedef (type);
714 switch (TYPE_CODE (type))
717 /* Delegate varargs to the C printer. */
718 if (TYPE_VARARGS (type))
721 fputs_filtered ("fn ", stream);
722 if (varstring != NULL)
723 fputs_filtered (varstring, stream);
724 fputs_filtered ("(", stream);
725 for (i = 0; i < TYPE_NFIELDS (type); ++i)
729 fputs_filtered (", ", stream);
730 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
733 fputs_filtered (") -> ", stream);
734 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
737 case TYPE_CODE_ARRAY:
739 LONGEST low_bound, high_bound;
741 fputs_filtered ("[", stream);
742 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
743 stream, show - 1, level, flags);
744 fputs_filtered ("; ", stream);
746 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
747 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
748 fprintf_filtered (stream, "variable length");
749 else if (get_array_bounds (type, &low_bound, &high_bound))
750 fprintf_filtered (stream, "%s",
751 plongest (high_bound - low_bound + 1));
752 fputs_filtered ("]", stream);
756 case TYPE_CODE_STRUCT:
760 /* Print a tuple type simply. */
761 if (rust_tuple_type_p (type))
763 fputs_filtered (TYPE_TAG_NAME (type), stream);
767 /* If we see a base class, delegate to C. */
768 if (TYPE_N_BASECLASSES (type) > 0)
771 fputs_filtered ("struct ", stream);
772 if (TYPE_TAG_NAME (type) != NULL)
773 fputs_filtered (TYPE_TAG_NAME (type), stream);
775 is_tuple_struct = rust_tuple_struct_type_p (type);
777 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
779 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
781 for (i = 0; i < TYPE_NFIELDS (type); ++i)
786 if (field_is_static (&TYPE_FIELD (type, i)))
789 /* We'd like to print "pub" here as needed, but rustc
790 doesn't emit the debuginfo, and our types don't have
791 cplus_struct_type attached. */
793 /* For a tuple struct we print the type but nothing
795 print_spaces_filtered (level + 2, stream);
796 if (!is_tuple_struct)
797 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
799 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
800 stream, show - 1, level + 2,
802 fputs_filtered (",\n", stream);
805 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
813 fputs_filtered ("enum ", stream);
814 if (TYPE_TAG_NAME (type) != NULL)
816 fputs_filtered (TYPE_TAG_NAME (type), stream);
817 fputs_filtered (" ", stream);
818 len = strlen (TYPE_TAG_NAME (type));
820 fputs_filtered ("{\n", stream);
822 for (i = 0; i < TYPE_NFIELDS (type); ++i)
824 const char *name = TYPE_FIELD_NAME (type, i);
829 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
831 && name[len + 1] == ':')
833 fprintfi_filtered (level + 2, stream, "%s,\n", name);
836 fputs_filtered ("}", stream);
840 case TYPE_CODE_UNION:
845 fputs_filtered ("enum ", stream);
846 if (TYPE_TAG_NAME (type) != NULL)
848 fputs_filtered (TYPE_TAG_NAME (type), stream);
849 fputs_filtered (" ", stream);
850 len = strlen (TYPE_TAG_NAME (type));
852 fputs_filtered ("{\n", stream);
854 for (i = 0; i < TYPE_NFIELDS (type); ++i)
856 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
858 = rust_last_path_segment (TYPE_NAME (variant_type));
860 fprintfi_filtered (level + 2, stream, "%s", name);
862 if (TYPE_NFIELDS (variant_type) > 1)
865 int is_tuple = rust_tuple_variant_type_p (variant_type);
868 fputs_filtered (is_tuple ? "(" : "{", stream);
869 for (j = 1; j < TYPE_NFIELDS (variant_type); j++)
874 fputs_filtered (", ", stream);
877 fprintf_filtered (stream, "%s: ",
878 TYPE_FIELD_NAME (variant_type, j));
880 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
881 stream, show - 1, level + 2,
884 fputs_filtered (is_tuple ? ")" : "}", stream);
887 fputs_filtered (",\n", stream);
890 fputs_filtered ("}", stream);
896 c_print_type (type, varstring, stream, show, level, flags);
902 /* Compute the alignment of the type T. */
905 rust_type_alignment (struct type *t)
907 t = check_typedef (t);
908 switch (TYPE_CODE (t))
911 error (_("Could not compute alignment of type"));
920 return TYPE_LENGTH (t);
922 case TYPE_CODE_ARRAY:
923 case TYPE_CODE_COMPLEX:
924 return rust_type_alignment (TYPE_TARGET_TYPE (t));
926 case TYPE_CODE_STRUCT:
927 case TYPE_CODE_UNION:
932 for (i = 0; i < TYPE_NFIELDS (t); ++i)
934 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
943 /* Like arch_composite_type, but uses TYPE to decide how to allocate
944 -- either on an obstack or on a gdbarch. */
947 rust_composite_type (struct type *original,
949 const char *field1, struct type *type1,
950 const char *field2, struct type *type2)
952 struct type *result = alloc_type_copy (original);
953 int i, nfields, bitpos;
961 TYPE_CODE (result) = TYPE_CODE_STRUCT;
962 TYPE_NAME (result) = name;
963 TYPE_TAG_NAME (result) = name;
965 TYPE_NFIELDS (result) = nfields;
967 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
973 struct field *field = &TYPE_FIELD (result, i);
975 SET_FIELD_BITPOS (*field, bitpos);
976 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
978 FIELD_NAME (*field) = field1;
979 FIELD_TYPE (*field) = type1;
984 struct field *field = &TYPE_FIELD (result, i);
985 int align = rust_type_alignment (type2);
991 align *= TARGET_CHAR_BIT;
992 delta = bitpos % align;
994 bitpos += align - delta;
996 SET_FIELD_BITPOS (*field, bitpos);
998 FIELD_NAME (*field) = field2;
999 FIELD_TYPE (*field) = type2;
1004 TYPE_LENGTH (result)
1005 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1006 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1010 /* See rust-lang.h. */
1013 rust_slice_type (const char *name, struct type *elt_type,
1014 struct type *usize_type)
1018 elt_type = lookup_pointer_type (elt_type);
1019 type = rust_composite_type (elt_type, name,
1020 "data_ptr", elt_type,
1021 "length", usize_type);
1026 enum rust_primitive_types
1028 rust_primitive_bool,
1029 rust_primitive_char,
1038 rust_primitive_isize,
1039 rust_primitive_usize,
1042 rust_primitive_unit,
1044 nr_rust_primitive_types
1047 /* la_language_arch_info implementation for Rust. */
1050 rust_language_arch_info (struct gdbarch *gdbarch,
1051 struct language_arch_info *lai)
1053 const struct builtin_type *builtin = builtin_type (gdbarch);
1055 struct type **types;
1056 unsigned int length;
1058 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1061 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1062 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1063 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1064 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1065 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1066 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1067 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1068 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1069 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1070 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1072 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1073 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1074 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1076 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32", NULL);
1077 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64", NULL);
1079 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1081 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1082 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1083 types[rust_primitive_usize]);
1085 lai->primitive_type_vector = types;
1086 lai->bool_type_default = types[rust_primitive_bool];
1087 lai->string_char_type = types[rust_primitive_u8];
1092 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1094 static struct value *
1095 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1098 int num_args = exp->elts[*pos + 1].longconst;
1101 struct value *function, *result, *arg0;
1102 struct value **args;
1103 struct cleanup *cleanup;
1104 struct type *type, *fn_type;
1105 const struct block *block;
1106 struct block_symbol sym;
1108 /* For an ordinary function call we can simply defer to the
1109 generic implementation. */
1110 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1111 return evaluate_subexp_standard (NULL, exp, pos, noside);
1113 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1115 method = &exp->elts[*pos + 1].string;
1116 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1118 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1119 type in order to look up the method. */
1120 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1122 if (noside == EVAL_SKIP)
1124 for (i = 0; i < num_args; ++i)
1125 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1129 args = XNEWVEC (struct value *, num_args + 1);
1130 cleanup = make_cleanup (xfree, args);
1133 /* We don't yet implement real Deref semantics. */
1134 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1135 args[0] = value_ind (args[0]);
1137 type = value_type (args[0]);
1138 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1139 && TYPE_CODE (type) != TYPE_CODE_UNION
1140 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1141 || rust_tuple_type_p (type))
1142 error (_("Method calls only supported on struct or enum types"));
1143 if (TYPE_TAG_NAME (type) == NULL)
1144 error (_("Method call on nameless type"));
1146 name = concat (TYPE_TAG_NAME (type), "::", method, (char *) NULL);
1147 make_cleanup (xfree, name);
1149 block = get_selected_block (0);
1150 sym = lookup_symbol (name, block, VAR_DOMAIN, NULL);
1151 if (sym.symbol == NULL)
1152 error (_("Could not find function named '%s'"), name);
1154 fn_type = SYMBOL_TYPE (sym.symbol);
1155 if (TYPE_NFIELDS (fn_type) == 0)
1156 error (_("Function '%s' takes no arguments"), name);
1158 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1159 args[0] = value_addr (args[0]);
1161 function = address_of_variable (sym.symbol, block);
1163 for (i = 0; i < num_args; ++i)
1164 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1166 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1167 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1169 result = call_function_by_hand (function, num_args + 1, args);
1170 do_cleanups (cleanup);
1174 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1176 static struct value *
1177 rust_range (struct expression *exp, int *pos, enum noside noside)
1179 enum range_type kind;
1180 struct value *low = NULL, *high = NULL;
1181 struct value *addrval, *result;
1183 struct type *range_type;
1184 struct type *index_type;
1185 struct type *temp_type;
1188 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1191 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1192 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1193 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1194 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1196 if (noside == EVAL_SKIP)
1197 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1204 name = "std::ops::RangeFull";
1208 index_type = value_type (high);
1209 name = "std::ops::RangeTo";
1216 index_type = value_type (low);
1217 name = "std::ops::RangeFrom";
1221 if (!types_equal (value_type (low), value_type (high)))
1222 error (_("Range expression with different types"));
1223 index_type = value_type (low);
1224 name = "std::ops::Range";
1228 /* If we don't have an index type, just allocate this on the
1229 arch. Here any type will do. */
1230 temp_type = (index_type == NULL
1231 ? language_bool_type (exp->language_defn, exp->gdbarch)
1233 /* It would be nicer to cache the range type. */
1234 range_type = rust_composite_type (temp_type, name,
1235 low == NULL ? NULL : "start", index_type,
1236 high == NULL ? NULL : "end", index_type);
1238 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1239 return value_zero (range_type, lval_memory);
1241 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1242 addr = value_as_long (addrval);
1243 result = value_at_lazy (range_type, addr);
1247 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1250 value_assign (start, low);
1255 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1258 value_assign (end, high);
1261 result = value_at_lazy (range_type, addr);
1265 /* A helper function to compute the range and kind given a range
1266 value. TYPE is the type of the range value. RANGE is the range
1267 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1268 parameters might be filled in, or might not be, depending on the
1269 kind of range this is. KIND will always be set to the appropriate
1270 value describing the kind of range, and this can be used to
1271 determine whether LOW or HIGH are valid. */
1274 rust_compute_range (struct type *type, struct value *range,
1275 LONGEST *low, LONGEST *high,
1276 enum range_type *kind)
1282 *kind = BOTH_BOUND_DEFAULT;
1284 if (TYPE_NFIELDS (type) == 0)
1288 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1290 *kind = HIGH_BOUND_DEFAULT;
1291 *low = value_as_long (value_field (range, 0));
1294 if (TYPE_NFIELDS (type) > i
1295 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1297 *kind = (*kind == BOTH_BOUND_DEFAULT
1298 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1299 *high = value_as_long (value_field (range, i));
1303 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1305 static struct value *
1306 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1309 struct value *lhs, *rhs, *result;
1310 struct type *rhstype;
1311 LONGEST low, high_bound;
1312 /* Initialized to appease the compiler. */
1313 enum range_type kind = BOTH_BOUND_DEFAULT;
1318 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1319 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1321 if (noside == EVAL_SKIP)
1324 rhstype = check_typedef (value_type (rhs));
1325 if (rust_range_type_p (rhstype))
1328 error (_("Can't take slice of array without '&'"));
1329 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1333 low = value_as_long (rhs);
1335 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1337 struct type *type = check_typedef (value_type (lhs));
1339 result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
1345 struct type *type = check_typedef (value_type (lhs));
1347 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1350 if (!get_array_bounds (type, &low_bound, &high_bound))
1351 error (_("Can't compute array bounds"));
1353 error (_("Found array with non-zero lower bound"));
1356 else if (rust_slice_type_p (type))
1360 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1361 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1363 high_bound = value_as_long (len);
1366 error (_("Cannot subscript non-array type"));
1369 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1372 error (_("Index less than zero"));
1373 if (low > high_bound)
1374 error (_("Index greater than length"));
1376 result = value_subscript (base, low);
1383 struct type *usize, *slice;
1385 struct value *addrval, *tem;
1387 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1390 error (_("High index less than zero"));
1392 error (_("Low index greater than high index"));
1393 if (high > high_bound)
1394 error (_("High index greater than length"));
1396 usize = language_lookup_primitive_type (exp->language_defn,
1399 slice = rust_slice_type ("&[*gdb*]", value_type (result),
1402 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1403 addr = value_as_long (addrval);
1404 tem = value_at_lazy (slice, addr);
1406 value_assign (value_field (tem, 0), value_addr (result));
1407 value_assign (value_field (tem, 1),
1408 value_from_longest (usize, high - low));
1410 result = value_at_lazy (slice, addr);
1413 result = value_addr (result);
1419 /* evaluate_exp implementation for Rust. */
1421 static struct value *
1422 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1423 int *pos, enum noside noside)
1425 struct value *result;
1427 switch (exp->elts[*pos].opcode)
1429 case UNOP_COMPLEMENT:
1431 struct value *value;
1434 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1435 if (noside == EVAL_SKIP)
1437 /* Preserving the type is enough. */
1440 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1441 result = value_from_longest (value_type (value),
1442 value_logical_not (value));
1444 result = value_complement (value);
1448 case BINOP_SUBSCRIPT:
1449 result = rust_subscript (exp, pos, noside, 0);
1453 result = rust_evaluate_funcall (exp, pos, noside);
1459 struct type *type = exp->elts[pc + 1].type;
1460 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1463 struct value *addrval = NULL;
1467 if (noside == EVAL_NORMAL)
1469 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1470 addr = value_as_long (addrval);
1471 result = value_at_lazy (type, addr);
1474 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1479 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1480 if (noside == EVAL_NORMAL)
1482 /* This isn't quite right but will do for the time
1483 being, seeing that we can't implement the Copy
1485 value_assign (result, init);
1491 gdb_assert (arglen % 2 == 0);
1492 for (i = 0; i < arglen; i += 2)
1495 const char *fieldname;
1496 struct value *value, *field;
1498 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1500 len = longest_to_int (exp->elts[*pos].longconst);
1502 fieldname = &exp->elts[*pos].string;
1503 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1505 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1506 if (noside == EVAL_NORMAL)
1508 field = value_struct_elt (&result, NULL, fieldname, NULL,
1510 value_assign (field, value);
1514 if (noside == EVAL_SKIP)
1515 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1517 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1518 result = allocate_value (type);
1520 result = value_at_lazy (type, addr);
1529 struct value *ncopies;
1531 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1532 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1533 copies = value_as_long (ncopies);
1535 error (_("Array with negative number of elements"));
1537 if (noside == EVAL_NORMAL)
1541 struct value **eltvec = XNEWVEC (struct value *, copies);
1542 struct cleanup *cleanup = make_cleanup (xfree, eltvec);
1544 for (i = 0; i < copies; ++i)
1546 result = value_array (0, copies - 1, eltvec);
1548 do_cleanups (cleanup);
1552 struct type *arraytype
1553 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1554 result = allocate_value (arraytype);
1559 case STRUCTOP_ANONYMOUS:
1561 /* Anonymous field access, i.e. foo.1. */
1563 int pc, field_number, nfields;
1564 struct type *type, *variant_type;
1565 struct disr_info disr;
1568 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1570 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1572 type = value_type (lhs);
1573 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1575 struct cleanup *cleanup;
1577 disr = rust_get_disr_info (type, value_contents (lhs),
1578 value_embedded_offset (lhs),
1579 value_address (lhs), lhs);
1581 cleanup = make_cleanup (xfree, disr.name);
1583 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1585 variant_type = NULL;
1590 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1591 nfields = TYPE_NFIELDS (variant_type);
1594 if (!disr.is_encoded)
1597 if (field_number >= nfields || field_number < 0)
1598 error(_("Cannot access field %d of variant %s, \
1599 there are only %d fields"),
1600 disr.is_encoded ? field_number : field_number - 1,
1602 disr.is_encoded ? nfields : nfields - 1);
1604 if (!(disr.is_encoded
1605 ? rust_tuple_struct_type_p (variant_type)
1606 : rust_tuple_variant_type_p (variant_type)))
1607 error(_("Variant %s is not a tuple variant"), disr.name);
1609 result = value_primitive_field (lhs, 0, field_number,
1611 do_cleanups (cleanup);
1613 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1615 /* Tuples and tuple structs */
1616 nfields = TYPE_NFIELDS(type);
1618 if (field_number >= nfields || field_number < 0)
1619 error(_("Cannot access field %d of %s, there are only %d fields"),
1620 field_number, TYPE_TAG_NAME (type), nfields);
1622 /* Tuples are tuple structs too. */
1623 if (!rust_tuple_struct_type_p (type))
1624 error(_("Attempting to access anonymous field %d of %s, which is \
1625 not a tuple, tuple struct, or tuple-like variant"),
1626 field_number, TYPE_TAG_NAME (type));
1628 result = value_primitive_field (lhs, 0, field_number, type);
1631 error(_("Anonymous field access is only allowed on tuples, \
1632 tuple structs, and tuple-like enum variants"));
1636 case STRUCTOP_STRUCT:
1643 tem = longest_to_int (exp->elts[pc + 1].longconst);
1644 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1645 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1647 type = value_type (lhs);
1649 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1652 struct disr_info disr;
1653 struct cleanup* cleanup;
1654 struct type* variant_type;
1657 field_name = &exp->elts[pc + 2].string;
1659 disr = rust_get_disr_info (type, value_contents (lhs),
1660 value_embedded_offset (lhs),
1661 value_address (lhs), lhs);
1663 cleanup = make_cleanup (xfree, disr.name);
1665 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1666 error(_("Could not find field %s of struct variant %s"),
1667 field_name, disr.name);
1669 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1671 if (variant_type == NULL
1672 || rust_tuple_variant_type_p (variant_type))
1673 error(_("Attempting to access named field %s of tuple variant %s, \
1674 which has only anonymous fields"),
1675 field_name, disr.name);
1677 start = disr.is_encoded ? 0 : 1;
1678 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1680 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1682 result = value_primitive_field (lhs, 0, i, variant_type);
1687 if (i == TYPE_NFIELDS (variant_type))
1688 /* We didn't find it. */
1689 error(_("Could not find field %s of struct variant %s"),
1690 field_name, disr.name);
1692 do_cleanups (cleanup);
1697 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1703 result = rust_range (exp, pos, noside);
1707 /* We might have &array[range], in which case we need to make a
1709 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1712 result = rust_subscript (exp, pos, noside, 1);
1717 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1724 /* operator_length implementation for Rust. */
1727 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1733 switch (exp->elts[pc - 1].opcode)
1736 /* We handle aggregate as a type and argument count. The first
1737 argument might be OP_OTHERS. After that the arguments
1738 alternate: first an OP_NAME, then an expression. */
1740 args = longest_to_int (exp->elts[pc - 2].longconst);
1748 case STRUCTOP_ANONYMOUS:
1759 operator_length_standard (exp, pc, oplenp, argsp);
1767 /* op_name implementation for Rust. */
1770 rust_op_name (enum exp_opcode opcode)
1775 return "OP_AGGREGATE";
1779 return op_name_standard (opcode);
1783 /* dump_subexp_body implementation for Rust. */
1786 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1789 switch (exp->elts[elt].opcode)
1793 int length = longest_to_int (exp->elts[elt + 2].longconst);
1796 fprintf_filtered (stream, "Type @");
1797 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1798 fprintf_filtered (stream, " (");
1799 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1800 fprintf_filtered (stream, "), length %d", length);
1803 for (i = 0; i < length; ++i)
1804 elt = dump_subexp (exp, stream, elt);
1811 LONGEST len = exp->elts[elt + 1].longconst;
1813 fprintf_filtered (stream, "%s: %s",
1814 (exp->elts[elt].opcode == OP_STRING
1815 ? "string" : "name"),
1816 &exp->elts[elt + 2].string);
1817 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1822 elt = dump_subexp (exp, stream, elt + 1);
1825 case STRUCTOP_ANONYMOUS:
1829 field_number = longest_to_int (exp->elts[elt].longconst);
1831 fprintf_filtered (stream, "Field number: %d", field_number);
1832 elt = dump_subexp (exp, stream, elt + 2);
1840 elt = dump_subexp_body_standard (exp, stream, elt);
1847 /* print_subexp implementation for Rust. */
1850 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1851 enum precedence prec)
1853 switch (exp->elts[*pos].opcode)
1857 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1860 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1861 fputs_filtered (" { ", stream);
1864 for (i = 0; i < length; ++i)
1866 rust_print_subexp (exp, pos, stream, prec);
1867 fputs_filtered (", ", stream);
1869 fputs_filtered (" }", stream);
1875 LONGEST len = exp->elts[*pos + 1].longconst;
1877 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1878 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1884 fputs_filtered ("<<others>> (", stream);
1886 rust_print_subexp (exp, pos, stream, prec);
1887 fputs_filtered (")", stream);
1891 case STRUCTOP_ANONYMOUS:
1893 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1896 print_subexp (exp, pos, stream, PREC_SUFFIX);
1897 fprintf_filtered (stream, ".%d", tem);
1903 fprintf_filtered (stream, "[");
1904 rust_print_subexp (exp, pos, stream, prec);
1905 fprintf_filtered (stream, "; ");
1906 rust_print_subexp (exp, pos, stream, prec);
1907 fprintf_filtered (stream, "]");
1911 print_subexp_standard (exp, pos, stream, prec);
1916 /* operator_check implementation for Rust. */
1919 rust_operator_check (struct expression *exp, int pos,
1920 int (*objfile_func) (struct objfile *objfile,
1924 switch (exp->elts[pos].opcode)
1928 struct type *type = exp->elts[pos + 1].type;
1929 struct objfile *objfile = TYPE_OBJFILE (type);
1931 if (objfile != NULL && (*objfile_func) (objfile, data))
1942 return operator_check_standard (exp, pos, objfile_func, data);
1950 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1952 static struct block_symbol
1953 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
1955 const struct block *block,
1956 const domain_enum domain)
1958 struct block_symbol result = {NULL, NULL};
1960 if (symbol_lookup_debug)
1962 fprintf_unfiltered (gdb_stdlog,
1963 "rust_lookup_symbol_non_local"
1964 " (%s, %s (scope %s), %s)\n",
1965 name, host_address_to_string (block),
1966 block_scope (block), domain_name (domain));
1969 /* Look up bare names in the block's scope. */
1970 if (name[cp_find_first_component (name)] == '\0')
1972 const char *scope = block_scope (block);
1974 if (scope[0] != '\0')
1976 char *scopedname = concat (scope, "::", name, (char *) NULL);
1977 struct cleanup *cleanup = make_cleanup (xfree, scopedname);
1979 result = lookup_symbol_in_static_block (scopedname, block,
1981 if (result.symbol == NULL)
1982 result = lookup_global_symbol (scopedname, block, domain);
1983 do_cleanups (cleanup);
1991 static const struct exp_descriptor exp_descriptor_rust =
1994 rust_operator_length,
1995 rust_operator_check,
1997 rust_dump_subexp_body,
1998 rust_evaluate_subexp
2001 static const struct language_defn rust_language_defn =
2010 &exp_descriptor_rust,
2014 rust_printchar, /* Print a character constant */
2015 rust_printstr, /* Function to print string constant */
2016 rust_emitchar, /* Print a single char */
2017 rust_print_type, /* Print a type using appropriate syntax */
2018 rust_print_typedef, /* Print a typedef using appropriate syntax */
2019 rust_val_print, /* Print a value using appropriate syntax */
2020 c_value_print, /* Print a top-level value */
2021 default_read_var_value, /* la_read_var_value */
2022 NULL, /* Language specific skip_trampoline */
2023 NULL, /* name_of_this */
2024 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2025 basic_lookup_transparent_type,/* lookup_transparent_type */
2026 gdb_demangle, /* Language specific symbol demangler */
2027 NULL, /* Language specific
2028 class_name_from_physname */
2029 c_op_print_tab, /* expression operators for printing */
2030 1, /* c-style arrays */
2031 0, /* String lower bound */
2032 default_word_break_characters,
2033 default_make_symbol_completion_list,
2034 rust_language_arch_info,
2035 default_print_array_index,
2036 default_pass_by_reference,
2038 NULL, /* la_get_symbol_name_cmp */
2039 iterate_over_symbols,
2040 &default_varobj_ops,
2047 _initialize_rust_language (void)
2049 add_language (&rust_language_defn);