Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / gdb / rust-lang.c
blobd4cd8802fa58f3b8e1b2cc648916b60edc710293
1 /* Rust language support routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2024 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/>. */
21 #include <ctype.h>
23 #include "block.h"
24 #include "c-lang.h"
25 #include "charset.h"
26 #include "cp-support.h"
27 #include "demangle.h"
28 #include "event-top.h"
29 #include "gdbarch.h"
30 #include "infcall.h"
31 #include "objfiles.h"
32 #include "rust-lang.h"
33 #include "typeprint.h"
34 #include "valprint.h"
35 #include "varobj.h"
36 #include <algorithm>
37 #include <string>
38 #include <vector>
39 #include "cli/cli-style.h"
40 #include "parser-defs.h"
41 #include "rust-exp.h"
43 /* See rust-lang.h. */
45 const char *
46 rust_last_path_segment (const char *path)
48 const char *result = strrchr (path, ':');
50 if (result == NULL)
51 return path;
52 return result + 1;
55 /* See rust-lang.h. */
57 std::string
58 rust_crate_for_block (const struct block *block)
60 const char *scope = block->scope ();
62 if (scope[0] == '\0')
63 return std::string ();
65 return std::string (scope, cp_find_first_component (scope));
68 /* Return true if TYPE, which must be a struct type, represents a Rust
69 enum. */
71 static bool
72 rust_enum_p (struct type *type)
74 /* is_dynamic_type will return true if any field has a dynamic
75 attribute -- but we only want to check the top level. */
76 return TYPE_HAS_VARIANT_PARTS (type);
79 /* Return true if TYPE, which must be an already-resolved enum type,
80 has no variants. */
82 static bool
83 rust_empty_enum_p (const struct type *type)
85 return type->num_fields () == 0;
88 /* Given an already-resolved enum type and contents, find which
89 variant is active. */
91 static int
92 rust_enum_variant (struct type *type)
94 /* The active variant is simply the first non-artificial field. */
95 for (int i = 0; i < type->num_fields (); ++i)
96 if (!type->field (i).is_artificial ())
97 return i;
99 /* Perhaps we could get here by trying to print an Ada variant
100 record in Rust mode. Unlikely, but an error is safer than an
101 assert. */
102 error (_("Could not find active enum variant"));
105 /* See rust-lang.h. */
107 bool
108 rust_tuple_type_p (struct type *type)
110 /* The current implementation is a bit of a hack, but there's
111 nothing else in the debuginfo to distinguish a tuple from a
112 struct. */
113 return (type->code () == TYPE_CODE_STRUCT
114 && type->name () != NULL
115 && type->name ()[0] == '(');
118 /* Return true if all non-static fields of a structlike type are in a
119 sequence like __0, __1, __2. */
121 static bool
122 rust_underscore_fields (struct type *type)
124 int field_number = 0;
126 if (type->code () != TYPE_CODE_STRUCT)
127 return false;
128 for (int i = 0; i < type->num_fields (); ++i)
130 if (!type->field (i).is_static ())
132 char buf[20];
134 xsnprintf (buf, sizeof (buf), "__%d", field_number);
135 if (strcmp (buf, type->field (i).name ()) != 0)
136 return false;
137 field_number++;
140 return true;
143 /* See rust-lang.h. */
145 bool
146 rust_tuple_struct_type_p (struct type *type)
148 /* This is just an approximation until DWARF can represent Rust more
149 precisely. We exclude zero-length structs because they may not
150 be tuple structs, and there's no way to tell. */
151 return type->num_fields () > 0 && rust_underscore_fields (type);
154 /* Return true if TYPE is "slice-like"; false otherwise. */
156 static bool
157 rust_slice_type_p (const struct type *type)
159 if (type->code () == TYPE_CODE_STRUCT
160 && type->name () != NULL
161 && type->num_fields () == 2)
163 /* The order of fields doesn't matter. While it would be nice
164 to check for artificiality here, the Rust compiler doesn't
165 emit this information. */
166 const char *n1 = type->field (0).name ();
167 const char *n2 = type->field (1).name ();
168 return ((streq (n1, "data_ptr") && streq (n2, "length"))
169 || (streq (n2, "data_ptr") && streq (n1, "length")));
171 return false;
174 /* Return true if TYPE is a range type, otherwise false. */
176 static bool
177 rust_range_type_p (struct type *type)
179 if (type->code () != TYPE_CODE_STRUCT
180 || type->num_fields () > 2
181 || type->name () == NULL
182 || strstr (type->name (), "::Range") == NULL)
183 return false;
185 if (type->num_fields () == 0)
186 return true;
188 int field_num = 0;
189 if (strcmp (type->field (0).name (), "start") == 0)
191 if (type->num_fields () == 1)
192 return true;
193 field_num = 1;
195 else if (type->num_fields () == 2)
197 /* First field had to be "start". */
198 return false;
201 return strcmp (type->field (field_num).name (), "end") == 0;
204 /* Return true if TYPE is an inclusive range type, otherwise false.
205 This is only valid for types which are already known to be range
206 types. */
208 static bool
209 rust_inclusive_range_type_p (struct type *type)
211 return (strstr (type->name (), "::RangeInclusive") != NULL
212 || strstr (type->name (), "::RangeToInclusive") != NULL);
215 /* Return true if TYPE seems to be the type "u8", otherwise false. */
217 static bool
218 rust_u8_type_p (struct type *type)
220 return (type->code () == TYPE_CODE_INT
221 && type->is_unsigned ()
222 && type->length () == 1);
225 /* Return true if TYPE is a Rust character type. */
227 static bool
228 rust_chartype_p (struct type *type)
230 return (type->code () == TYPE_CODE_CHAR
231 && type->length () == 4
232 && type->is_unsigned ());
235 /* If VALUE represents a trait object pointer, return the underlying
236 pointer with the correct (i.e., runtime) type. Otherwise, return
237 NULL. */
239 static struct value *
240 rust_get_trait_object_pointer (struct value *value)
242 struct type *type = check_typedef (value->type ());
244 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
245 return NULL;
247 /* Try to be a bit resilient if the ABI changes. */
248 int vtable_field = 0;
249 for (int i = 0; i < 2; ++i)
251 if (strcmp (type->field (i).name (), "vtable") == 0)
252 vtable_field = i;
253 else if (strcmp (type->field (i).name (), "pointer") != 0)
254 return NULL;
257 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
258 struct symbol *symbol = find_symbol_at_address (vtable);
259 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
260 return NULL;
262 struct rust_vtable_symbol *vtable_sym
263 = static_cast<struct rust_vtable_symbol *> (symbol);
264 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
265 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
268 /* Find and possibly rewrite the unsized part of a slice-like type.
270 This function has two modes. If the out parameters are both NULL,
271 it will return true if an unsized member of IN_TYPE is found.
273 If the out parameters are both non-NULL, it will do the same, but
274 will also rewrite the unsized member's type to be an array of the
275 appropriate type. BOUND is the upper bound of the new array.
277 See convert_slice to understand the different kinds of unsized type
278 and how they are represented.
280 static bool
281 rewrite_slice_type (struct type *in_type, struct type **new_type,
282 LONGEST bound, ULONGEST *additional_length)
284 if (in_type->code () != TYPE_CODE_STRUCT)
285 return false;
287 unsigned nfields = in_type->num_fields ();
288 if (nfields == 0)
289 return false;
291 struct type *rewritten;
292 const field &field = in_type->field (nfields - 1);
293 struct type *field_type = field.type ();
294 if (field.loc_kind () == FIELD_LOC_KIND_BITPOS
295 && field.loc_bitpos () == 8 * in_type->length ())
297 if (additional_length == nullptr)
298 return true;
299 rewritten = lookup_array_range_type (field_type, 0, bound);
300 *additional_length = rewritten->length ();
302 else
304 if (!rewrite_slice_type (field_type, &rewritten, bound,
305 additional_length))
306 return false;
307 if (additional_length == nullptr)
308 return true;
311 struct type *result = copy_type (in_type);
312 result->copy_fields (in_type);
313 result->field (nfields - 1).set_type (rewritten);
314 result->set_length (result->length () + *additional_length);
316 *new_type = result;
317 return true;
320 /* Convert a Rust slice to its "true" representation.
322 The Rust compiler emits slices as "fat" pointers like:
324 struct { payload *data_ptr; usize length }
326 Any sort of unsized type is emitted this way.
328 If 'payload' is a struct type, then it must be searched to see if
329 the trailing field is unsized. This has to be done recursively (as
330 in, if the final field in the struct type itself has struct type,
331 then that type must be searched). In this scenario, the unsized
332 field can be recognized because it does not contribute to the
333 type's size.
335 If 'payload' does not have a trailing unsized type, or if it is not
336 of struct type, then this slice is "array-like". In this case
337 rewriting will return an array.
339 static struct value *
340 convert_slice (struct value *val)
342 struct type *type = check_typedef (val->type ());
343 /* This must have been checked by the caller. */
344 gdb_assert (rust_slice_type_p (type));
346 struct value *len = value_struct_elt (&val, {}, "length", nullptr,
347 "slice");
348 LONGEST llen = value_as_long (len);
350 struct value *ptr = value_struct_elt (&val, {}, "data_ptr", nullptr,
351 "slice");
352 struct type *original_type = ptr->type ()->target_type ();
353 ULONGEST new_length_storage = 0;
354 struct type *new_type = nullptr;
355 if (!rewrite_slice_type (original_type, &new_type, llen - 1,
356 &new_length_storage))
357 new_type = lookup_array_range_type (original_type, 0, llen - 1);
359 struct value *result = value::allocate_lazy (new_type);
360 result->set_lval (lval_memory);
361 result->set_address (value_as_address (ptr));
362 result->fetch_lazy ();
364 return result;
367 /* If TYPE is an array-like slice, return the element type; otherwise
368 return NULL. */
369 static struct type *
370 rust_array_like_element_type (struct type *type)
372 /* Caller must check this. */
373 gdb_assert (rust_slice_type_p (type));
374 for (int i = 0; i < type->num_fields (); ++i)
376 if (strcmp (type->field (i).name (), "data_ptr") == 0)
378 struct type *base_type = type->field (i).type ()->target_type ();
379 if (rewrite_slice_type (base_type, nullptr, 0, nullptr))
380 return nullptr;
381 return base_type;
384 return nullptr;
389 /* See language.h. */
391 void
392 rust_language::printstr (struct ui_file *stream, struct type *type,
393 const gdb_byte *string, unsigned int length,
394 const char *user_encoding, int force_ellipses,
395 const struct value_print_options *options) const
397 /* Rust always uses UTF-8, but let the caller override this if need
398 be. */
399 const char *encoding = user_encoding;
400 if (user_encoding == NULL || !*user_encoding)
402 /* In Rust strings, characters are "u8". */
403 if (rust_u8_type_p (type))
404 encoding = "UTF-8";
405 else
407 /* This is probably some C string, so let's let C deal with
408 it. */
409 language_defn::printstr (stream, type, string, length,
410 user_encoding, force_ellipses,
411 options);
412 return;
416 /* This is not ideal as it doesn't use our character printer. */
417 generic_printstr (stream, type, string, length, encoding, force_ellipses,
418 '"', 0, options);
423 static const struct generic_val_print_decorations rust_decorations =
425 /* Complex isn't used in Rust, but we provide C-ish values just in
426 case. */
428 " + ",
429 " * I",
430 "true",
431 "false",
432 "()",
433 "[",
437 /* See rust-lang.h. */
439 struct value *
440 rust_slice_to_array (struct value *val)
442 val = convert_slice (val);
443 if (val->type ()->code () != TYPE_CODE_ARRAY)
444 return nullptr;
445 return val;
448 /* Helper function to print a slice. */
450 void
451 rust_language::val_print_slice
452 (struct value *val, struct ui_file *stream, int recurse,
453 const struct value_print_options *options) const
455 struct type *orig_type = check_typedef (val->type ());
457 val = convert_slice (val);
458 struct type *type = check_typedef (val->type ());
460 /* &str is handled here; but for all other slice types it is fine to
461 simply print the contents. */
462 if (orig_type->name () != nullptr
463 && strcmp (orig_type->name (), "&str") == 0)
465 LONGEST low_bound, high_bound;
466 if (get_array_bounds (type, &low_bound, &high_bound))
468 val_print_string (type->target_type (), "UTF-8",
469 val->address (), high_bound - low_bound + 1,
470 stream, options);
471 return;
475 /* Print the slice type here. This was gdb's historical behavior
476 (from before unsized types were generically handled) and helps
477 make it clear that the user is seeing a slice, not an array.
478 Only arrays must be handled as the other cases are handled by
479 value_print_inner. */
480 if (type->code () == TYPE_CODE_ARRAY)
482 type_print (orig_type, "", stream, -1);
483 gdb_printf (stream, " ");
486 value_print_inner (val, stream, recurse, options);
489 /* See rust-lang.h. */
491 void
492 rust_language::val_print_struct
493 (struct value *val, struct ui_file *stream, int recurse,
494 const struct value_print_options *options) const
496 int first_field;
497 struct type *type = check_typedef (val->type ());
499 if (rust_slice_type_p (type))
501 val_print_slice (val, stream, recurse, options);
502 return;
505 bool is_tuple = rust_tuple_type_p (type);
506 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
507 struct value_print_options opts;
509 if (!is_tuple)
511 if (type->name () != NULL)
512 gdb_printf (stream, "%s", type->name ());
514 if (type->num_fields () == 0)
515 return;
517 if (type->name () != NULL)
518 gdb_puts (" ", stream);
521 if (is_tuple || is_tuple_struct)
522 gdb_puts ("(", stream);
523 else
524 gdb_puts ("{", stream);
526 opts = *options;
527 opts.deref_ref = false;
529 first_field = 1;
530 for (int i = 0; i < type->num_fields (); ++i)
532 if (type->field (i).is_static ())
533 continue;
535 if (!first_field)
536 gdb_puts (",", stream);
538 if (options->prettyformat)
540 gdb_puts ("\n", stream);
541 print_spaces (2 + 2 * recurse, stream);
543 else if (!first_field)
544 gdb_puts (" ", stream);
546 first_field = 0;
548 if (!is_tuple && !is_tuple_struct)
550 fputs_styled (type->field (i).name (),
551 variable_name_style.style (), stream);
552 gdb_puts (": ", stream);
555 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
556 this);
559 if (options->prettyformat)
561 gdb_puts ("\n", stream);
562 print_spaces (2 * recurse, stream);
565 if (is_tuple || is_tuple_struct)
566 gdb_puts (")", stream);
567 else
568 gdb_puts ("}", stream);
571 /* See rust-lang.h. */
573 void
574 rust_language::print_enum (struct value *val, struct ui_file *stream,
575 int recurse,
576 const struct value_print_options *options) const
578 struct value_print_options opts = *options;
579 struct type *type = check_typedef (val->type ());
581 opts.deref_ref = false;
583 gdb_assert (rust_enum_p (type));
584 gdb::array_view<const gdb_byte> view
585 (val->contents_for_printing ().data (),
586 val->type ()->length ());
587 type = resolve_dynamic_type (type, view, val->address ());
589 if (rust_empty_enum_p (type))
591 /* Print the enum type name here to be more clear. */
592 gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
593 type->name (),
594 metadata_style.style ().ptr (), nullptr);
595 return;
598 int variant_fieldno = rust_enum_variant (type);
599 val = val->primitive_field (0, variant_fieldno, type);
600 struct type *variant_type = type->field (variant_fieldno).type ();
602 int nfields = variant_type->num_fields ();
604 bool is_tuple = rust_tuple_struct_type_p (variant_type);
606 gdb_printf (stream, "%s", variant_type->name ());
607 if (nfields == 0)
609 /* In case of a nullary variant like 'None', just output
610 the name. */
611 return;
614 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
615 if (is_tuple)
616 gdb_printf (stream, "(");
617 else
619 /* struct variant. */
620 gdb_printf (stream, "{");
623 bool first_field = true;
624 for (int j = 0; j < nfields; j++)
626 if (!first_field)
627 gdb_puts (", ", stream);
628 first_field = false;
630 if (!is_tuple)
631 gdb_printf (stream, "%ps: ",
632 styled_string (variable_name_style.style (),
633 variant_type->field (j).name ()));
635 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
636 this);
639 if (is_tuple)
640 gdb_puts (")", stream);
641 else
642 gdb_puts ("}", stream);
645 /* See language.h. */
647 void
648 rust_language::value_print_inner
649 (struct value *val, struct ui_file *stream, int recurse,
650 const struct value_print_options *options) const
652 struct value_print_options opts = *options;
653 opts.deref_ref = true;
655 if (opts.prettyformat == Val_prettyformat_default)
656 opts.prettyformat = (opts.prettyformat_structs
657 ? Val_prettyformat : Val_no_prettyformat);
659 struct type *type = check_typedef (val->type ());
660 switch (type->code ())
662 case TYPE_CODE_PTR:
664 LONGEST low_bound, high_bound;
666 if (type->target_type ()->code () == TYPE_CODE_ARRAY
667 && rust_u8_type_p (type->target_type ()->target_type ())
668 && get_array_bounds (type->target_type (), &low_bound,
669 &high_bound))
671 /* We have a pointer to a byte string, so just print
672 that. */
673 struct type *elttype = check_typedef (type->target_type ());
674 CORE_ADDR addr = value_as_address (val);
675 struct gdbarch *arch = type->arch ();
677 if (opts.addressprint)
679 gdb_puts (paddress (arch, addr), stream);
680 gdb_puts (" ", stream);
683 gdb_puts ("b", stream);
684 val_print_string (elttype->target_type (), "ASCII", addr,
685 high_bound - low_bound + 1, stream,
686 &opts);
687 break;
690 goto generic_print;
692 case TYPE_CODE_INT:
693 /* Recognize the unit type. */
694 if (type->is_unsigned () && type->length () == 0
695 && type->name () != NULL && strcmp (type->name (), "()") == 0)
697 gdb_puts ("()", stream);
698 break;
700 goto generic_print;
702 case TYPE_CODE_STRING:
704 LONGEST low_bound, high_bound;
706 if (!get_array_bounds (type, &low_bound, &high_bound))
707 error (_("Could not determine the array bounds"));
709 /* If we see a plain TYPE_CODE_STRING, then we're printing a
710 byte string, hence the choice of "ASCII" as the
711 encoding. */
712 gdb_puts ("b", stream);
713 printstr (stream, type->target_type (),
714 val->contents_for_printing ().data (),
715 high_bound - low_bound + 1, "ASCII", 0, &opts);
717 break;
719 case TYPE_CODE_ARRAY:
721 LONGEST low_bound, high_bound;
723 if (get_array_bounds (type, &low_bound, &high_bound)
724 && high_bound - low_bound + 1 == 0)
725 gdb_puts ("[]", stream);
726 else
727 goto generic_print;
729 break;
731 case TYPE_CODE_UNION:
732 /* Untagged unions are printed as if they are structs. Since
733 the field bit positions overlap in the debuginfo, the code
734 for printing a union is same as that for a struct, the only
735 difference is that the input type will have overlapping
736 fields. */
737 val_print_struct (val, stream, recurse, &opts);
738 break;
740 case TYPE_CODE_STRUCT:
741 if (rust_enum_p (type))
742 print_enum (val, stream, recurse, &opts);
743 else
744 val_print_struct (val, stream, recurse, &opts);
745 break;
747 default:
748 generic_print:
749 /* Nothing special yet. */
750 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
754 /* See language.h. */
756 void
757 rust_language::value_print
758 (struct value *val, struct ui_file *stream,
759 const struct value_print_options *options) const
761 value_print_options opts = *options;
762 opts.deref_ref = true;
764 struct type *type = check_typedef (val->type ());
765 if (type->is_pointer_or_reference ())
767 gdb_printf (stream, "(");
768 type_print (val->type (), "", stream, -1);
769 gdb_printf (stream, ") ");
772 return common_val_print (val, stream, 0, &opts, this);
777 static void
778 rust_internal_print_type (struct type *type, const char *varstring,
779 struct ui_file *stream, int show, int level,
780 const struct type_print_options *flags,
781 bool for_rust_enum, print_offset_data *podata);
783 /* Print a struct or union typedef. */
784 static void
785 rust_print_struct_def (struct type *type, const char *varstring,
786 struct ui_file *stream, int show, int level,
787 const struct type_print_options *flags,
788 bool for_rust_enum, print_offset_data *podata)
790 /* Print a tuple type simply. */
791 if (rust_tuple_type_p (type))
793 gdb_puts (type->name (), stream);
794 return;
797 /* If we see a base class, delegate to C. */
798 if (TYPE_N_BASECLASSES (type) > 0)
799 c_print_type (type, varstring, stream, show, level, language_rust, flags);
801 if (flags->print_offsets)
803 /* Temporarily bump the level so that the output lines up
804 correctly. */
805 level += 2;
808 /* Compute properties of TYPE here because, in the enum case, the
809 rest of the code ends up looking only at the variant part. */
810 const char *tagname = type->name ();
811 bool is_tuple_struct = rust_tuple_struct_type_p (type);
812 bool is_tuple = rust_tuple_type_p (type);
813 bool is_enum = rust_enum_p (type);
815 if (for_rust_enum)
817 /* Already printing an outer enum, so nothing to print here. */
819 else
821 /* This code path is also used by unions and enums. */
822 if (is_enum)
824 gdb_puts ("enum ", stream);
825 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
826 if (prop != nullptr && prop->kind () == PROP_TYPE)
827 type = prop->original_type ();
829 else if (type->code () == TYPE_CODE_STRUCT)
830 gdb_puts ("struct ", stream);
831 else
832 gdb_puts ("union ", stream);
834 if (tagname != NULL)
835 gdb_puts (tagname, stream);
838 if (type->num_fields () == 0 && !is_tuple)
839 return;
840 if (for_rust_enum && !flags->print_offsets)
841 gdb_puts (is_tuple_struct ? "(" : "{", stream);
842 else
843 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
845 /* When printing offsets, we rearrange the fields into storage
846 order. This lets us show holes more clearly. We work using
847 field indices here because it simplifies calls to
848 print_offset_data::update below. */
849 std::vector<int> fields;
850 for (int i = 0; i < type->num_fields (); ++i)
852 if (type->field (i).is_static ())
853 continue;
854 if (is_enum && type->field (i).is_artificial ())
855 continue;
856 fields.push_back (i);
858 if (flags->print_offsets)
859 std::sort (fields.begin (), fields.end (),
860 [&] (int a, int b)
862 return (type->field (a).loc_bitpos ()
863 < type->field (b).loc_bitpos ());
866 for (int i : fields)
868 QUIT;
870 gdb_assert (!type->field (i).is_static ());
871 gdb_assert (! (is_enum && type->field (i).is_artificial ()));
873 if (flags->print_offsets)
874 podata->update (type, i, stream);
876 /* We'd like to print "pub" here as needed, but rustc
877 doesn't emit the debuginfo, and our types don't have
878 cplus_struct_type attached. */
880 /* For a tuple struct we print the type but nothing
881 else. */
882 if (!for_rust_enum || flags->print_offsets)
883 print_spaces (level + 2, stream);
884 if (is_enum)
885 fputs_styled (type->field (i).name (), variable_name_style.style (),
886 stream);
887 else if (!is_tuple_struct)
888 gdb_printf (stream, "%ps: ",
889 styled_string (variable_name_style.style (),
890 type->field (i).name ()));
892 rust_internal_print_type (type->field (i).type (), NULL,
893 stream, (is_enum ? show : show - 1),
894 level + 2, flags, is_enum, podata);
895 if (!for_rust_enum || flags->print_offsets)
896 gdb_puts (",\n", stream);
897 /* Note that this check of "I" is ok because we only sorted the
898 fields by offset when print_offsets was set, so we won't take
899 this branch in that case. */
900 else if (i + 1 < type->num_fields ())
901 gdb_puts (", ", stream);
904 if (flags->print_offsets)
906 /* Undo the temporary level increase we did above. */
907 level -= 2;
908 podata->finish (type, level, stream);
909 print_spaces (print_offset_data::indentation, stream);
910 if (level == 0)
911 print_spaces (2, stream);
913 if (!for_rust_enum || flags->print_offsets)
914 print_spaces (level, stream);
915 gdb_puts (is_tuple_struct ? ")" : "}", stream);
918 /* la_print_type implementation for Rust. */
920 static void
921 rust_internal_print_type (struct type *type, const char *varstring,
922 struct ui_file *stream, int show, int level,
923 const struct type_print_options *flags,
924 bool for_rust_enum, print_offset_data *podata)
926 QUIT;
927 if (show <= 0
928 && type->name () != NULL)
930 /* Rust calls the unit type "void" in its debuginfo,
931 but we don't want to print it as that. */
932 if (type->code () == TYPE_CODE_VOID)
933 gdb_puts ("()", stream);
934 else
935 gdb_puts (type->name (), stream);
936 return;
939 type = check_typedef (type);
940 switch (type->code ())
942 case TYPE_CODE_VOID:
943 /* If we have an enum, we've already printed the type's
944 unqualified name, and there is nothing else to print
945 here. */
946 if (!for_rust_enum)
947 gdb_puts ("()", stream);
948 break;
950 case TYPE_CODE_FUNC:
951 /* Delegate varargs to the C printer. */
952 if (type->has_varargs ())
953 goto c_printer;
955 gdb_puts ("fn ", stream);
956 if (varstring != NULL)
957 gdb_puts (varstring, stream);
958 gdb_puts ("(", stream);
959 for (int i = 0; i < type->num_fields (); ++i)
961 QUIT;
962 if (i > 0)
963 gdb_puts (", ", stream);
964 rust_internal_print_type (type->field (i).type (), "", stream,
965 -1, 0, flags, false, podata);
967 gdb_puts (")", stream);
968 /* If it returns unit, we can omit the return type. */
969 if (type->target_type ()->code () != TYPE_CODE_VOID)
971 gdb_puts (" -> ", stream);
972 rust_internal_print_type (type->target_type (), "", stream,
973 -1, 0, flags, false, podata);
975 break;
977 case TYPE_CODE_ARRAY:
979 LONGEST low_bound, high_bound;
981 gdb_puts ("[", stream);
982 rust_internal_print_type (type->target_type (), NULL,
983 stream, show - 1, level, flags, false,
984 podata);
986 if (type->bounds ()->high.kind () == PROP_LOCEXPR
987 || type->bounds ()->high.kind () == PROP_LOCLIST)
988 gdb_printf (stream, "; variable length");
989 else if (get_array_bounds (type, &low_bound, &high_bound))
990 gdb_printf (stream, "; %s",
991 plongest (high_bound - low_bound + 1));
992 gdb_puts ("]", stream);
994 break;
996 case TYPE_CODE_UNION:
997 case TYPE_CODE_STRUCT:
998 rust_print_struct_def (type, varstring, stream, show, level, flags,
999 for_rust_enum, podata);
1000 break;
1002 case TYPE_CODE_ENUM:
1004 int len = 0;
1006 gdb_puts ("enum ", stream);
1007 if (type->name () != NULL)
1009 gdb_puts (type->name (), stream);
1010 gdb_puts (" ", stream);
1011 len = strlen (type->name ());
1013 gdb_puts ("{\n", stream);
1015 for (int i = 0; i < type->num_fields (); ++i)
1017 const char *name = type->field (i).name ();
1019 QUIT;
1021 if (len > 0
1022 && strncmp (name, type->name (), len) == 0
1023 && name[len] == ':'
1024 && name[len + 1] == ':')
1025 name += len + 2;
1026 gdb_printf (stream, "%*s%ps,\n",
1027 level + 2, "",
1028 styled_string (variable_name_style.style (),
1029 name));
1032 gdb_puts ("}", stream);
1034 break;
1036 case TYPE_CODE_PTR:
1038 if (type->name () != nullptr)
1039 gdb_puts (type->name (), stream);
1040 else
1042 /* We currently can't distinguish between pointers and
1043 references. */
1044 gdb_puts ("*mut ", stream);
1045 type_print (type->target_type (), "", stream, 0);
1048 break;
1050 default:
1051 c_printer:
1052 c_print_type (type, varstring, stream, show, level, language_rust,
1053 flags);
1059 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1060 -- either on an obstack or on a gdbarch. */
1062 static struct type *
1063 rust_composite_type (struct type *original,
1064 const char *name,
1065 const char *field1, struct type *type1,
1066 const char *field2, struct type *type2)
1068 struct type *result = type_allocator (original).new_type ();
1069 int i, nfields, bitpos;
1071 nfields = 0;
1072 if (field1 != NULL)
1073 ++nfields;
1074 if (field2 != NULL)
1075 ++nfields;
1077 result->set_code (TYPE_CODE_STRUCT);
1078 result->set_name (name);
1080 result->alloc_fields (nfields);
1082 i = 0;
1083 bitpos = 0;
1084 if (field1 != NULL)
1086 struct field *field = &result->field (i);
1088 field->set_loc_bitpos (bitpos);
1089 bitpos += type1->length () * TARGET_CHAR_BIT;
1091 field->set_name (field1);
1092 field->set_type (type1);
1093 ++i;
1095 if (field2 != NULL)
1097 struct field *field = &result->field (i);
1098 unsigned align = type_align (type2);
1100 if (align != 0)
1102 int delta;
1104 align *= TARGET_CHAR_BIT;
1105 delta = bitpos % align;
1106 if (delta != 0)
1107 bitpos += align - delta;
1109 field->set_loc_bitpos (bitpos);
1111 field->set_name (field2);
1112 field->set_type (type2);
1113 ++i;
1116 if (i > 0)
1117 result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT
1118 + result->field (i - 1).type ()->length ());
1119 return result;
1122 /* See rust-lang.h. */
1124 struct type *
1125 rust_slice_type (const char *name, struct type *elt_type,
1126 struct type *usize_type)
1128 struct type *type;
1130 elt_type = lookup_pointer_type (elt_type);
1131 type = rust_composite_type (elt_type, name,
1132 "data_ptr", elt_type,
1133 "length", usize_type);
1135 return type;
1140 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1142 struct value *
1143 rust_range (struct type *expect_type, struct expression *exp,
1144 enum noside noside, enum range_flag kind,
1145 struct value *low, struct value *high)
1147 struct value *addrval, *result;
1148 CORE_ADDR addr;
1149 struct type *range_type;
1150 struct type *index_type;
1151 struct type *temp_type;
1152 const char *name;
1154 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
1156 if (low == NULL)
1158 if (high == NULL)
1160 index_type = NULL;
1161 name = "std::ops::RangeFull";
1163 else
1165 index_type = high->type ();
1166 name = (inclusive
1167 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1170 else
1172 if (high == NULL)
1174 index_type = low->type ();
1175 name = "std::ops::RangeFrom";
1177 else
1179 if (!types_equal (low->type (), high->type ()))
1180 error (_("Range expression with different types"));
1181 index_type = low->type ();
1182 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1186 /* If we don't have an index type, just allocate this on the
1187 arch. Here any type will do. */
1188 temp_type = (index_type == NULL
1189 ? language_bool_type (exp->language_defn, exp->gdbarch)
1190 : index_type);
1191 /* It would be nicer to cache the range type. */
1192 range_type = rust_composite_type (temp_type, name,
1193 low == NULL ? NULL : "start", index_type,
1194 high == NULL ? NULL : "end", index_type);
1196 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1197 return value::zero (range_type, lval_memory);
1199 addrval = value_allocate_space_in_inferior (range_type->length ());
1200 addr = value_as_long (addrval);
1201 result = value_at_lazy (range_type, addr);
1203 if (low != NULL)
1205 struct value *start = value_struct_elt (&result, {}, "start", NULL,
1206 "range");
1208 value_assign (start, low);
1211 if (high != NULL)
1213 struct value *end = value_struct_elt (&result, {}, "end", NULL,
1214 "range");
1216 value_assign (end, high);
1219 result = value_at_lazy (range_type, addr);
1220 return result;
1223 /* A helper function to compute the range and kind given a range
1224 value. TYPE is the type of the range value. RANGE is the range
1225 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1226 parameters might be filled in, or might not be, depending on the
1227 kind of range this is. KIND will always be set to the appropriate
1228 value describing the kind of range, and this can be used to
1229 determine whether LOW or HIGH are valid. */
1231 static void
1232 rust_compute_range (struct type *type, struct value *range,
1233 LONGEST *low, LONGEST *high,
1234 range_flags *kind)
1236 int i;
1238 *low = 0;
1239 *high = 0;
1240 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1242 if (type->num_fields () == 0)
1243 return;
1245 i = 0;
1246 if (strcmp (type->field (0).name (), "start") == 0)
1248 *kind = RANGE_HIGH_BOUND_DEFAULT;
1249 *low = value_as_long (value_field (range, 0));
1250 ++i;
1252 if (type->num_fields () > i
1253 && strcmp (type->field (i).name (), "end") == 0)
1255 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1256 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
1257 *high = value_as_long (value_field (range, i));
1259 if (rust_inclusive_range_type_p (type))
1260 ++*high;
1264 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1266 struct value *
1267 rust_subscript (struct type *expect_type, struct expression *exp,
1268 enum noside noside, bool for_addr,
1269 struct value *lhs, struct value *rhs)
1271 struct value *result;
1272 struct type *rhstype;
1273 LONGEST low, high_bound;
1274 /* Initialized to appease the compiler. */
1275 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1276 LONGEST high = 0;
1277 bool want_slice = false;
1279 rhstype = check_typedef (rhs->type ());
1280 if (rust_range_type_p (rhstype))
1282 if (!for_addr)
1283 error (_("Can't take slice of array without '&'"));
1284 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1285 want_slice = true;
1287 else
1288 low = value_as_long (rhs);
1290 struct type *type = check_typedef (lhs->type ());
1291 struct type *orig_type = type;
1292 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1294 struct type *base_type = nullptr;
1295 if (type->code () == TYPE_CODE_ARRAY)
1296 base_type = type->target_type ();
1297 else if (rust_slice_type_p (type))
1299 base_type = rust_array_like_element_type (type);
1300 if (base_type == nullptr)
1301 error (_("Cannot subscript non-array-like slice"));
1303 else if (type->code () == TYPE_CODE_PTR)
1304 base_type = type->target_type ();
1305 else
1306 error (_("Cannot subscript non-array type"));
1308 struct type *new_type;
1309 if (want_slice)
1311 if (rust_slice_type_p (type))
1312 new_type = type;
1313 else
1315 struct type *usize
1316 = language_lookup_primitive_type (exp->language_defn,
1317 exp->gdbarch,
1318 "usize");
1319 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1322 else
1323 new_type = base_type;
1325 return value::zero (new_type, lhs->lval ());
1327 else
1329 LONGEST low_bound;
1330 struct value *base;
1332 if (rust_slice_type_p (type))
1334 lhs = convert_slice (lhs);
1335 type = check_typedef (lhs->type ());
1338 if (type->code () == TYPE_CODE_ARRAY)
1340 base = lhs;
1341 if (!get_array_bounds (type, &low_bound, &high_bound))
1342 error (_("Can't compute array bounds"));
1343 if (low_bound != 0)
1344 error (_("Found array with non-zero lower bound"));
1345 ++high_bound;
1347 else if (type->code () == TYPE_CODE_PTR)
1349 base = lhs;
1350 low_bound = 0;
1351 high_bound = LONGEST_MAX;
1353 else
1354 error (_("Cannot subscript non-array type"));
1356 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
1357 low = low_bound;
1358 if (low < 0)
1359 error (_("Index less than zero"));
1360 if (low > high_bound)
1361 error (_("Index greater than length"));
1363 result = value_subscript (base, low);
1366 if (for_addr)
1368 if (want_slice)
1370 struct type *usize, *slice;
1371 CORE_ADDR addr;
1372 struct value *addrval, *tem;
1374 if (kind & RANGE_HIGH_BOUND_DEFAULT)
1375 high = high_bound;
1376 if (high < 0)
1377 error (_("High index less than zero"));
1378 if (low > high)
1379 error (_("Low index greater than high index"));
1380 if (high > high_bound)
1381 error (_("High index greater than length"));
1383 usize = language_lookup_primitive_type (exp->language_defn,
1384 exp->gdbarch,
1385 "usize");
1386 /* Preserve the name for slice-of-slice; this lets
1387 string-printing work a bit more nicely. */
1388 const char *new_name = ((orig_type != nullptr
1389 && rust_slice_type_p (orig_type))
1390 ? orig_type->name () : "&[*gdb*]");
1392 slice = rust_slice_type (new_name, result->type (), usize);
1394 addrval = value_allocate_space_in_inferior (slice->length ());
1395 addr = value_as_long (addrval);
1396 tem = value_at_lazy (slice, addr);
1398 value_assign (value_field (tem, 0), value_addr (result));
1399 value_assign (value_field (tem, 1),
1400 value_from_longest (usize, high - low));
1402 result = value_at_lazy (slice, addr);
1404 else
1405 result = value_addr (result);
1408 return result;
1411 namespace expr
1414 struct value *
1415 rust_unop_ind_operation::evaluate (struct type *expect_type,
1416 struct expression *exp,
1417 enum noside noside)
1419 if (noside != EVAL_NORMAL)
1420 return unop_ind_operation::evaluate (expect_type, exp, noside);
1422 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1423 noside);
1424 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1425 if (trait_ptr != NULL)
1426 value = trait_ptr;
1428 return value_ind (value);
1431 } /* namespace expr */
1433 /* A helper function for UNOP_COMPLEMENT. */
1435 struct value *
1436 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1437 enum noside noside,
1438 enum exp_opcode opcode,
1439 struct value *value)
1441 if (value->type ()->code () == TYPE_CODE_BOOL)
1442 return value_from_longest (value->type (), value_logical_not (value));
1443 return value_complement (value);
1446 /* A helper function for OP_ARRAY. */
1448 struct value *
1449 eval_op_rust_array (struct type *expect_type, struct expression *exp,
1450 enum noside noside,
1451 enum exp_opcode opcode,
1452 struct value *elt, struct value *ncopies)
1454 int copies = value_as_long (ncopies);
1455 if (copies < 0)
1456 error (_("Array with negative number of elements"));
1458 if (noside == EVAL_NORMAL)
1459 return value_array (0, std::vector<value *> (copies, elt));
1460 else
1462 struct type *arraytype
1463 = lookup_array_range_type (elt->type (), 0, copies - 1);
1464 return value::allocate (arraytype);
1468 namespace expr
1471 struct value *
1472 rust_struct_anon::evaluate (struct type *expect_type,
1473 struct expression *exp,
1474 enum noside noside)
1476 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1477 int field_number = std::get<0> (m_storage);
1479 struct type *type = lhs->type ();
1481 if (type->code () == TYPE_CODE_STRUCT)
1483 struct type *outer_type = NULL;
1485 if (rust_enum_p (type))
1487 type = resolve_dynamic_type (type, lhs->contents (),
1488 lhs->address ());
1490 if (rust_empty_enum_p (type))
1491 error (_("Cannot access field %d of empty enum %s"),
1492 field_number, type->name ());
1494 int fieldno = rust_enum_variant (type);
1495 lhs = lhs->primitive_field (0, fieldno, type);
1496 outer_type = type;
1497 type = lhs->type ();
1500 /* Tuples and tuple structs */
1501 int nfields = type->num_fields ();
1503 if (field_number >= nfields || field_number < 0)
1505 if (outer_type != NULL)
1506 error(_("Cannot access field %d of variant %s::%s, "
1507 "there are only %d fields"),
1508 field_number, outer_type->name (),
1509 rust_last_path_segment (type->name ()),
1510 nfields);
1511 else
1512 error(_("Cannot access field %d of %s, "
1513 "there are only %d fields"),
1514 field_number, type->name (), nfields);
1517 /* Tuples are tuple structs too. */
1518 if (!rust_tuple_struct_type_p (type))
1520 if (outer_type != NULL)
1521 error(_("Variant %s::%s is not a tuple variant"),
1522 outer_type->name (),
1523 rust_last_path_segment (type->name ()));
1524 else
1525 error(_("Attempting to access anonymous field %d "
1526 "of %s, which is not a tuple, tuple struct, or "
1527 "tuple-like variant"),
1528 field_number, type->name ());
1531 return lhs->primitive_field (0, field_number, type);
1533 else
1534 error(_("Anonymous field access is only allowed on tuples, \
1535 tuple structs, and tuple-like enum variants"));
1538 struct value *
1539 rust_structop::evaluate (struct type *expect_type,
1540 struct expression *exp,
1541 enum noside noside)
1543 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1544 const char *field_name = std::get<1> (m_storage).c_str ();
1546 struct value *result;
1547 struct type *type = lhs->type ();
1548 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1550 type = resolve_dynamic_type (type, lhs->contents (),
1551 lhs->address ());
1553 if (rust_empty_enum_p (type))
1554 error (_("Cannot access field %s of empty enum %s"),
1555 field_name, type->name ());
1557 int fieldno = rust_enum_variant (type);
1558 lhs = lhs->primitive_field (0, fieldno, type);
1560 struct type *outer_type = type;
1561 type = lhs->type ();
1562 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1563 error (_("Attempting to access named field %s of tuple "
1564 "variant %s::%s, which has only anonymous fields"),
1565 field_name, outer_type->name (),
1566 rust_last_path_segment (type->name ()));
1570 result = value_struct_elt (&lhs, {}, field_name,
1571 NULL, "structure");
1573 catch (const gdb_exception_error &except)
1575 error (_("Could not find field %s of struct variant %s::%s"),
1576 field_name, outer_type->name (),
1577 rust_last_path_segment (type->name ()));
1580 else
1582 if (rust_slice_type_p (type))
1583 lhs = convert_slice (lhs);
1584 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1586 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1587 result = value::zero (result->type (), result->lval ());
1588 return result;
1591 value *
1592 rust_aggregate_operation::evaluate (struct type *expect_type,
1593 struct expression *exp,
1594 enum noside noside)
1596 struct type *type = std::get<0> (m_storage);
1597 CORE_ADDR addr = 0;
1598 struct value *addrval = NULL;
1599 value *result;
1601 if (noside == EVAL_NORMAL)
1603 addrval = value_allocate_space_in_inferior (type->length ());
1604 addr = value_as_long (addrval);
1605 result = value_at_lazy (type, addr);
1608 if (std::get<1> (m_storage) != nullptr)
1610 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1611 noside);
1613 if (noside == EVAL_NORMAL)
1615 /* This isn't quite right but will do for the time
1616 being, seeing that we can't implement the Copy
1617 trait anyway. */
1618 value_assign (result, init);
1622 for (const auto &item : std::get<2> (m_storage))
1624 value *val = item.second->evaluate (nullptr, exp, noside);
1625 if (noside == EVAL_NORMAL)
1627 const char *fieldname = item.first.c_str ();
1628 value *field = value_struct_elt (&result, {}, fieldname,
1629 nullptr, "structure");
1630 value_assign (field, val);
1634 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1635 result = value::allocate (type);
1636 else
1637 result = value_at_lazy (type, addr);
1639 return result;
1642 value *
1643 rust_structop::evaluate_funcall (struct type *expect_type,
1644 struct expression *exp,
1645 enum noside noside,
1646 const std::vector<operation_up> &ops)
1648 std::vector<struct value *> args (ops.size () + 1);
1650 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1651 type in order to look up the method. */
1652 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1653 /* We don't yet implement real Deref semantics. */
1654 while (args[0]->type ()->code () == TYPE_CODE_PTR)
1655 args[0] = value_ind (args[0]);
1657 struct type *type = args[0]->type ();
1658 if ((type->code () != TYPE_CODE_STRUCT
1659 && type->code () != TYPE_CODE_UNION
1660 && type->code () != TYPE_CODE_ENUM)
1661 || rust_tuple_type_p (type))
1662 error (_("Method calls only supported on struct or enum types"));
1663 if (type->name () == NULL)
1664 error (_("Method call on nameless type"));
1666 std::string name = (std::string (type->name ()) + "::"
1667 + std::get<1> (m_storage));
1669 const struct block *block = get_selected_block (0);
1670 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1671 SEARCH_FUNCTION_DOMAIN,
1672 nullptr);
1673 if (sym.symbol == NULL)
1674 error (_("Could not find function named '%s'"), name.c_str ());
1676 struct type *fn_type = sym.symbol->type ();
1677 if (fn_type->num_fields () == 0)
1678 error (_("Function '%s' takes no arguments"), name.c_str ());
1680 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1681 args[0] = value_addr (args[0]);
1683 value *function = address_of_variable (sym.symbol, block);
1685 for (int i = 0; i < ops.size (); ++i)
1686 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1688 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1689 return value::zero (fn_type->target_type (), not_lval);
1690 return call_function_by_hand (function, NULL, args);
1697 /* See language.h. */
1699 void
1700 rust_language::language_arch_info (struct gdbarch *gdbarch,
1701 struct language_arch_info *lai) const
1703 const struct builtin_type *builtin = builtin_type (gdbarch);
1705 /* Helper function to allow shorter lines below. */
1706 auto add = [&] (struct type * t) -> struct type *
1708 lai->add_primitive_type (t);
1709 return t;
1712 type_allocator alloc (gdbarch);
1713 struct type *bool_type
1714 = add (init_boolean_type (alloc, 8, 1, "bool"));
1715 add (init_character_type (alloc, 32, 1, "char"));
1716 add (init_integer_type (alloc, 8, 0, "i8"));
1717 struct type *u8_type
1718 = add (init_integer_type (alloc, 8, 1, "u8"));
1719 add (init_integer_type (alloc, 16, 0, "i16"));
1720 add (init_integer_type (alloc, 16, 1, "u16"));
1721 add (init_integer_type (alloc, 32, 0, "i32"));
1722 add (init_integer_type (alloc, 32, 1, "u32"));
1723 add (init_integer_type (alloc, 64, 0, "i64"));
1724 add (init_integer_type (alloc, 64, 1, "u64"));
1725 add (init_integer_type (alloc, 128, 0, "i128"));
1726 add (init_integer_type (alloc, 128, 1, "u128"));
1728 unsigned int length = 8 * builtin->builtin_data_ptr->length ();
1729 add (init_integer_type (alloc, length, 0, "isize"));
1730 struct type *usize_type
1731 = add (init_integer_type (alloc, length, 1, "usize"));
1733 add (init_float_type (alloc, 32, "f32", floatformats_ieee_single));
1734 add (init_float_type (alloc, 64, "f64", floatformats_ieee_double));
1735 add (init_integer_type (alloc, 0, 1, "()"));
1737 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1738 add (rust_slice_type ("&str", tem, usize_type));
1740 lai->set_bool_type (bool_type);
1741 lai->set_string_char_type (u8_type);
1744 /* See language.h. */
1746 void
1747 rust_language::print_type (struct type *type, const char *varstring,
1748 struct ui_file *stream, int show, int level,
1749 const struct type_print_options *flags) const
1751 print_offset_data podata (flags);
1752 rust_internal_print_type (type, varstring, stream, show, level,
1753 flags, false, &podata);
1756 /* See language.h. */
1758 void
1759 rust_language::emitchar (int ch, struct type *chtype,
1760 struct ui_file *stream, int quoter) const
1762 if (!rust_chartype_p (chtype))
1763 generic_emit_char (ch, chtype, stream, quoter,
1764 target_charset (chtype->arch ()));
1765 else if (ch == '\\' || ch == quoter)
1766 gdb_printf (stream, "\\%c", ch);
1767 else if (ch == '\n')
1768 gdb_puts ("\\n", stream);
1769 else if (ch == '\r')
1770 gdb_puts ("\\r", stream);
1771 else if (ch == '\t')
1772 gdb_puts ("\\t", stream);
1773 else if (ch == '\0')
1774 gdb_puts ("\\0", stream);
1775 else if (ch >= 32 && ch <= 127 && isprint (ch))
1776 gdb_putc (ch, stream);
1777 else if (ch <= 255)
1778 gdb_printf (stream, "\\x%02x", ch);
1779 else
1780 gdb_printf (stream, "\\u{%06x}", ch);
1783 /* See language.h. */
1785 bool
1786 rust_language::is_array_like (struct type *type) const
1788 if (!rust_slice_type_p (type))
1789 return false;
1790 return rust_array_like_element_type (type) != nullptr;
1793 /* See language.h. */
1795 bool
1796 rust_language::is_string_type_p (struct type *type) const
1798 LONGEST low_bound, high_bound;
1800 type = check_typedef (type);
1801 return ((type->code () == TYPE_CODE_STRING)
1802 || (type->code () == TYPE_CODE_PTR
1803 && (type->target_type ()->code () == TYPE_CODE_ARRAY
1804 && rust_u8_type_p (type->target_type ()->target_type ())
1805 && get_array_bounds (type->target_type (), &low_bound,
1806 &high_bound)))
1807 || (type->code () == TYPE_CODE_STRUCT
1808 && !rust_enum_p (type)
1809 && rust_slice_type_p (type)
1810 && strcmp (type->name (), "&str") == 0));
1813 /* See language.h. */
1815 struct block_symbol
1816 rust_language::lookup_symbol_nonlocal
1817 (const char *name, const struct block *block,
1818 const domain_search_flags domain) const
1820 struct block_symbol result = {};
1822 const char *scope = block == nullptr ? "" : block->scope ();
1823 symbol_lookup_debug_printf
1824 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1825 name, host_address_to_string (block), scope,
1826 domain_name (domain).c_str ());
1828 /* Look up bare names in the block's scope. */
1829 std::string scopedname;
1830 if (name[cp_find_first_component (name)] == '\0')
1832 if (scope[0] != '\0')
1834 scopedname = std::string (scope) + "::" + name;
1835 name = scopedname.c_str ();
1837 else
1838 name = NULL;
1841 if (name != NULL)
1843 result = lookup_symbol_in_static_block (name, block, domain);
1844 if (result.symbol == NULL)
1845 result = lookup_global_symbol (name, block, domain);
1847 return result;
1850 /* Single instance of the Rust language class. */
1852 static rust_language rust_language_defn;