Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / rust-lang.c
blob53e7356c451e7a02ec91466dfdfa2c4f5ef30cb2
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 "gdbarch.h"
29 #include "infcall.h"
30 #include "objfiles.h"
31 #include "rust-lang.h"
32 #include "typeprint.h"
33 #include "valprint.h"
34 #include "varobj.h"
35 #include <algorithm>
36 #include <string>
37 #include <vector>
38 #include "cli/cli-style.h"
39 #include "parser-defs.h"
40 #include "rust-exp.h"
42 /* See rust-lang.h. */
44 const char *
45 rust_last_path_segment (const char *path)
47 const char *result = strrchr (path, ':');
49 if (result == NULL)
50 return path;
51 return result + 1;
54 /* See rust-lang.h. */
56 std::string
57 rust_crate_for_block (const struct block *block)
59 const char *scope = block->scope ();
61 if (scope[0] == '\0')
62 return std::string ();
64 return std::string (scope, cp_find_first_component (scope));
67 /* Return true if TYPE, which must be a struct type, represents a Rust
68 enum. */
70 static bool
71 rust_enum_p (struct type *type)
73 /* is_dynamic_type will return true if any field has a dynamic
74 attribute -- but we only want to check the top level. */
75 return TYPE_HAS_VARIANT_PARTS (type);
78 /* Return true if TYPE, which must be an already-resolved enum type,
79 has no variants. */
81 static bool
82 rust_empty_enum_p (const struct type *type)
84 return type->num_fields () == 0;
87 /* Given an already-resolved enum type and contents, find which
88 variant is active. */
90 static int
91 rust_enum_variant (struct type *type)
93 /* The active variant is simply the first non-artificial field. */
94 for (int i = 0; i < type->num_fields (); ++i)
95 if (!type->field (i).is_artificial ())
96 return i;
98 /* Perhaps we could get here by trying to print an Ada variant
99 record in Rust mode. Unlikely, but an error is safer than an
100 assert. */
101 error (_("Could not find active enum variant"));
104 /* See rust-lang.h. */
106 bool
107 rust_tuple_type_p (struct type *type)
109 /* The current implementation is a bit of a hack, but there's
110 nothing else in the debuginfo to distinguish a tuple from a
111 struct. */
112 return (type->code () == TYPE_CODE_STRUCT
113 && type->name () != NULL
114 && type->name ()[0] == '(');
117 /* Return true if all non-static fields of a structlike type are in a
118 sequence like __0, __1, __2. */
120 static bool
121 rust_underscore_fields (struct type *type)
123 int i, field_number;
125 field_number = 0;
127 if (type->code () != TYPE_CODE_STRUCT)
128 return false;
129 for (i = 0; i < type->num_fields (); ++i)
131 if (!type->field (i).is_static ())
133 char buf[20];
135 xsnprintf (buf, sizeof (buf), "__%d", field_number);
136 if (strcmp (buf, type->field (i).name ()) != 0)
137 return false;
138 field_number++;
141 return true;
144 /* See rust-lang.h. */
146 bool
147 rust_tuple_struct_type_p (struct type *type)
149 /* This is just an approximation until DWARF can represent Rust more
150 precisely. We exclude zero-length structs because they may not
151 be tuple structs, and there's no way to tell. */
152 return type->num_fields () > 0 && rust_underscore_fields (type);
155 /* Return true if TYPE is "slice-like"; false otherwise. */
157 static bool
158 rust_slice_type_p (const struct type *type)
160 if (type->code () == TYPE_CODE_STRUCT
161 && type->name () != NULL
162 && type->num_fields () == 2)
164 /* The order of fields doesn't matter. While it would be nice
165 to check for artificiality here, the Rust compiler doesn't
166 emit this information. */
167 const char *n1 = type->field (0).name ();
168 const char *n2 = type->field (1).name ();
169 return ((streq (n1, "data_ptr") && streq (n2, "length"))
170 || (streq (n2, "data_ptr") && streq (n1, "length")));
172 return false;
175 /* Return true if TYPE is a range type, otherwise false. */
177 static bool
178 rust_range_type_p (struct type *type)
180 int i;
182 if (type->code () != TYPE_CODE_STRUCT
183 || type->num_fields () > 2
184 || type->name () == NULL
185 || strstr (type->name (), "::Range") == NULL)
186 return false;
188 if (type->num_fields () == 0)
189 return true;
191 i = 0;
192 if (strcmp (type->field (0).name (), "start") == 0)
194 if (type->num_fields () == 1)
195 return true;
196 i = 1;
198 else if (type->num_fields () == 2)
200 /* First field had to be "start". */
201 return false;
204 return strcmp (type->field (i).name (), "end") == 0;
207 /* Return true if TYPE is an inclusive range type, otherwise false.
208 This is only valid for types which are already known to be range
209 types. */
211 static bool
212 rust_inclusive_range_type_p (struct type *type)
214 return (strstr (type->name (), "::RangeInclusive") != NULL
215 || strstr (type->name (), "::RangeToInclusive") != NULL);
218 /* Return true if TYPE seems to be the type "u8", otherwise false. */
220 static bool
221 rust_u8_type_p (struct type *type)
223 return (type->code () == TYPE_CODE_INT
224 && type->is_unsigned ()
225 && type->length () == 1);
228 /* Return true if TYPE is a Rust character type. */
230 static bool
231 rust_chartype_p (struct type *type)
233 return (type->code () == TYPE_CODE_CHAR
234 && type->length () == 4
235 && type->is_unsigned ());
238 /* If VALUE represents a trait object pointer, return the underlying
239 pointer with the correct (i.e., runtime) type. Otherwise, return
240 NULL. */
242 static struct value *
243 rust_get_trait_object_pointer (struct value *value)
245 struct type *type = check_typedef (value->type ());
247 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
248 return NULL;
250 /* Try to be a bit resilient if the ABI changes. */
251 int vtable_field = 0;
252 for (int i = 0; i < 2; ++i)
254 if (strcmp (type->field (i).name (), "vtable") == 0)
255 vtable_field = i;
256 else if (strcmp (type->field (i).name (), "pointer") != 0)
257 return NULL;
260 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
261 struct symbol *symbol = find_symbol_at_address (vtable);
262 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
263 return NULL;
265 struct rust_vtable_symbol *vtable_sym
266 = static_cast<struct rust_vtable_symbol *> (symbol);
267 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
268 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
271 /* Find and possibly rewrite the unsized part of a slice-like type.
273 This function has two modes. If the out parameters are both NULL,
274 it will return true if an unsized member of IN_TYPE is found.
276 If the out parameters are both non-NULL, it will do the same, but
277 will also rewrite the unsized member's type to be an array of the
278 appropriate type. BOUND is the upper bound of the new array.
280 See convert_slice to understand the different kinds of unsized type
281 and how they are represented.
283 static bool
284 rewrite_slice_type (struct type *in_type, struct type **new_type,
285 LONGEST bound, ULONGEST *additional_length)
287 if (in_type->code () != TYPE_CODE_STRUCT)
288 return false;
290 unsigned nfields = in_type->num_fields ();
291 if (nfields == 0)
292 return false;
294 struct type *rewritten;
295 const field &field = in_type->field (nfields - 1);
296 struct type *field_type = field.type ();
297 if (field.loc_kind () == FIELD_LOC_KIND_BITPOS
298 && field.loc_bitpos () == 8 * in_type->length ())
300 if (additional_length == nullptr)
301 return true;
302 rewritten = lookup_array_range_type (field_type, 0, bound);
303 *additional_length = rewritten->length ();
305 else
307 if (!rewrite_slice_type (field_type, &rewritten, bound,
308 additional_length))
309 return false;
310 if (additional_length == nullptr)
311 return true;
314 struct type *result = copy_type (in_type);
315 result->copy_fields (in_type);
316 result->field (nfields - 1).set_type (rewritten);
317 result->set_length (result->length () + *additional_length);
319 *new_type = result;
320 return true;
323 /* Convert a Rust slice to its "true" representation.
325 The Rust compiler emits slices as "fat" pointers like:
327 struct { payload *data_ptr; usize length }
329 Any sort of unsized type is emitted this way.
331 If 'payload' is a struct type, then it must be searched to see if
332 the trailing field is unsized. This has to be done recursively (as
333 in, if the final field in the struct type itself has struct type,
334 then that type must be searched). In this scenario, the unsized
335 field can be recognized because it does not contribute to the
336 type's size.
338 If 'payload' does not have a trailing unsized type, or if it is not
339 of struct type, then this slice is "array-like". In this case
340 rewriting will return an array.
342 static struct value *
343 convert_slice (struct value *val)
345 struct type *type = check_typedef (val->type ());
346 /* This must have been checked by the caller. */
347 gdb_assert (rust_slice_type_p (type));
349 struct value *len = value_struct_elt (&val, {}, "length", nullptr,
350 "slice");
351 LONGEST llen = value_as_long (len);
353 struct value *ptr = value_struct_elt (&val, {}, "data_ptr", nullptr,
354 "slice");
355 struct type *original_type = ptr->type ()->target_type ();
356 ULONGEST new_length_storage = 0;
357 struct type *new_type = nullptr;
358 if (!rewrite_slice_type (original_type, &new_type, llen - 1,
359 &new_length_storage))
360 new_type = lookup_array_range_type (original_type, 0, llen - 1);
362 struct value *result = value::allocate_lazy (new_type);
363 result->set_lval (lval_memory);
364 result->set_address (value_as_address (ptr));
365 result->fetch_lazy ();
367 return result;
370 /* If TYPE is an array-like slice, return the element type; otherwise
371 return NULL. */
372 static struct type *
373 rust_array_like_element_type (struct type *type)
375 /* Caller must check this. */
376 gdb_assert (rust_slice_type_p (type));
377 for (int i = 0; i < type->num_fields (); ++i)
379 if (strcmp (type->field (i).name (), "data_ptr") == 0)
381 struct type *base_type = type->field (i).type ()->target_type ();
382 if (rewrite_slice_type (base_type, nullptr, 0, nullptr))
383 return nullptr;
384 return base_type;
387 return nullptr;
392 /* See language.h. */
394 void
395 rust_language::printstr (struct ui_file *stream, struct type *type,
396 const gdb_byte *string, unsigned int length,
397 const char *user_encoding, int force_ellipses,
398 const struct value_print_options *options) const
400 /* Rust always uses UTF-8, but let the caller override this if need
401 be. */
402 const char *encoding = user_encoding;
403 if (user_encoding == NULL || !*user_encoding)
405 /* In Rust strings, characters are "u8". */
406 if (rust_u8_type_p (type))
407 encoding = "UTF-8";
408 else
410 /* This is probably some C string, so let's let C deal with
411 it. */
412 language_defn::printstr (stream, type, string, length,
413 user_encoding, force_ellipses,
414 options);
415 return;
419 /* This is not ideal as it doesn't use our character printer. */
420 generic_printstr (stream, type, string, length, encoding, force_ellipses,
421 '"', 0, options);
426 static const struct generic_val_print_decorations rust_decorations =
428 /* Complex isn't used in Rust, but we provide C-ish values just in
429 case. */
431 " + ",
432 " * I",
433 "true",
434 "false",
435 "()",
436 "[",
440 /* See rust-lang.h. */
442 struct value *
443 rust_slice_to_array (struct value *val)
445 val = convert_slice (val);
446 if (val->type ()->code () != TYPE_CODE_ARRAY)
447 return nullptr;
448 return val;
451 /* Helper function to print a slice. */
453 void
454 rust_language::val_print_slice
455 (struct value *val, struct ui_file *stream, int recurse,
456 const struct value_print_options *options) const
458 struct type *orig_type = check_typedef (val->type ());
460 val = convert_slice (val);
461 struct type *type = check_typedef (val->type ());
463 /* &str is handled here; but for all other slice types it is fine to
464 simply print the contents. */
465 if (orig_type->name () != nullptr
466 && strcmp (orig_type->name (), "&str") == 0)
468 LONGEST low_bound, high_bound;
469 if (get_array_bounds (type, &low_bound, &high_bound))
471 val_print_string (type->target_type (), "UTF-8",
472 val->address (), high_bound - low_bound + 1,
473 stream, options);
474 return;
478 /* Print the slice type here. This was gdb's historical behavior
479 (from before unsized types were generically handled) and helps
480 make it clear that the user is seeing a slice, not an array.
481 Only arrays must be handled as the other cases are handled by
482 value_print_inner. */
483 if (type->code () == TYPE_CODE_ARRAY)
485 type_print (orig_type, "", stream, -1);
486 gdb_printf (stream, " ");
489 value_print_inner (val, stream, recurse, options);
492 /* See rust-lang.h. */
494 void
495 rust_language::val_print_struct
496 (struct value *val, struct ui_file *stream, int recurse,
497 const struct value_print_options *options) const
499 int i;
500 int first_field;
501 struct type *type = check_typedef (val->type ());
503 if (rust_slice_type_p (type))
505 val_print_slice (val, stream, recurse, options);
506 return;
509 bool is_tuple = rust_tuple_type_p (type);
510 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
511 struct value_print_options opts;
513 if (!is_tuple)
515 if (type->name () != NULL)
516 gdb_printf (stream, "%s", type->name ());
518 if (type->num_fields () == 0)
519 return;
521 if (type->name () != NULL)
522 gdb_puts (" ", stream);
525 if (is_tuple || is_tuple_struct)
526 gdb_puts ("(", stream);
527 else
528 gdb_puts ("{", stream);
530 opts = *options;
531 opts.deref_ref = false;
533 first_field = 1;
534 for (i = 0; i < type->num_fields (); ++i)
536 if (type->field (i).is_static ())
537 continue;
539 if (!first_field)
540 gdb_puts (",", stream);
542 if (options->prettyformat)
544 gdb_puts ("\n", stream);
545 print_spaces (2 + 2 * recurse, stream);
547 else if (!first_field)
548 gdb_puts (" ", stream);
550 first_field = 0;
552 if (!is_tuple && !is_tuple_struct)
554 fputs_styled (type->field (i).name (),
555 variable_name_style.style (), stream);
556 gdb_puts (": ", stream);
559 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
560 this);
563 if (options->prettyformat)
565 gdb_puts ("\n", stream);
566 print_spaces (2 * recurse, stream);
569 if (is_tuple || is_tuple_struct)
570 gdb_puts (")", stream);
571 else
572 gdb_puts ("}", stream);
575 /* See rust-lang.h. */
577 void
578 rust_language::print_enum (struct value *val, struct ui_file *stream,
579 int recurse,
580 const struct value_print_options *options) const
582 struct value_print_options opts = *options;
583 struct type *type = check_typedef (val->type ());
585 opts.deref_ref = false;
587 gdb_assert (rust_enum_p (type));
588 gdb::array_view<const gdb_byte> view
589 (val->contents_for_printing ().data (),
590 val->type ()->length ());
591 type = resolve_dynamic_type (type, view, val->address ());
593 if (rust_empty_enum_p (type))
595 /* Print the enum type name here to be more clear. */
596 gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
597 type->name (),
598 metadata_style.style ().ptr (), nullptr);
599 return;
602 int variant_fieldno = rust_enum_variant (type);
603 val = val->primitive_field (0, variant_fieldno, type);
604 struct type *variant_type = type->field (variant_fieldno).type ();
606 int nfields = variant_type->num_fields ();
608 bool is_tuple = rust_tuple_struct_type_p (variant_type);
610 gdb_printf (stream, "%s", variant_type->name ());
611 if (nfields == 0)
613 /* In case of a nullary variant like 'None', just output
614 the name. */
615 return;
618 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
619 if (is_tuple)
620 gdb_printf (stream, "(");
621 else
623 /* struct variant. */
624 gdb_printf (stream, "{");
627 bool first_field = true;
628 for (int j = 0; j < nfields; j++)
630 if (!first_field)
631 gdb_puts (", ", stream);
632 first_field = false;
634 if (!is_tuple)
635 gdb_printf (stream, "%ps: ",
636 styled_string (variable_name_style.style (),
637 variant_type->field (j).name ()));
639 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
640 this);
643 if (is_tuple)
644 gdb_puts (")", stream);
645 else
646 gdb_puts ("}", stream);
649 /* See language.h. */
651 void
652 rust_language::value_print_inner
653 (struct value *val, struct ui_file *stream, int recurse,
654 const struct value_print_options *options) const
656 struct value_print_options opts = *options;
657 opts.deref_ref = true;
659 if (opts.prettyformat == Val_prettyformat_default)
660 opts.prettyformat = (opts.prettyformat_structs
661 ? Val_prettyformat : Val_no_prettyformat);
663 struct type *type = check_typedef (val->type ());
664 switch (type->code ())
666 case TYPE_CODE_PTR:
668 LONGEST low_bound, high_bound;
670 if (type->target_type ()->code () == TYPE_CODE_ARRAY
671 && rust_u8_type_p (type->target_type ()->target_type ())
672 && get_array_bounds (type->target_type (), &low_bound,
673 &high_bound))
675 /* We have a pointer to a byte string, so just print
676 that. */
677 struct type *elttype = check_typedef (type->target_type ());
678 CORE_ADDR addr = value_as_address (val);
679 struct gdbarch *arch = type->arch ();
681 if (opts.addressprint)
683 gdb_puts (paddress (arch, addr), stream);
684 gdb_puts (" ", stream);
687 gdb_puts ("b", stream);
688 val_print_string (elttype->target_type (), "ASCII", addr,
689 high_bound - low_bound + 1, stream,
690 &opts);
691 break;
694 goto generic_print;
696 case TYPE_CODE_INT:
697 /* Recognize the unit type. */
698 if (type->is_unsigned () && type->length () == 0
699 && type->name () != NULL && strcmp (type->name (), "()") == 0)
701 gdb_puts ("()", stream);
702 break;
704 goto generic_print;
706 case TYPE_CODE_STRING:
708 LONGEST low_bound, high_bound;
710 if (!get_array_bounds (type, &low_bound, &high_bound))
711 error (_("Could not determine the array bounds"));
713 /* If we see a plain TYPE_CODE_STRING, then we're printing a
714 byte string, hence the choice of "ASCII" as the
715 encoding. */
716 gdb_puts ("b", stream);
717 printstr (stream, type->target_type (),
718 val->contents_for_printing ().data (),
719 high_bound - low_bound + 1, "ASCII", 0, &opts);
721 break;
723 case TYPE_CODE_ARRAY:
725 LONGEST low_bound, high_bound;
727 if (get_array_bounds (type, &low_bound, &high_bound)
728 && high_bound - low_bound + 1 == 0)
729 gdb_puts ("[]", stream);
730 else
731 goto generic_print;
733 break;
735 case TYPE_CODE_UNION:
736 /* Untagged unions are printed as if they are structs. Since
737 the field bit positions overlap in the debuginfo, the code
738 for printing a union is same as that for a struct, the only
739 difference is that the input type will have overlapping
740 fields. */
741 val_print_struct (val, stream, recurse, &opts);
742 break;
744 case TYPE_CODE_STRUCT:
745 if (rust_enum_p (type))
746 print_enum (val, stream, recurse, &opts);
747 else
748 val_print_struct (val, stream, recurse, &opts);
749 break;
751 default:
752 generic_print:
753 /* Nothing special yet. */
754 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
758 /* See language.h. */
760 void
761 rust_language::value_print
762 (struct value *val, struct ui_file *stream,
763 const struct value_print_options *options) const
765 value_print_options opts = *options;
766 opts.deref_ref = true;
768 struct type *type = check_typedef (val->type ());
769 if (type->is_pointer_or_reference ())
771 gdb_printf (stream, "(");
772 type_print (val->type (), "", stream, -1);
773 gdb_printf (stream, ") ");
776 return common_val_print (val, stream, 0, &opts, this);
781 static void
782 rust_internal_print_type (struct type *type, const char *varstring,
783 struct ui_file *stream, int show, int level,
784 const struct type_print_options *flags,
785 bool for_rust_enum, print_offset_data *podata);
787 /* Print a struct or union typedef. */
788 static void
789 rust_print_struct_def (struct type *type, const char *varstring,
790 struct ui_file *stream, int show, int level,
791 const struct type_print_options *flags,
792 bool for_rust_enum, print_offset_data *podata)
794 /* Print a tuple type simply. */
795 if (rust_tuple_type_p (type))
797 gdb_puts (type->name (), stream);
798 return;
801 /* If we see a base class, delegate to C. */
802 if (TYPE_N_BASECLASSES (type) > 0)
803 c_print_type (type, varstring, stream, show, level, language_rust, flags);
805 if (flags->print_offsets)
807 /* Temporarily bump the level so that the output lines up
808 correctly. */
809 level += 2;
812 /* Compute properties of TYPE here because, in the enum case, the
813 rest of the code ends up looking only at the variant part. */
814 const char *tagname = type->name ();
815 bool is_tuple_struct = rust_tuple_struct_type_p (type);
816 bool is_tuple = rust_tuple_type_p (type);
817 bool is_enum = rust_enum_p (type);
819 if (for_rust_enum)
821 /* Already printing an outer enum, so nothing to print here. */
823 else
825 /* This code path is also used by unions and enums. */
826 if (is_enum)
828 gdb_puts ("enum ", stream);
829 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
830 if (prop != nullptr && prop->kind () == PROP_TYPE)
831 type = prop->original_type ();
833 else if (type->code () == TYPE_CODE_STRUCT)
834 gdb_puts ("struct ", stream);
835 else
836 gdb_puts ("union ", stream);
838 if (tagname != NULL)
839 gdb_puts (tagname, stream);
842 if (type->num_fields () == 0 && !is_tuple)
843 return;
844 if (for_rust_enum && !flags->print_offsets)
845 gdb_puts (is_tuple_struct ? "(" : "{", stream);
846 else
847 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
849 /* When printing offsets, we rearrange the fields into storage
850 order. This lets us show holes more clearly. We work using
851 field indices here because it simplifies calls to
852 print_offset_data::update below. */
853 std::vector<int> fields;
854 for (int i = 0; i < type->num_fields (); ++i)
856 if (type->field (i).is_static ())
857 continue;
858 if (is_enum && type->field (i).is_artificial ())
859 continue;
860 fields.push_back (i);
862 if (flags->print_offsets)
863 std::sort (fields.begin (), fields.end (),
864 [&] (int a, int b)
866 return (type->field (a).loc_bitpos ()
867 < type->field (b).loc_bitpos ());
870 for (int i : fields)
872 QUIT;
874 gdb_assert (!type->field (i).is_static ());
875 gdb_assert (! (is_enum && type->field (i).is_artificial ()));
877 if (flags->print_offsets)
878 podata->update (type, i, stream);
880 /* We'd like to print "pub" here as needed, but rustc
881 doesn't emit the debuginfo, and our types don't have
882 cplus_struct_type attached. */
884 /* For a tuple struct we print the type but nothing
885 else. */
886 if (!for_rust_enum || flags->print_offsets)
887 print_spaces (level + 2, stream);
888 if (is_enum)
889 fputs_styled (type->field (i).name (), variable_name_style.style (),
890 stream);
891 else if (!is_tuple_struct)
892 gdb_printf (stream, "%ps: ",
893 styled_string (variable_name_style.style (),
894 type->field (i).name ()));
896 rust_internal_print_type (type->field (i).type (), NULL,
897 stream, (is_enum ? show : show - 1),
898 level + 2, flags, is_enum, podata);
899 if (!for_rust_enum || flags->print_offsets)
900 gdb_puts (",\n", stream);
901 /* Note that this check of "I" is ok because we only sorted the
902 fields by offset when print_offsets was set, so we won't take
903 this branch in that case. */
904 else if (i + 1 < type->num_fields ())
905 gdb_puts (", ", stream);
908 if (flags->print_offsets)
910 /* Undo the temporary level increase we did above. */
911 level -= 2;
912 podata->finish (type, level, stream);
913 print_spaces (print_offset_data::indentation, stream);
914 if (level == 0)
915 print_spaces (2, stream);
917 if (!for_rust_enum || flags->print_offsets)
918 print_spaces (level, stream);
919 gdb_puts (is_tuple_struct ? ")" : "}", stream);
922 /* la_print_type implementation for Rust. */
924 static void
925 rust_internal_print_type (struct type *type, const char *varstring,
926 struct ui_file *stream, int show, int level,
927 const struct type_print_options *flags,
928 bool for_rust_enum, print_offset_data *podata)
930 QUIT;
931 if (show <= 0
932 && type->name () != NULL)
934 /* Rust calls the unit type "void" in its debuginfo,
935 but we don't want to print it as that. */
936 if (type->code () == TYPE_CODE_VOID)
937 gdb_puts ("()", stream);
938 else
939 gdb_puts (type->name (), stream);
940 return;
943 type = check_typedef (type);
944 switch (type->code ())
946 case TYPE_CODE_VOID:
947 /* If we have an enum, we've already printed the type's
948 unqualified name, and there is nothing else to print
949 here. */
950 if (!for_rust_enum)
951 gdb_puts ("()", stream);
952 break;
954 case TYPE_CODE_FUNC:
955 /* Delegate varargs to the C printer. */
956 if (type->has_varargs ())
957 goto c_printer;
959 gdb_puts ("fn ", stream);
960 if (varstring != NULL)
961 gdb_puts (varstring, stream);
962 gdb_puts ("(", stream);
963 for (int i = 0; i < type->num_fields (); ++i)
965 QUIT;
966 if (i > 0)
967 gdb_puts (", ", stream);
968 rust_internal_print_type (type->field (i).type (), "", stream,
969 -1, 0, flags, false, podata);
971 gdb_puts (")", stream);
972 /* If it returns unit, we can omit the return type. */
973 if (type->target_type ()->code () != TYPE_CODE_VOID)
975 gdb_puts (" -> ", stream);
976 rust_internal_print_type (type->target_type (), "", stream,
977 -1, 0, flags, false, podata);
979 break;
981 case TYPE_CODE_ARRAY:
983 LONGEST low_bound, high_bound;
985 gdb_puts ("[", stream);
986 rust_internal_print_type (type->target_type (), NULL,
987 stream, show - 1, level, flags, false,
988 podata);
990 if (type->bounds ()->high.kind () == PROP_LOCEXPR
991 || type->bounds ()->high.kind () == PROP_LOCLIST)
992 gdb_printf (stream, "; variable length");
993 else if (get_array_bounds (type, &low_bound, &high_bound))
994 gdb_printf (stream, "; %s",
995 plongest (high_bound - low_bound + 1));
996 gdb_puts ("]", stream);
998 break;
1000 case TYPE_CODE_UNION:
1001 case TYPE_CODE_STRUCT:
1002 rust_print_struct_def (type, varstring, stream, show, level, flags,
1003 for_rust_enum, podata);
1004 break;
1006 case TYPE_CODE_ENUM:
1008 int len = 0;
1010 gdb_puts ("enum ", stream);
1011 if (type->name () != NULL)
1013 gdb_puts (type->name (), stream);
1014 gdb_puts (" ", stream);
1015 len = strlen (type->name ());
1017 gdb_puts ("{\n", stream);
1019 for (int i = 0; i < type->num_fields (); ++i)
1021 const char *name = type->field (i).name ();
1023 QUIT;
1025 if (len > 0
1026 && strncmp (name, type->name (), len) == 0
1027 && name[len] == ':'
1028 && name[len + 1] == ':')
1029 name += len + 2;
1030 gdb_printf (stream, "%*s%ps,\n",
1031 level + 2, "",
1032 styled_string (variable_name_style.style (),
1033 name));
1036 gdb_puts ("}", stream);
1038 break;
1040 case TYPE_CODE_PTR:
1042 if (type->name () != nullptr)
1043 gdb_puts (type->name (), stream);
1044 else
1046 /* We currently can't distinguish between pointers and
1047 references. */
1048 gdb_puts ("*mut ", stream);
1049 type_print (type->target_type (), "", stream, 0);
1052 break;
1054 default:
1055 c_printer:
1056 c_print_type (type, varstring, stream, show, level, language_rust,
1057 flags);
1063 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1064 -- either on an obstack or on a gdbarch. */
1066 static struct type *
1067 rust_composite_type (struct type *original,
1068 const char *name,
1069 const char *field1, struct type *type1,
1070 const char *field2, struct type *type2)
1072 struct type *result = type_allocator (original).new_type ();
1073 int i, nfields, bitpos;
1075 nfields = 0;
1076 if (field1 != NULL)
1077 ++nfields;
1078 if (field2 != NULL)
1079 ++nfields;
1081 result->set_code (TYPE_CODE_STRUCT);
1082 result->set_name (name);
1084 result->alloc_fields (nfields);
1086 i = 0;
1087 bitpos = 0;
1088 if (field1 != NULL)
1090 struct field *field = &result->field (i);
1092 field->set_loc_bitpos (bitpos);
1093 bitpos += type1->length () * TARGET_CHAR_BIT;
1095 field->set_name (field1);
1096 field->set_type (type1);
1097 ++i;
1099 if (field2 != NULL)
1101 struct field *field = &result->field (i);
1102 unsigned align = type_align (type2);
1104 if (align != 0)
1106 int delta;
1108 align *= TARGET_CHAR_BIT;
1109 delta = bitpos % align;
1110 if (delta != 0)
1111 bitpos += align - delta;
1113 field->set_loc_bitpos (bitpos);
1115 field->set_name (field2);
1116 field->set_type (type2);
1117 ++i;
1120 if (i > 0)
1121 result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT
1122 + result->field (i - 1).type ()->length ());
1123 return result;
1126 /* See rust-lang.h. */
1128 struct type *
1129 rust_slice_type (const char *name, struct type *elt_type,
1130 struct type *usize_type)
1132 struct type *type;
1134 elt_type = lookup_pointer_type (elt_type);
1135 type = rust_composite_type (elt_type, name,
1136 "data_ptr", elt_type,
1137 "length", usize_type);
1139 return type;
1144 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1146 struct value *
1147 rust_range (struct type *expect_type, struct expression *exp,
1148 enum noside noside, enum range_flag kind,
1149 struct value *low, struct value *high)
1151 struct value *addrval, *result;
1152 CORE_ADDR addr;
1153 struct type *range_type;
1154 struct type *index_type;
1155 struct type *temp_type;
1156 const char *name;
1158 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
1160 if (low == NULL)
1162 if (high == NULL)
1164 index_type = NULL;
1165 name = "std::ops::RangeFull";
1167 else
1169 index_type = high->type ();
1170 name = (inclusive
1171 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1174 else
1176 if (high == NULL)
1178 index_type = low->type ();
1179 name = "std::ops::RangeFrom";
1181 else
1183 if (!types_equal (low->type (), high->type ()))
1184 error (_("Range expression with different types"));
1185 index_type = low->type ();
1186 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1190 /* If we don't have an index type, just allocate this on the
1191 arch. Here any type will do. */
1192 temp_type = (index_type == NULL
1193 ? language_bool_type (exp->language_defn, exp->gdbarch)
1194 : index_type);
1195 /* It would be nicer to cache the range type. */
1196 range_type = rust_composite_type (temp_type, name,
1197 low == NULL ? NULL : "start", index_type,
1198 high == NULL ? NULL : "end", index_type);
1200 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1201 return value::zero (range_type, lval_memory);
1203 addrval = value_allocate_space_in_inferior (range_type->length ());
1204 addr = value_as_long (addrval);
1205 result = value_at_lazy (range_type, addr);
1207 if (low != NULL)
1209 struct value *start = value_struct_elt (&result, {}, "start", NULL,
1210 "range");
1212 value_assign (start, low);
1215 if (high != NULL)
1217 struct value *end = value_struct_elt (&result, {}, "end", NULL,
1218 "range");
1220 value_assign (end, high);
1223 result = value_at_lazy (range_type, addr);
1224 return result;
1227 /* A helper function to compute the range and kind given a range
1228 value. TYPE is the type of the range value. RANGE is the range
1229 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1230 parameters might be filled in, or might not be, depending on the
1231 kind of range this is. KIND will always be set to the appropriate
1232 value describing the kind of range, and this can be used to
1233 determine whether LOW or HIGH are valid. */
1235 static void
1236 rust_compute_range (struct type *type, struct value *range,
1237 LONGEST *low, LONGEST *high,
1238 range_flags *kind)
1240 int i;
1242 *low = 0;
1243 *high = 0;
1244 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1246 if (type->num_fields () == 0)
1247 return;
1249 i = 0;
1250 if (strcmp (type->field (0).name (), "start") == 0)
1252 *kind = RANGE_HIGH_BOUND_DEFAULT;
1253 *low = value_as_long (value_field (range, 0));
1254 ++i;
1256 if (type->num_fields () > i
1257 && strcmp (type->field (i).name (), "end") == 0)
1259 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1260 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
1261 *high = value_as_long (value_field (range, i));
1263 if (rust_inclusive_range_type_p (type))
1264 ++*high;
1268 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1270 struct value *
1271 rust_subscript (struct type *expect_type, struct expression *exp,
1272 enum noside noside, bool for_addr,
1273 struct value *lhs, struct value *rhs)
1275 struct value *result;
1276 struct type *rhstype;
1277 LONGEST low, high_bound;
1278 /* Initialized to appease the compiler. */
1279 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1280 LONGEST high = 0;
1281 int want_slice = 0;
1283 rhstype = check_typedef (rhs->type ());
1284 if (rust_range_type_p (rhstype))
1286 if (!for_addr)
1287 error (_("Can't take slice of array without '&'"));
1288 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1289 want_slice = 1;
1291 else
1292 low = value_as_long (rhs);
1294 struct type *type = check_typedef (lhs->type ());
1295 struct type *orig_type = type;
1296 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1298 struct type *base_type = nullptr;
1299 if (type->code () == TYPE_CODE_ARRAY)
1300 base_type = type->target_type ();
1301 else if (rust_slice_type_p (type))
1303 base_type = rust_array_like_element_type (type);
1304 if (base_type == nullptr)
1305 error (_("Cannot subscript non-array-like slice"));
1307 else if (type->code () == TYPE_CODE_PTR)
1308 base_type = type->target_type ();
1309 else
1310 error (_("Cannot subscript non-array type"));
1312 struct type *new_type;
1313 if (want_slice)
1315 if (rust_slice_type_p (type))
1316 new_type = type;
1317 else
1319 struct type *usize
1320 = language_lookup_primitive_type (exp->language_defn,
1321 exp->gdbarch,
1322 "usize");
1323 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1326 else
1327 new_type = base_type;
1329 return value::zero (new_type, lhs->lval ());
1331 else
1333 LONGEST low_bound;
1334 struct value *base;
1336 if (rust_slice_type_p (type))
1338 lhs = convert_slice (lhs);
1339 type = check_typedef (lhs->type ());
1342 if (type->code () == TYPE_CODE_ARRAY)
1344 base = lhs;
1345 if (!get_array_bounds (type, &low_bound, &high_bound))
1346 error (_("Can't compute array bounds"));
1347 if (low_bound != 0)
1348 error (_("Found array with non-zero lower bound"));
1349 ++high_bound;
1351 else if (type->code () == TYPE_CODE_PTR)
1353 base = lhs;
1354 low_bound = 0;
1355 high_bound = LONGEST_MAX;
1357 else
1358 error (_("Cannot subscript non-array type"));
1360 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
1361 low = low_bound;
1362 if (low < 0)
1363 error (_("Index less than zero"));
1364 if (low > high_bound)
1365 error (_("Index greater than length"));
1367 result = value_subscript (base, low);
1370 if (for_addr)
1372 if (want_slice)
1374 struct type *usize, *slice;
1375 CORE_ADDR addr;
1376 struct value *addrval, *tem;
1378 if (kind & RANGE_HIGH_BOUND_DEFAULT)
1379 high = high_bound;
1380 if (high < 0)
1381 error (_("High index less than zero"));
1382 if (low > high)
1383 error (_("Low index greater than high index"));
1384 if (high > high_bound)
1385 error (_("High index greater than length"));
1387 usize = language_lookup_primitive_type (exp->language_defn,
1388 exp->gdbarch,
1389 "usize");
1390 /* Preserve the name for slice-of-slice; this lets
1391 string-printing work a bit more nicely. */
1392 const char *new_name = ((orig_type != nullptr
1393 && rust_slice_type_p (orig_type))
1394 ? orig_type->name () : "&[*gdb*]");
1396 slice = rust_slice_type (new_name, result->type (), usize);
1398 addrval = value_allocate_space_in_inferior (slice->length ());
1399 addr = value_as_long (addrval);
1400 tem = value_at_lazy (slice, addr);
1402 value_assign (value_field (tem, 0), value_addr (result));
1403 value_assign (value_field (tem, 1),
1404 value_from_longest (usize, high - low));
1406 result = value_at_lazy (slice, addr);
1408 else
1409 result = value_addr (result);
1412 return result;
1415 namespace expr
1418 struct value *
1419 rust_unop_ind_operation::evaluate (struct type *expect_type,
1420 struct expression *exp,
1421 enum noside noside)
1423 if (noside != EVAL_NORMAL)
1424 return unop_ind_operation::evaluate (expect_type, exp, noside);
1426 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1427 noside);
1428 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1429 if (trait_ptr != NULL)
1430 value = trait_ptr;
1432 return value_ind (value);
1435 } /* namespace expr */
1437 /* A helper function for UNOP_COMPLEMENT. */
1439 struct value *
1440 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1441 enum noside noside,
1442 enum exp_opcode opcode,
1443 struct value *value)
1445 if (value->type ()->code () == TYPE_CODE_BOOL)
1446 return value_from_longest (value->type (), value_logical_not (value));
1447 return value_complement (value);
1450 /* A helper function for OP_ARRAY. */
1452 struct value *
1453 eval_op_rust_array (struct type *expect_type, struct expression *exp,
1454 enum noside noside,
1455 enum exp_opcode opcode,
1456 struct value *elt, struct value *ncopies)
1458 int copies = value_as_long (ncopies);
1459 if (copies < 0)
1460 error (_("Array with negative number of elements"));
1462 if (noside == EVAL_NORMAL)
1463 return value_array (0, std::vector<value *> (copies, elt));
1464 else
1466 struct type *arraytype
1467 = lookup_array_range_type (elt->type (), 0, copies - 1);
1468 return value::allocate (arraytype);
1472 namespace expr
1475 struct value *
1476 rust_struct_anon::evaluate (struct type *expect_type,
1477 struct expression *exp,
1478 enum noside noside)
1480 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1481 int field_number = std::get<0> (m_storage);
1483 struct type *type = lhs->type ();
1485 if (type->code () == TYPE_CODE_STRUCT)
1487 struct type *outer_type = NULL;
1489 if (rust_enum_p (type))
1491 type = resolve_dynamic_type (type, lhs->contents (),
1492 lhs->address ());
1494 if (rust_empty_enum_p (type))
1495 error (_("Cannot access field %d of empty enum %s"),
1496 field_number, type->name ());
1498 int fieldno = rust_enum_variant (type);
1499 lhs = lhs->primitive_field (0, fieldno, type);
1500 outer_type = type;
1501 type = lhs->type ();
1504 /* Tuples and tuple structs */
1505 int nfields = type->num_fields ();
1507 if (field_number >= nfields || field_number < 0)
1509 if (outer_type != NULL)
1510 error(_("Cannot access field %d of variant %s::%s, "
1511 "there are only %d fields"),
1512 field_number, outer_type->name (),
1513 rust_last_path_segment (type->name ()),
1514 nfields);
1515 else
1516 error(_("Cannot access field %d of %s, "
1517 "there are only %d fields"),
1518 field_number, type->name (), nfields);
1521 /* Tuples are tuple structs too. */
1522 if (!rust_tuple_struct_type_p (type))
1524 if (outer_type != NULL)
1525 error(_("Variant %s::%s is not a tuple variant"),
1526 outer_type->name (),
1527 rust_last_path_segment (type->name ()));
1528 else
1529 error(_("Attempting to access anonymous field %d "
1530 "of %s, which is not a tuple, tuple struct, or "
1531 "tuple-like variant"),
1532 field_number, type->name ());
1535 return lhs->primitive_field (0, field_number, type);
1537 else
1538 error(_("Anonymous field access is only allowed on tuples, \
1539 tuple structs, and tuple-like enum variants"));
1542 struct value *
1543 rust_structop::evaluate (struct type *expect_type,
1544 struct expression *exp,
1545 enum noside noside)
1547 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1548 const char *field_name = std::get<1> (m_storage).c_str ();
1550 struct value *result;
1551 struct type *type = lhs->type ();
1552 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1554 type = resolve_dynamic_type (type, lhs->contents (),
1555 lhs->address ());
1557 if (rust_empty_enum_p (type))
1558 error (_("Cannot access field %s of empty enum %s"),
1559 field_name, type->name ());
1561 int fieldno = rust_enum_variant (type);
1562 lhs = lhs->primitive_field (0, fieldno, type);
1564 struct type *outer_type = type;
1565 type = lhs->type ();
1566 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1567 error (_("Attempting to access named field %s of tuple "
1568 "variant %s::%s, which has only anonymous fields"),
1569 field_name, outer_type->name (),
1570 rust_last_path_segment (type->name ()));
1574 result = value_struct_elt (&lhs, {}, field_name,
1575 NULL, "structure");
1577 catch (const gdb_exception_error &except)
1579 error (_("Could not find field %s of struct variant %s::%s"),
1580 field_name, outer_type->name (),
1581 rust_last_path_segment (type->name ()));
1584 else
1586 if (rust_slice_type_p (type))
1587 lhs = convert_slice (lhs);
1588 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1590 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1591 result = value::zero (result->type (), result->lval ());
1592 return result;
1595 value *
1596 rust_aggregate_operation::evaluate (struct type *expect_type,
1597 struct expression *exp,
1598 enum noside noside)
1600 struct type *type = std::get<0> (m_storage);
1601 CORE_ADDR addr = 0;
1602 struct value *addrval = NULL;
1603 value *result;
1605 if (noside == EVAL_NORMAL)
1607 addrval = value_allocate_space_in_inferior (type->length ());
1608 addr = value_as_long (addrval);
1609 result = value_at_lazy (type, addr);
1612 if (std::get<1> (m_storage) != nullptr)
1614 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1615 noside);
1617 if (noside == EVAL_NORMAL)
1619 /* This isn't quite right but will do for the time
1620 being, seeing that we can't implement the Copy
1621 trait anyway. */
1622 value_assign (result, init);
1626 for (const auto &item : std::get<2> (m_storage))
1628 value *val = item.second->evaluate (nullptr, exp, noside);
1629 if (noside == EVAL_NORMAL)
1631 const char *fieldname = item.first.c_str ();
1632 value *field = value_struct_elt (&result, {}, fieldname,
1633 nullptr, "structure");
1634 value_assign (field, val);
1638 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1639 result = value::allocate (type);
1640 else
1641 result = value_at_lazy (type, addr);
1643 return result;
1646 value *
1647 rust_structop::evaluate_funcall (struct type *expect_type,
1648 struct expression *exp,
1649 enum noside noside,
1650 const std::vector<operation_up> &ops)
1652 std::vector<struct value *> args (ops.size () + 1);
1654 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1655 type in order to look up the method. */
1656 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1657 /* We don't yet implement real Deref semantics. */
1658 while (args[0]->type ()->code () == TYPE_CODE_PTR)
1659 args[0] = value_ind (args[0]);
1661 struct type *type = args[0]->type ();
1662 if ((type->code () != TYPE_CODE_STRUCT
1663 && type->code () != TYPE_CODE_UNION
1664 && type->code () != TYPE_CODE_ENUM)
1665 || rust_tuple_type_p (type))
1666 error (_("Method calls only supported on struct or enum types"));
1667 if (type->name () == NULL)
1668 error (_("Method call on nameless type"));
1670 std::string name = (std::string (type->name ()) + "::"
1671 + std::get<1> (m_storage));
1673 const struct block *block = get_selected_block (0);
1674 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1675 SEARCH_FUNCTION_DOMAIN,
1676 nullptr);
1677 if (sym.symbol == NULL)
1678 error (_("Could not find function named '%s'"), name.c_str ());
1680 struct type *fn_type = sym.symbol->type ();
1681 if (fn_type->num_fields () == 0)
1682 error (_("Function '%s' takes no arguments"), name.c_str ());
1684 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1685 args[0] = value_addr (args[0]);
1687 value *function = address_of_variable (sym.symbol, block);
1689 for (int i = 0; i < ops.size (); ++i)
1690 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1692 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1693 return value::zero (fn_type->target_type (), not_lval);
1694 return call_function_by_hand (function, NULL, args);
1701 /* See language.h. */
1703 void
1704 rust_language::language_arch_info (struct gdbarch *gdbarch,
1705 struct language_arch_info *lai) const
1707 const struct builtin_type *builtin = builtin_type (gdbarch);
1709 /* Helper function to allow shorter lines below. */
1710 auto add = [&] (struct type * t) -> struct type *
1712 lai->add_primitive_type (t);
1713 return t;
1716 type_allocator alloc (gdbarch);
1717 struct type *bool_type
1718 = add (init_boolean_type (alloc, 8, 1, "bool"));
1719 add (init_character_type (alloc, 32, 1, "char"));
1720 add (init_integer_type (alloc, 8, 0, "i8"));
1721 struct type *u8_type
1722 = add (init_integer_type (alloc, 8, 1, "u8"));
1723 add (init_integer_type (alloc, 16, 0, "i16"));
1724 add (init_integer_type (alloc, 16, 1, "u16"));
1725 add (init_integer_type (alloc, 32, 0, "i32"));
1726 add (init_integer_type (alloc, 32, 1, "u32"));
1727 add (init_integer_type (alloc, 64, 0, "i64"));
1728 add (init_integer_type (alloc, 64, 1, "u64"));
1729 add (init_integer_type (alloc, 128, 0, "i128"));
1730 add (init_integer_type (alloc, 128, 1, "u128"));
1732 unsigned int length = 8 * builtin->builtin_data_ptr->length ();
1733 add (init_integer_type (alloc, length, 0, "isize"));
1734 struct type *usize_type
1735 = add (init_integer_type (alloc, length, 1, "usize"));
1737 add (init_float_type (alloc, 32, "f32", floatformats_ieee_single));
1738 add (init_float_type (alloc, 64, "f64", floatformats_ieee_double));
1739 add (init_integer_type (alloc, 0, 1, "()"));
1741 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1742 add (rust_slice_type ("&str", tem, usize_type));
1744 lai->set_bool_type (bool_type);
1745 lai->set_string_char_type (u8_type);
1748 /* See language.h. */
1750 void
1751 rust_language::print_type (struct type *type, const char *varstring,
1752 struct ui_file *stream, int show, int level,
1753 const struct type_print_options *flags) const
1755 print_offset_data podata (flags);
1756 rust_internal_print_type (type, varstring, stream, show, level,
1757 flags, false, &podata);
1760 /* See language.h. */
1762 void
1763 rust_language::emitchar (int ch, struct type *chtype,
1764 struct ui_file *stream, int quoter) const
1766 if (!rust_chartype_p (chtype))
1767 generic_emit_char (ch, chtype, stream, quoter,
1768 target_charset (chtype->arch ()));
1769 else if (ch == '\\' || ch == quoter)
1770 gdb_printf (stream, "\\%c", ch);
1771 else if (ch == '\n')
1772 gdb_puts ("\\n", stream);
1773 else if (ch == '\r')
1774 gdb_puts ("\\r", stream);
1775 else if (ch == '\t')
1776 gdb_puts ("\\t", stream);
1777 else if (ch == '\0')
1778 gdb_puts ("\\0", stream);
1779 else if (ch >= 32 && ch <= 127 && isprint (ch))
1780 gdb_putc (ch, stream);
1781 else if (ch <= 255)
1782 gdb_printf (stream, "\\x%02x", ch);
1783 else
1784 gdb_printf (stream, "\\u{%06x}", ch);
1787 /* See language.h. */
1789 bool
1790 rust_language::is_array_like (struct type *type) const
1792 if (!rust_slice_type_p (type))
1793 return false;
1794 return rust_array_like_element_type (type) != nullptr;
1797 /* See language.h. */
1799 bool
1800 rust_language::is_string_type_p (struct type *type) const
1802 LONGEST low_bound, high_bound;
1804 type = check_typedef (type);
1805 return ((type->code () == TYPE_CODE_STRING)
1806 || (type->code () == TYPE_CODE_PTR
1807 && (type->target_type ()->code () == TYPE_CODE_ARRAY
1808 && rust_u8_type_p (type->target_type ()->target_type ())
1809 && get_array_bounds (type->target_type (), &low_bound,
1810 &high_bound)))
1811 || (type->code () == TYPE_CODE_STRUCT
1812 && !rust_enum_p (type)
1813 && rust_slice_type_p (type)
1814 && strcmp (type->name (), "&str") == 0));
1817 /* See language.h. */
1819 struct block_symbol
1820 rust_language::lookup_symbol_nonlocal
1821 (const char *name, const struct block *block,
1822 const domain_search_flags domain) const
1824 struct block_symbol result = {};
1826 const char *scope = block == nullptr ? "" : block->scope ();
1827 symbol_lookup_debug_printf
1828 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1829 name, host_address_to_string (block), scope,
1830 domain_name (domain).c_str ());
1832 /* Look up bare names in the block's scope. */
1833 std::string scopedname;
1834 if (name[cp_find_first_component (name)] == '\0')
1836 if (scope[0] != '\0')
1838 scopedname = std::string (scope) + "::" + name;
1839 name = scopedname.c_str ();
1841 else
1842 name = NULL;
1845 if (name != NULL)
1847 result = lookup_symbol_in_static_block (name, block, domain);
1848 if (result.symbol == NULL)
1849 result = lookup_global_symbol (name, block, domain);
1851 return result;
1854 /* Single instance of the Rust language class. */
1856 static rust_language rust_language_defn;