[gdb/symtab] Workaround PR gas/31115
[binutils-gdb.git] / gdb / rust-lang.c
blobab537cc975289f21170cd8e64886f734a5d13e1a
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/>. */
20 #include "defs.h"
22 #include <ctype.h>
24 #include "block.h"
25 #include "c-lang.h"
26 #include "charset.h"
27 #include "cp-support.h"
28 #include "demangle.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 i, field_number;
126 field_number = 0;
128 if (type->code () != TYPE_CODE_STRUCT)
129 return false;
130 for (i = 0; i < type->num_fields (); ++i)
132 if (!type->field (i).is_static ())
134 char buf[20];
136 xsnprintf (buf, sizeof (buf), "__%d", field_number);
137 if (strcmp (buf, type->field (i).name ()) != 0)
138 return false;
139 field_number++;
142 return true;
145 /* See rust-lang.h. */
147 bool
148 rust_tuple_struct_type_p (struct type *type)
150 /* This is just an approximation until DWARF can represent Rust more
151 precisely. We exclude zero-length structs because they may not
152 be tuple structs, and there's no way to tell. */
153 return type->num_fields () > 0 && rust_underscore_fields (type);
156 /* Return true if TYPE is "slice-like"; false otherwise. */
158 static bool
159 rust_slice_type_p (const struct type *type)
161 if (type->code () == TYPE_CODE_STRUCT
162 && type->name () != NULL
163 && type->num_fields () == 2)
165 /* The order of fields doesn't matter. While it would be nice
166 to check for artificiality here, the Rust compiler doesn't
167 emit this information. */
168 const char *n1 = type->field (0).name ();
169 const char *n2 = type->field (1).name ();
170 return ((streq (n1, "data_ptr") && streq (n2, "length"))
171 || (streq (n2, "data_ptr") && streq (n1, "length")));
173 return false;
176 /* Return true if TYPE is a range type, otherwise false. */
178 static bool
179 rust_range_type_p (struct type *type)
181 int i;
183 if (type->code () != TYPE_CODE_STRUCT
184 || type->num_fields () > 2
185 || type->name () == NULL
186 || strstr (type->name (), "::Range") == NULL)
187 return false;
189 if (type->num_fields () == 0)
190 return true;
192 i = 0;
193 if (strcmp (type->field (0).name (), "start") == 0)
195 if (type->num_fields () == 1)
196 return true;
197 i = 1;
199 else if (type->num_fields () == 2)
201 /* First field had to be "start". */
202 return false;
205 return strcmp (type->field (i).name (), "end") == 0;
208 /* Return true if TYPE is an inclusive range type, otherwise false.
209 This is only valid for types which are already known to be range
210 types. */
212 static bool
213 rust_inclusive_range_type_p (struct type *type)
215 return (strstr (type->name (), "::RangeInclusive") != NULL
216 || strstr (type->name (), "::RangeToInclusive") != NULL);
219 /* Return true if TYPE seems to be the type "u8", otherwise false. */
221 static bool
222 rust_u8_type_p (struct type *type)
224 return (type->code () == TYPE_CODE_INT
225 && type->is_unsigned ()
226 && type->length () == 1);
229 /* Return true if TYPE is a Rust character type. */
231 static bool
232 rust_chartype_p (struct type *type)
234 return (type->code () == TYPE_CODE_CHAR
235 && type->length () == 4
236 && type->is_unsigned ());
239 /* If VALUE represents a trait object pointer, return the underlying
240 pointer with the correct (i.e., runtime) type. Otherwise, return
241 NULL. */
243 static struct value *
244 rust_get_trait_object_pointer (struct value *value)
246 struct type *type = check_typedef (value->type ());
248 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
249 return NULL;
251 /* Try to be a bit resilient if the ABI changes. */
252 int vtable_field = 0;
253 for (int i = 0; i < 2; ++i)
255 if (strcmp (type->field (i).name (), "vtable") == 0)
256 vtable_field = i;
257 else if (strcmp (type->field (i).name (), "pointer") != 0)
258 return NULL;
261 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
262 struct symbol *symbol = find_symbol_at_address (vtable);
263 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
264 return NULL;
266 struct rust_vtable_symbol *vtable_sym
267 = static_cast<struct rust_vtable_symbol *> (symbol);
268 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
269 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
272 /* Find and possibly rewrite the unsized part of a slice-like type.
274 This function has two modes. If the out parameters are both NULL,
275 it will return true if an unsized member of IN_TYPE is found.
277 If the out parameters are both non-NULL, it will do the same, but
278 will also rewrite the unsized member's type to be an array of the
279 appropriate type. BOUND is the upper bound of the new array.
281 See convert_slice to understand the different kinds of unsized type
282 and how they are represented.
284 static bool
285 rewrite_slice_type (struct type *in_type, struct type **new_type,
286 LONGEST bound, ULONGEST *additional_length)
288 if (in_type->code () != TYPE_CODE_STRUCT)
289 return false;
291 unsigned nfields = in_type->num_fields ();
292 if (nfields == 0)
293 return false;
295 struct type *rewritten;
296 const field &field = in_type->field (nfields - 1);
297 struct type *field_type = field.type ();
298 if (field.loc_kind () == FIELD_LOC_KIND_BITPOS
299 && field.loc_bitpos () == 8 * in_type->length ())
301 if (additional_length == nullptr)
302 return true;
303 rewritten = lookup_array_range_type (field_type, 0, bound);
304 *additional_length = rewritten->length ();
306 else
308 if (!rewrite_slice_type (field_type, &rewritten, bound,
309 additional_length))
310 return false;
311 if (additional_length == nullptr)
312 return true;
315 struct type *result = copy_type (in_type);
316 result->copy_fields (in_type);
317 result->field (nfields - 1).set_type (rewritten);
318 result->set_length (result->length () + *additional_length);
320 *new_type = result;
321 return true;
324 /* Convert a Rust slice to its "true" representation.
326 The Rust compiler emits slices as "fat" pointers like:
328 struct { payload *data_ptr; usize length }
330 Any sort of unsized type is emitted this way.
332 If 'payload' is a struct type, then it must be searched to see if
333 the trailing field is unsized. This has to be done recursively (as
334 in, if the final field in the struct type itself has struct type,
335 then that type must be searched). In this scenario, the unsized
336 field can be recognized because it does not contribute to the
337 type's size.
339 If 'payload' does not have a trailing unsized type, or if it is not
340 of struct type, then this slice is "array-like". In this case
341 rewriting will return an array.
343 static struct value *
344 convert_slice (struct value *val)
346 struct type *type = check_typedef (val->type ());
347 /* This must have been checked by the caller. */
348 gdb_assert (rust_slice_type_p (type));
350 struct value *len = value_struct_elt (&val, {}, "length", nullptr,
351 "slice");
352 LONGEST llen = value_as_long (len);
354 struct value *ptr = value_struct_elt (&val, {}, "data_ptr", nullptr,
355 "slice");
356 struct type *original_type = ptr->type ()->target_type ();
357 ULONGEST new_length_storage = 0;
358 struct type *new_type = nullptr;
359 if (!rewrite_slice_type (original_type, &new_type, llen - 1,
360 &new_length_storage))
361 new_type = lookup_array_range_type (original_type, 0, llen - 1);
363 struct value *result = value::allocate_lazy (new_type);
364 result->set_lval (lval_memory);
365 result->set_address (value_as_address (ptr));
366 result->fetch_lazy ();
368 return result;
371 /* If TYPE is an array-like slice, return the element type; otherwise
372 return NULL. */
373 static struct type *
374 rust_array_like_element_type (struct type *type)
376 /* Caller must check this. */
377 gdb_assert (rust_slice_type_p (type));
378 for (int i = 0; i < type->num_fields (); ++i)
380 if (strcmp (type->field (i).name (), "data_ptr") == 0)
382 struct type *base_type = type->field (i).type ()->target_type ();
383 if (rewrite_slice_type (base_type, nullptr, 0, nullptr))
384 return nullptr;
385 return base_type;
388 return nullptr;
393 /* See language.h. */
395 void
396 rust_language::printstr (struct ui_file *stream, struct type *type,
397 const gdb_byte *string, unsigned int length,
398 const char *user_encoding, int force_ellipses,
399 const struct value_print_options *options) const
401 /* Rust always uses UTF-8, but let the caller override this if need
402 be. */
403 const char *encoding = user_encoding;
404 if (user_encoding == NULL || !*user_encoding)
406 /* In Rust strings, characters are "u8". */
407 if (rust_u8_type_p (type))
408 encoding = "UTF-8";
409 else
411 /* This is probably some C string, so let's let C deal with
412 it. */
413 language_defn::printstr (stream, type, string, length,
414 user_encoding, force_ellipses,
415 options);
416 return;
420 /* This is not ideal as it doesn't use our character printer. */
421 generic_printstr (stream, type, string, length, encoding, force_ellipses,
422 '"', 0, options);
427 static const struct generic_val_print_decorations rust_decorations =
429 /* Complex isn't used in Rust, but we provide C-ish values just in
430 case. */
432 " + ",
433 " * I",
434 "true",
435 "false",
436 "()",
437 "[",
441 /* See rust-lang.h. */
443 struct value *
444 rust_slice_to_array (struct value *val)
446 val = convert_slice (val);
447 if (val->type ()->code () != TYPE_CODE_ARRAY)
448 return nullptr;
449 return val;
452 /* Helper function to print a slice. */
454 void
455 rust_language::val_print_slice
456 (struct value *val, struct ui_file *stream, int recurse,
457 const struct value_print_options *options) const
459 struct type *orig_type = check_typedef (val->type ());
461 val = convert_slice (val);
462 struct type *type = check_typedef (val->type ());
464 /* &str is handled here; but for all other slice types it is fine to
465 simply print the contents. */
466 if (orig_type->name () != nullptr
467 && strcmp (orig_type->name (), "&str") == 0)
469 LONGEST low_bound, high_bound;
470 if (get_array_bounds (type, &low_bound, &high_bound))
472 val_print_string (type->target_type (), "UTF-8",
473 val->address (), high_bound - low_bound + 1,
474 stream, options);
475 return;
479 value_print_inner (val, stream, recurse, options);
482 /* See rust-lang.h. */
484 void
485 rust_language::val_print_struct
486 (struct value *val, struct ui_file *stream, int recurse,
487 const struct value_print_options *options) const
489 int i;
490 int first_field;
491 struct type *type = check_typedef (val->type ());
493 if (rust_slice_type_p (type))
495 val_print_slice (val, stream, recurse, options);
496 return;
499 bool is_tuple = rust_tuple_type_p (type);
500 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
501 struct value_print_options opts;
503 if (!is_tuple)
505 if (type->name () != NULL)
506 gdb_printf (stream, "%s", type->name ());
508 if (type->num_fields () == 0)
509 return;
511 if (type->name () != NULL)
512 gdb_puts (" ", stream);
515 if (is_tuple || is_tuple_struct)
516 gdb_puts ("(", stream);
517 else
518 gdb_puts ("{", stream);
520 opts = *options;
521 opts.deref_ref = false;
523 first_field = 1;
524 for (i = 0; i < type->num_fields (); ++i)
526 if (type->field (i).is_static ())
527 continue;
529 if (!first_field)
530 gdb_puts (",", stream);
532 if (options->prettyformat)
534 gdb_puts ("\n", stream);
535 print_spaces (2 + 2 * recurse, stream);
537 else if (!first_field)
538 gdb_puts (" ", stream);
540 first_field = 0;
542 if (!is_tuple && !is_tuple_struct)
544 fputs_styled (type->field (i).name (),
545 variable_name_style.style (), stream);
546 gdb_puts (": ", stream);
549 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
550 this);
553 if (options->prettyformat)
555 gdb_puts ("\n", stream);
556 print_spaces (2 * recurse, stream);
559 if (is_tuple || is_tuple_struct)
560 gdb_puts (")", stream);
561 else
562 gdb_puts ("}", stream);
565 /* See rust-lang.h. */
567 void
568 rust_language::print_enum (struct value *val, struct ui_file *stream,
569 int recurse,
570 const struct value_print_options *options) const
572 struct value_print_options opts = *options;
573 struct type *type = check_typedef (val->type ());
575 opts.deref_ref = false;
577 gdb_assert (rust_enum_p (type));
578 gdb::array_view<const gdb_byte> view
579 (val->contents_for_printing ().data (),
580 val->type ()->length ());
581 type = resolve_dynamic_type (type, view, val->address ());
583 if (rust_empty_enum_p (type))
585 /* Print the enum type name here to be more clear. */
586 gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
587 type->name (),
588 metadata_style.style ().ptr (), nullptr);
589 return;
592 int variant_fieldno = rust_enum_variant (type);
593 val = val->primitive_field (0, variant_fieldno, type);
594 struct type *variant_type = type->field (variant_fieldno).type ();
596 int nfields = variant_type->num_fields ();
598 bool is_tuple = rust_tuple_struct_type_p (variant_type);
600 gdb_printf (stream, "%s", variant_type->name ());
601 if (nfields == 0)
603 /* In case of a nullary variant like 'None', just output
604 the name. */
605 return;
608 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
609 if (is_tuple)
610 gdb_printf (stream, "(");
611 else
613 /* struct variant. */
614 gdb_printf (stream, "{");
617 bool first_field = true;
618 for (int j = 0; j < nfields; j++)
620 if (!first_field)
621 gdb_puts (", ", stream);
622 first_field = false;
624 if (!is_tuple)
625 gdb_printf (stream, "%ps: ",
626 styled_string (variable_name_style.style (),
627 variant_type->field (j).name ()));
629 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
630 this);
633 if (is_tuple)
634 gdb_puts (")", stream);
635 else
636 gdb_puts ("}", stream);
639 /* See language.h. */
641 void
642 rust_language::value_print_inner
643 (struct value *val, struct ui_file *stream, int recurse,
644 const struct value_print_options *options) const
646 struct value_print_options opts = *options;
647 opts.deref_ref = true;
649 if (opts.prettyformat == Val_prettyformat_default)
650 opts.prettyformat = (opts.prettyformat_structs
651 ? Val_prettyformat : Val_no_prettyformat);
653 struct type *type = check_typedef (val->type ());
654 switch (type->code ())
656 case TYPE_CODE_PTR:
658 LONGEST low_bound, high_bound;
660 if (type->target_type ()->code () == TYPE_CODE_ARRAY
661 && rust_u8_type_p (type->target_type ()->target_type ())
662 && get_array_bounds (type->target_type (), &low_bound,
663 &high_bound))
665 /* We have a pointer to a byte string, so just print
666 that. */
667 struct type *elttype = check_typedef (type->target_type ());
668 CORE_ADDR addr = value_as_address (val);
669 struct gdbarch *arch = type->arch ();
671 if (opts.addressprint)
673 gdb_puts (paddress (arch, addr), stream);
674 gdb_puts (" ", stream);
677 gdb_puts ("b", stream);
678 val_print_string (elttype->target_type (), "ASCII", addr,
679 high_bound - low_bound + 1, stream,
680 &opts);
681 break;
684 goto generic_print;
686 case TYPE_CODE_INT:
687 /* Recognize the unit type. */
688 if (type->is_unsigned () && type->length () == 0
689 && type->name () != NULL && strcmp (type->name (), "()") == 0)
691 gdb_puts ("()", stream);
692 break;
694 goto generic_print;
696 case TYPE_CODE_STRING:
698 LONGEST low_bound, high_bound;
700 if (!get_array_bounds (type, &low_bound, &high_bound))
701 error (_("Could not determine the array bounds"));
703 /* If we see a plain TYPE_CODE_STRING, then we're printing a
704 byte string, hence the choice of "ASCII" as the
705 encoding. */
706 gdb_puts ("b", stream);
707 printstr (stream, type->target_type (),
708 val->contents_for_printing ().data (),
709 high_bound - low_bound + 1, "ASCII", 0, &opts);
711 break;
713 case TYPE_CODE_ARRAY:
715 LONGEST low_bound, high_bound;
717 if (get_array_bounds (type, &low_bound, &high_bound)
718 && high_bound - low_bound + 1 == 0)
719 gdb_puts ("[]", stream);
720 else
721 goto generic_print;
723 break;
725 case TYPE_CODE_UNION:
726 /* Untagged unions are printed as if they are structs. Since
727 the field bit positions overlap in the debuginfo, the code
728 for printing a union is same as that for a struct, the only
729 difference is that the input type will have overlapping
730 fields. */
731 val_print_struct (val, stream, recurse, &opts);
732 break;
734 case TYPE_CODE_STRUCT:
735 if (rust_enum_p (type))
736 print_enum (val, stream, recurse, &opts);
737 else
738 val_print_struct (val, stream, recurse, &opts);
739 break;
741 default:
742 generic_print:
743 /* Nothing special yet. */
744 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
748 /* See language.h. */
750 void
751 rust_language::value_print
752 (struct value *val, struct ui_file *stream,
753 const struct value_print_options *options) const
755 value_print_options opts = *options;
756 opts.deref_ref = true;
758 struct type *type = check_typedef (val->type ());
759 if (type->is_pointer_or_reference ())
761 gdb_printf (stream, "(");
762 type_print (val->type (), "", stream, -1);
763 gdb_printf (stream, ") ");
766 return common_val_print (val, stream, 0, &opts, this);
771 static void
772 rust_internal_print_type (struct type *type, const char *varstring,
773 struct ui_file *stream, int show, int level,
774 const struct type_print_options *flags,
775 bool for_rust_enum, print_offset_data *podata);
777 /* Print a struct or union typedef. */
778 static void
779 rust_print_struct_def (struct type *type, const char *varstring,
780 struct ui_file *stream, int show, int level,
781 const struct type_print_options *flags,
782 bool for_rust_enum, print_offset_data *podata)
784 /* Print a tuple type simply. */
785 if (rust_tuple_type_p (type))
787 gdb_puts (type->name (), stream);
788 return;
791 /* If we see a base class, delegate to C. */
792 if (TYPE_N_BASECLASSES (type) > 0)
793 c_print_type (type, varstring, stream, show, level, language_rust, flags);
795 if (flags->print_offsets)
797 /* Temporarily bump the level so that the output lines up
798 correctly. */
799 level += 2;
802 /* Compute properties of TYPE here because, in the enum case, the
803 rest of the code ends up looking only at the variant part. */
804 const char *tagname = type->name ();
805 bool is_tuple_struct = rust_tuple_struct_type_p (type);
806 bool is_tuple = rust_tuple_type_p (type);
807 bool is_enum = rust_enum_p (type);
809 if (for_rust_enum)
811 /* Already printing an outer enum, so nothing to print here. */
813 else
815 /* This code path is also used by unions and enums. */
816 if (is_enum)
818 gdb_puts ("enum ", stream);
819 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
820 if (prop != nullptr && prop->kind () == PROP_TYPE)
821 type = prop->original_type ();
823 else if (type->code () == TYPE_CODE_STRUCT)
824 gdb_puts ("struct ", stream);
825 else
826 gdb_puts ("union ", stream);
828 if (tagname != NULL)
829 gdb_puts (tagname, stream);
832 if (type->num_fields () == 0 && !is_tuple)
833 return;
834 if (for_rust_enum && !flags->print_offsets)
835 gdb_puts (is_tuple_struct ? "(" : "{", stream);
836 else
837 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
839 /* When printing offsets, we rearrange the fields into storage
840 order. This lets us show holes more clearly. We work using
841 field indices here because it simplifies calls to
842 print_offset_data::update below. */
843 std::vector<int> fields;
844 for (int i = 0; i < type->num_fields (); ++i)
846 if (type->field (i).is_static ())
847 continue;
848 if (is_enum && type->field (i).is_artificial ())
849 continue;
850 fields.push_back (i);
852 if (flags->print_offsets)
853 std::sort (fields.begin (), fields.end (),
854 [&] (int a, int b)
856 return (type->field (a).loc_bitpos ()
857 < type->field (b).loc_bitpos ());
860 for (int i : fields)
862 QUIT;
864 gdb_assert (!type->field (i).is_static ());
865 gdb_assert (! (is_enum && type->field (i).is_artificial ()));
867 if (flags->print_offsets)
868 podata->update (type, i, stream);
870 /* We'd like to print "pub" here as needed, but rustc
871 doesn't emit the debuginfo, and our types don't have
872 cplus_struct_type attached. */
874 /* For a tuple struct we print the type but nothing
875 else. */
876 if (!for_rust_enum || flags->print_offsets)
877 print_spaces (level + 2, stream);
878 if (is_enum)
879 fputs_styled (type->field (i).name (), variable_name_style.style (),
880 stream);
881 else if (!is_tuple_struct)
882 gdb_printf (stream, "%ps: ",
883 styled_string (variable_name_style.style (),
884 type->field (i).name ()));
886 rust_internal_print_type (type->field (i).type (), NULL,
887 stream, (is_enum ? show : show - 1),
888 level + 2, flags, is_enum, podata);
889 if (!for_rust_enum || flags->print_offsets)
890 gdb_puts (",\n", stream);
891 /* Note that this check of "I" is ok because we only sorted the
892 fields by offset when print_offsets was set, so we won't take
893 this branch in that case. */
894 else if (i + 1 < type->num_fields ())
895 gdb_puts (", ", stream);
898 if (flags->print_offsets)
900 /* Undo the temporary level increase we did above. */
901 level -= 2;
902 podata->finish (type, level, stream);
903 print_spaces (print_offset_data::indentation, stream);
904 if (level == 0)
905 print_spaces (2, stream);
907 if (!for_rust_enum || flags->print_offsets)
908 print_spaces (level, stream);
909 gdb_puts (is_tuple_struct ? ")" : "}", stream);
912 /* la_print_type implementation for Rust. */
914 static void
915 rust_internal_print_type (struct type *type, const char *varstring,
916 struct ui_file *stream, int show, int level,
917 const struct type_print_options *flags,
918 bool for_rust_enum, print_offset_data *podata)
920 QUIT;
921 if (show <= 0
922 && type->name () != NULL)
924 /* Rust calls the unit type "void" in its debuginfo,
925 but we don't want to print it as that. */
926 if (type->code () == TYPE_CODE_VOID)
927 gdb_puts ("()", stream);
928 else
929 gdb_puts (type->name (), stream);
930 return;
933 type = check_typedef (type);
934 switch (type->code ())
936 case TYPE_CODE_VOID:
937 /* If we have an enum, we've already printed the type's
938 unqualified name, and there is nothing else to print
939 here. */
940 if (!for_rust_enum)
941 gdb_puts ("()", stream);
942 break;
944 case TYPE_CODE_FUNC:
945 /* Delegate varargs to the C printer. */
946 if (type->has_varargs ())
947 goto c_printer;
949 gdb_puts ("fn ", stream);
950 if (varstring != NULL)
951 gdb_puts (varstring, stream);
952 gdb_puts ("(", stream);
953 for (int i = 0; i < type->num_fields (); ++i)
955 QUIT;
956 if (i > 0)
957 gdb_puts (", ", stream);
958 rust_internal_print_type (type->field (i).type (), "", stream,
959 -1, 0, flags, false, podata);
961 gdb_puts (")", stream);
962 /* If it returns unit, we can omit the return type. */
963 if (type->target_type ()->code () != TYPE_CODE_VOID)
965 gdb_puts (" -> ", stream);
966 rust_internal_print_type (type->target_type (), "", stream,
967 -1, 0, flags, false, podata);
969 break;
971 case TYPE_CODE_ARRAY:
973 LONGEST low_bound, high_bound;
975 gdb_puts ("[", stream);
976 rust_internal_print_type (type->target_type (), NULL,
977 stream, show - 1, level, flags, false,
978 podata);
980 if (type->bounds ()->high.kind () == PROP_LOCEXPR
981 || type->bounds ()->high.kind () == PROP_LOCLIST)
982 gdb_printf (stream, "; variable length");
983 else if (get_array_bounds (type, &low_bound, &high_bound))
984 gdb_printf (stream, "; %s",
985 plongest (high_bound - low_bound + 1));
986 gdb_puts ("]", stream);
988 break;
990 case TYPE_CODE_UNION:
991 case TYPE_CODE_STRUCT:
992 rust_print_struct_def (type, varstring, stream, show, level, flags,
993 for_rust_enum, podata);
994 break;
996 case TYPE_CODE_ENUM:
998 int len = 0;
1000 gdb_puts ("enum ", stream);
1001 if (type->name () != NULL)
1003 gdb_puts (type->name (), stream);
1004 gdb_puts (" ", stream);
1005 len = strlen (type->name ());
1007 gdb_puts ("{\n", stream);
1009 for (int i = 0; i < type->num_fields (); ++i)
1011 const char *name = type->field (i).name ();
1013 QUIT;
1015 if (len > 0
1016 && strncmp (name, type->name (), len) == 0
1017 && name[len] == ':'
1018 && name[len + 1] == ':')
1019 name += len + 2;
1020 gdb_printf (stream, "%*s%ps,\n",
1021 level + 2, "",
1022 styled_string (variable_name_style.style (),
1023 name));
1026 gdb_puts ("}", stream);
1028 break;
1030 case TYPE_CODE_PTR:
1032 if (type->name () != nullptr)
1033 gdb_puts (type->name (), stream);
1034 else
1036 /* We currently can't distinguish between pointers and
1037 references. */
1038 gdb_puts ("*mut ", stream);
1039 type_print (type->target_type (), "", stream, 0);
1042 break;
1044 default:
1045 c_printer:
1046 c_print_type (type, varstring, stream, show, level, language_rust,
1047 flags);
1053 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1054 -- either on an obstack or on a gdbarch. */
1056 static struct type *
1057 rust_composite_type (struct type *original,
1058 const char *name,
1059 const char *field1, struct type *type1,
1060 const char *field2, struct type *type2)
1062 struct type *result = type_allocator (original).new_type ();
1063 int i, nfields, bitpos;
1065 nfields = 0;
1066 if (field1 != NULL)
1067 ++nfields;
1068 if (field2 != NULL)
1069 ++nfields;
1071 result->set_code (TYPE_CODE_STRUCT);
1072 result->set_name (name);
1074 result->alloc_fields (nfields);
1076 i = 0;
1077 bitpos = 0;
1078 if (field1 != NULL)
1080 struct field *field = &result->field (i);
1082 field->set_loc_bitpos (bitpos);
1083 bitpos += type1->length () * TARGET_CHAR_BIT;
1085 field->set_name (field1);
1086 field->set_type (type1);
1087 ++i;
1089 if (field2 != NULL)
1091 struct field *field = &result->field (i);
1092 unsigned align = type_align (type2);
1094 if (align != 0)
1096 int delta;
1098 align *= TARGET_CHAR_BIT;
1099 delta = bitpos % align;
1100 if (delta != 0)
1101 bitpos += align - delta;
1103 field->set_loc_bitpos (bitpos);
1105 field->set_name (field2);
1106 field->set_type (type2);
1107 ++i;
1110 if (i > 0)
1111 result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT
1112 + result->field (i - 1).type ()->length ());
1113 return result;
1116 /* See rust-lang.h. */
1118 struct type *
1119 rust_slice_type (const char *name, struct type *elt_type,
1120 struct type *usize_type)
1122 struct type *type;
1124 elt_type = lookup_pointer_type (elt_type);
1125 type = rust_composite_type (elt_type, name,
1126 "data_ptr", elt_type,
1127 "length", usize_type);
1129 return type;
1134 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1136 struct value *
1137 rust_range (struct type *expect_type, struct expression *exp,
1138 enum noside noside, enum range_flag kind,
1139 struct value *low, struct value *high)
1141 struct value *addrval, *result;
1142 CORE_ADDR addr;
1143 struct type *range_type;
1144 struct type *index_type;
1145 struct type *temp_type;
1146 const char *name;
1148 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
1150 if (low == NULL)
1152 if (high == NULL)
1154 index_type = NULL;
1155 name = "std::ops::RangeFull";
1157 else
1159 index_type = high->type ();
1160 name = (inclusive
1161 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1164 else
1166 if (high == NULL)
1168 index_type = low->type ();
1169 name = "std::ops::RangeFrom";
1171 else
1173 if (!types_equal (low->type (), high->type ()))
1174 error (_("Range expression with different types"));
1175 index_type = low->type ();
1176 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1180 /* If we don't have an index type, just allocate this on the
1181 arch. Here any type will do. */
1182 temp_type = (index_type == NULL
1183 ? language_bool_type (exp->language_defn, exp->gdbarch)
1184 : index_type);
1185 /* It would be nicer to cache the range type. */
1186 range_type = rust_composite_type (temp_type, name,
1187 low == NULL ? NULL : "start", index_type,
1188 high == NULL ? NULL : "end", index_type);
1190 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1191 return value::zero (range_type, lval_memory);
1193 addrval = value_allocate_space_in_inferior (range_type->length ());
1194 addr = value_as_long (addrval);
1195 result = value_at_lazy (range_type, addr);
1197 if (low != NULL)
1199 struct value *start = value_struct_elt (&result, {}, "start", NULL,
1200 "range");
1202 value_assign (start, low);
1205 if (high != NULL)
1207 struct value *end = value_struct_elt (&result, {}, "end", NULL,
1208 "range");
1210 value_assign (end, high);
1213 result = value_at_lazy (range_type, addr);
1214 return result;
1217 /* A helper function to compute the range and kind given a range
1218 value. TYPE is the type of the range value. RANGE is the range
1219 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1220 parameters might be filled in, or might not be, depending on the
1221 kind of range this is. KIND will always be set to the appropriate
1222 value describing the kind of range, and this can be used to
1223 determine whether LOW or HIGH are valid. */
1225 static void
1226 rust_compute_range (struct type *type, struct value *range,
1227 LONGEST *low, LONGEST *high,
1228 range_flags *kind)
1230 int i;
1232 *low = 0;
1233 *high = 0;
1234 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1236 if (type->num_fields () == 0)
1237 return;
1239 i = 0;
1240 if (strcmp (type->field (0).name (), "start") == 0)
1242 *kind = RANGE_HIGH_BOUND_DEFAULT;
1243 *low = value_as_long (value_field (range, 0));
1244 ++i;
1246 if (type->num_fields () > i
1247 && strcmp (type->field (i).name (), "end") == 0)
1249 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1250 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
1251 *high = value_as_long (value_field (range, i));
1253 if (rust_inclusive_range_type_p (type))
1254 ++*high;
1258 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1260 struct value *
1261 rust_subscript (struct type *expect_type, struct expression *exp,
1262 enum noside noside, bool for_addr,
1263 struct value *lhs, struct value *rhs)
1265 struct value *result;
1266 struct type *rhstype;
1267 LONGEST low, high_bound;
1268 /* Initialized to appease the compiler. */
1269 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1270 LONGEST high = 0;
1271 int want_slice = 0;
1273 rhstype = check_typedef (rhs->type ());
1274 if (rust_range_type_p (rhstype))
1276 if (!for_addr)
1277 error (_("Can't take slice of array without '&'"));
1278 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1279 want_slice = 1;
1281 else
1282 low = value_as_long (rhs);
1284 struct type *type = check_typedef (lhs->type ());
1285 struct type *orig_type = type;
1286 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1288 struct type *base_type = nullptr;
1289 if (type->code () == TYPE_CODE_ARRAY)
1290 base_type = type->target_type ();
1291 else if (rust_slice_type_p (type))
1293 base_type = rust_array_like_element_type (type);
1294 if (base_type == nullptr)
1295 error (_("Cannot subscript non-array-like slice"));
1297 else if (type->code () == TYPE_CODE_PTR)
1298 base_type = type->target_type ();
1299 else
1300 error (_("Cannot subscript non-array type"));
1302 struct type *new_type;
1303 if (want_slice)
1305 if (rust_slice_type_p (type))
1306 new_type = type;
1307 else
1309 struct type *usize
1310 = language_lookup_primitive_type (exp->language_defn,
1311 exp->gdbarch,
1312 "usize");
1313 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1316 else
1317 new_type = base_type;
1319 return value::zero (new_type, lhs->lval ());
1321 else
1323 LONGEST low_bound;
1324 struct value *base;
1326 if (rust_slice_type_p (type))
1328 lhs = convert_slice (lhs);
1329 type = check_typedef (lhs->type ());
1332 if (type->code () == TYPE_CODE_ARRAY)
1334 base = lhs;
1335 if (!get_array_bounds (type, &low_bound, &high_bound))
1336 error (_("Can't compute array bounds"));
1337 if (low_bound != 0)
1338 error (_("Found array with non-zero lower bound"));
1339 ++high_bound;
1341 else if (type->code () == TYPE_CODE_PTR)
1343 base = lhs;
1344 low_bound = 0;
1345 high_bound = LONGEST_MAX;
1347 else
1348 error (_("Cannot subscript non-array type"));
1350 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
1351 low = low_bound;
1352 if (low < 0)
1353 error (_("Index less than zero"));
1354 if (low > high_bound)
1355 error (_("Index greater than length"));
1357 result = value_subscript (base, low);
1360 if (for_addr)
1362 if (want_slice)
1364 struct type *usize, *slice;
1365 CORE_ADDR addr;
1366 struct value *addrval, *tem;
1368 if (kind & RANGE_HIGH_BOUND_DEFAULT)
1369 high = high_bound;
1370 if (high < 0)
1371 error (_("High index less than zero"));
1372 if (low > high)
1373 error (_("Low index greater than high index"));
1374 if (high > high_bound)
1375 error (_("High index greater than length"));
1377 usize = language_lookup_primitive_type (exp->language_defn,
1378 exp->gdbarch,
1379 "usize");
1380 /* Preserve the name for slice-of-slice; this lets
1381 string-printing work a bit more nicely. */
1382 const char *new_name = ((orig_type != nullptr
1383 && rust_slice_type_p (orig_type))
1384 ? orig_type->name () : "&[*gdb*]");
1386 slice = rust_slice_type (new_name, result->type (), usize);
1388 addrval = value_allocate_space_in_inferior (slice->length ());
1389 addr = value_as_long (addrval);
1390 tem = value_at_lazy (slice, addr);
1392 value_assign (value_field (tem, 0), value_addr (result));
1393 value_assign (value_field (tem, 1),
1394 value_from_longest (usize, high - low));
1396 result = value_at_lazy (slice, addr);
1398 else
1399 result = value_addr (result);
1402 return result;
1405 namespace expr
1408 struct value *
1409 rust_unop_ind_operation::evaluate (struct type *expect_type,
1410 struct expression *exp,
1411 enum noside noside)
1413 if (noside != EVAL_NORMAL)
1414 return unop_ind_operation::evaluate (expect_type, exp, noside);
1416 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1417 noside);
1418 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1419 if (trait_ptr != NULL)
1420 value = trait_ptr;
1422 return value_ind (value);
1425 } /* namespace expr */
1427 /* A helper function for UNOP_COMPLEMENT. */
1429 struct value *
1430 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1431 enum noside noside,
1432 enum exp_opcode opcode,
1433 struct value *value)
1435 if (value->type ()->code () == TYPE_CODE_BOOL)
1436 return value_from_longest (value->type (), value_logical_not (value));
1437 return value_complement (value);
1440 /* A helper function for OP_ARRAY. */
1442 struct value *
1443 eval_op_rust_array (struct type *expect_type, struct expression *exp,
1444 enum noside noside,
1445 enum exp_opcode opcode,
1446 struct value *elt, struct value *ncopies)
1448 int copies = value_as_long (ncopies);
1449 if (copies < 0)
1450 error (_("Array with negative number of elements"));
1452 if (noside == EVAL_NORMAL)
1453 return value_array (0, std::vector<value *> (copies, elt));
1454 else
1456 struct type *arraytype
1457 = lookup_array_range_type (elt->type (), 0, copies - 1);
1458 return value::allocate (arraytype);
1462 namespace expr
1465 struct value *
1466 rust_struct_anon::evaluate (struct type *expect_type,
1467 struct expression *exp,
1468 enum noside noside)
1470 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1471 int field_number = std::get<0> (m_storage);
1473 struct type *type = lhs->type ();
1475 if (type->code () == TYPE_CODE_STRUCT)
1477 struct type *outer_type = NULL;
1479 if (rust_enum_p (type))
1481 type = resolve_dynamic_type (type, lhs->contents (),
1482 lhs->address ());
1484 if (rust_empty_enum_p (type))
1485 error (_("Cannot access field %d of empty enum %s"),
1486 field_number, type->name ());
1488 int fieldno = rust_enum_variant (type);
1489 lhs = lhs->primitive_field (0, fieldno, type);
1490 outer_type = type;
1491 type = lhs->type ();
1494 /* Tuples and tuple structs */
1495 int nfields = type->num_fields ();
1497 if (field_number >= nfields || field_number < 0)
1499 if (outer_type != NULL)
1500 error(_("Cannot access field %d of variant %s::%s, "
1501 "there are only %d fields"),
1502 field_number, outer_type->name (),
1503 rust_last_path_segment (type->name ()),
1504 nfields);
1505 else
1506 error(_("Cannot access field %d of %s, "
1507 "there are only %d fields"),
1508 field_number, type->name (), nfields);
1511 /* Tuples are tuple structs too. */
1512 if (!rust_tuple_struct_type_p (type))
1514 if (outer_type != NULL)
1515 error(_("Variant %s::%s is not a tuple variant"),
1516 outer_type->name (),
1517 rust_last_path_segment (type->name ()));
1518 else
1519 error(_("Attempting to access anonymous field %d "
1520 "of %s, which is not a tuple, tuple struct, or "
1521 "tuple-like variant"),
1522 field_number, type->name ());
1525 return lhs->primitive_field (0, field_number, type);
1527 else
1528 error(_("Anonymous field access is only allowed on tuples, \
1529 tuple structs, and tuple-like enum variants"));
1532 struct value *
1533 rust_structop::evaluate (struct type *expect_type,
1534 struct expression *exp,
1535 enum noside noside)
1537 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1538 const char *field_name = std::get<1> (m_storage).c_str ();
1540 struct value *result;
1541 struct type *type = lhs->type ();
1542 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1544 type = resolve_dynamic_type (type, lhs->contents (),
1545 lhs->address ());
1547 if (rust_empty_enum_p (type))
1548 error (_("Cannot access field %s of empty enum %s"),
1549 field_name, type->name ());
1551 int fieldno = rust_enum_variant (type);
1552 lhs = lhs->primitive_field (0, fieldno, type);
1554 struct type *outer_type = type;
1555 type = lhs->type ();
1556 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1557 error (_("Attempting to access named field %s of tuple "
1558 "variant %s::%s, which has only anonymous fields"),
1559 field_name, outer_type->name (),
1560 rust_last_path_segment (type->name ()));
1564 result = value_struct_elt (&lhs, {}, field_name,
1565 NULL, "structure");
1567 catch (const gdb_exception_error &except)
1569 error (_("Could not find field %s of struct variant %s::%s"),
1570 field_name, outer_type->name (),
1571 rust_last_path_segment (type->name ()));
1574 else
1576 if (rust_slice_type_p (type))
1577 lhs = convert_slice (lhs);
1578 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1580 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1581 result = value::zero (result->type (), result->lval ());
1582 return result;
1585 value *
1586 rust_aggregate_operation::evaluate (struct type *expect_type,
1587 struct expression *exp,
1588 enum noside noside)
1590 struct type *type = std::get<0> (m_storage);
1591 CORE_ADDR addr = 0;
1592 struct value *addrval = NULL;
1593 value *result;
1595 if (noside == EVAL_NORMAL)
1597 addrval = value_allocate_space_in_inferior (type->length ());
1598 addr = value_as_long (addrval);
1599 result = value_at_lazy (type, addr);
1602 if (std::get<1> (m_storage) != nullptr)
1604 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1605 noside);
1607 if (noside == EVAL_NORMAL)
1609 /* This isn't quite right but will do for the time
1610 being, seeing that we can't implement the Copy
1611 trait anyway. */
1612 value_assign (result, init);
1616 for (const auto &item : std::get<2> (m_storage))
1618 value *val = item.second->evaluate (nullptr, exp, noside);
1619 if (noside == EVAL_NORMAL)
1621 const char *fieldname = item.first.c_str ();
1622 value *field = value_struct_elt (&result, {}, fieldname,
1623 nullptr, "structure");
1624 value_assign (field, val);
1628 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1629 result = value::allocate (type);
1630 else
1631 result = value_at_lazy (type, addr);
1633 return result;
1636 value *
1637 rust_structop::evaluate_funcall (struct type *expect_type,
1638 struct expression *exp,
1639 enum noside noside,
1640 const std::vector<operation_up> &ops)
1642 std::vector<struct value *> args (ops.size () + 1);
1644 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1645 type in order to look up the method. */
1646 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1647 /* We don't yet implement real Deref semantics. */
1648 while (args[0]->type ()->code () == TYPE_CODE_PTR)
1649 args[0] = value_ind (args[0]);
1651 struct type *type = args[0]->type ();
1652 if ((type->code () != TYPE_CODE_STRUCT
1653 && type->code () != TYPE_CODE_UNION
1654 && type->code () != TYPE_CODE_ENUM)
1655 || rust_tuple_type_p (type))
1656 error (_("Method calls only supported on struct or enum types"));
1657 if (type->name () == NULL)
1658 error (_("Method call on nameless type"));
1660 std::string name = (std::string (type->name ()) + "::"
1661 + std::get<1> (m_storage));
1663 const struct block *block = get_selected_block (0);
1664 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1665 SEARCH_FUNCTION_DOMAIN,
1666 nullptr);
1667 if (sym.symbol == NULL)
1668 error (_("Could not find function named '%s'"), name.c_str ());
1670 struct type *fn_type = sym.symbol->type ();
1671 if (fn_type->num_fields () == 0)
1672 error (_("Function '%s' takes no arguments"), name.c_str ());
1674 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1675 args[0] = value_addr (args[0]);
1677 value *function = address_of_variable (sym.symbol, block);
1679 for (int i = 0; i < ops.size (); ++i)
1680 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1682 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1683 return value::zero (fn_type->target_type (), not_lval);
1684 return call_function_by_hand (function, NULL, args);
1691 /* See language.h. */
1693 void
1694 rust_language::language_arch_info (struct gdbarch *gdbarch,
1695 struct language_arch_info *lai) const
1697 const struct builtin_type *builtin = builtin_type (gdbarch);
1699 /* Helper function to allow shorter lines below. */
1700 auto add = [&] (struct type * t) -> struct type *
1702 lai->add_primitive_type (t);
1703 return t;
1706 type_allocator alloc (gdbarch);
1707 struct type *bool_type
1708 = add (init_boolean_type (alloc, 8, 1, "bool"));
1709 add (init_character_type (alloc, 32, 1, "char"));
1710 add (init_integer_type (alloc, 8, 0, "i8"));
1711 struct type *u8_type
1712 = add (init_integer_type (alloc, 8, 1, "u8"));
1713 add (init_integer_type (alloc, 16, 0, "i16"));
1714 add (init_integer_type (alloc, 16, 1, "u16"));
1715 add (init_integer_type (alloc, 32, 0, "i32"));
1716 add (init_integer_type (alloc, 32, 1, "u32"));
1717 add (init_integer_type (alloc, 64, 0, "i64"));
1718 add (init_integer_type (alloc, 64, 1, "u64"));
1719 add (init_integer_type (alloc, 128, 0, "i128"));
1720 add (init_integer_type (alloc, 128, 1, "u128"));
1722 unsigned int length = 8 * builtin->builtin_data_ptr->length ();
1723 add (init_integer_type (alloc, length, 0, "isize"));
1724 struct type *usize_type
1725 = add (init_integer_type (alloc, length, 1, "usize"));
1727 add (init_float_type (alloc, 32, "f32", floatformats_ieee_single));
1728 add (init_float_type (alloc, 64, "f64", floatformats_ieee_double));
1729 add (init_integer_type (alloc, 0, 1, "()"));
1731 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1732 add (rust_slice_type ("&str", tem, usize_type));
1734 lai->set_bool_type (bool_type);
1735 lai->set_string_char_type (u8_type);
1738 /* See language.h. */
1740 void
1741 rust_language::print_type (struct type *type, const char *varstring,
1742 struct ui_file *stream, int show, int level,
1743 const struct type_print_options *flags) const
1745 print_offset_data podata (flags);
1746 rust_internal_print_type (type, varstring, stream, show, level,
1747 flags, false, &podata);
1750 /* See language.h. */
1752 void
1753 rust_language::emitchar (int ch, struct type *chtype,
1754 struct ui_file *stream, int quoter) const
1756 if (!rust_chartype_p (chtype))
1757 generic_emit_char (ch, chtype, stream, quoter,
1758 target_charset (chtype->arch ()));
1759 else if (ch == '\\' || ch == quoter)
1760 gdb_printf (stream, "\\%c", ch);
1761 else if (ch == '\n')
1762 gdb_puts ("\\n", stream);
1763 else if (ch == '\r')
1764 gdb_puts ("\\r", stream);
1765 else if (ch == '\t')
1766 gdb_puts ("\\t", stream);
1767 else if (ch == '\0')
1768 gdb_puts ("\\0", stream);
1769 else if (ch >= 32 && ch <= 127 && isprint (ch))
1770 gdb_putc (ch, stream);
1771 else if (ch <= 255)
1772 gdb_printf (stream, "\\x%02x", ch);
1773 else
1774 gdb_printf (stream, "\\u{%06x}", ch);
1777 /* See language.h. */
1779 bool
1780 rust_language::is_array_like (struct type *type) const
1782 if (!rust_slice_type_p (type))
1783 return false;
1784 return rust_array_like_element_type (type) != nullptr;
1787 /* See language.h. */
1789 bool
1790 rust_language::is_string_type_p (struct type *type) const
1792 LONGEST low_bound, high_bound;
1794 type = check_typedef (type);
1795 return ((type->code () == TYPE_CODE_STRING)
1796 || (type->code () == TYPE_CODE_PTR
1797 && (type->target_type ()->code () == TYPE_CODE_ARRAY
1798 && rust_u8_type_p (type->target_type ()->target_type ())
1799 && get_array_bounds (type->target_type (), &low_bound,
1800 &high_bound)))
1801 || (type->code () == TYPE_CODE_STRUCT
1802 && !rust_enum_p (type)
1803 && rust_slice_type_p (type)
1804 && strcmp (type->name (), "&str") == 0));
1807 /* See language.h. */
1809 struct block_symbol
1810 rust_language::lookup_symbol_nonlocal
1811 (const char *name, const struct block *block,
1812 const domain_search_flags domain) const
1814 struct block_symbol result = {};
1816 const char *scope = block == nullptr ? "" : block->scope ();
1817 symbol_lookup_debug_printf
1818 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1819 name, host_address_to_string (block), scope,
1820 domain_name (domain).c_str ());
1822 /* Look up bare names in the block's scope. */
1823 std::string scopedname;
1824 if (name[cp_find_first_component (name)] == '\0')
1826 if (scope[0] != '\0')
1828 scopedname = std::string (scope) + "::" + name;
1829 name = scopedname.c_str ();
1831 else
1832 name = NULL;
1835 if (name != NULL)
1837 result = lookup_symbol_in_static_block (name, block, domain);
1838 if (result.symbol == NULL)
1839 result = lookup_global_symbol (name, block, domain);
1841 return result;
1844 /* Single instance of the Rust language class. */
1846 static rust_language rust_language_defn;