rust-lang.c revision 1.1.1.7 1 /* Rust language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20
21 #include <ctype.h>
22
23 #include "block.h"
24 #include "c-lang.h"
25 #include "charset.h"
26 #include "cp-support.h"
27 #include "demangle.h"
28 #include "event-top.h"
29 #include "gdbarch.h"
30 #include "infcall.h"
31 #include "objfiles.h"
32 #include "rust-lang.h"
33 #include "typeprint.h"
34 #include "valprint.h"
35 #include "varobj.h"
36 #include <algorithm>
37 #include <string>
38 #include <vector>
39 #include "cli/cli-style.h"
40 #include "parser-defs.h"
41 #include "rust-exp.h"
42
43 /* See rust-lang.h. */
44
45 const char *
46 rust_last_path_segment (const char *path)
47 {
48 const char *result = strrchr (path, ':');
49
50 if (result == NULL)
51 return path;
52 return result + 1;
53 }
54
55 /* See rust-lang.h. */
56
57 std::string
58 rust_crate_for_block (const struct block *block)
59 {
60 const char *scope = block->scope ();
61
62 if (scope[0] == '\0')
63 return std::string ();
64
65 return std::string (scope, cp_find_first_component (scope));
66 }
67
68 /* Return true if TYPE, which must be a struct type, represents a Rust
69 enum. */
70
71 static bool
72 rust_enum_p (struct type *type)
73 {
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);
77 }
78
79 /* Return true if TYPE, which must be an already-resolved enum type,
80 has no variants. */
81
82 static bool
83 rust_empty_enum_p (const struct type *type)
84 {
85 return type->num_fields () == 0;
86 }
87
88 /* Given an already-resolved enum type and contents, find which
89 variant is active. */
90
91 static int
92 rust_enum_variant (struct type *type)
93 {
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;
98
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"));
103 }
104
105 /* See rust-lang.h. */
106
107 bool
108 rust_tuple_type_p (struct type *type)
109 {
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] == '(');
116 }
117
118 /* Return true if all non-static fields of a structlike type are in a
119 sequence like __0, __1, __2. */
120
121 static bool
122 rust_underscore_fields (struct type *type)
123 {
124 int field_number = 0;
125
126 if (type->code () != TYPE_CODE_STRUCT)
127 return false;
128 for (int i = 0; i < type->num_fields (); ++i)
129 {
130 if (!type->field (i).is_static ())
131 {
132 char buf[20];
133
134 xsnprintf (buf, sizeof (buf), "__%d", field_number);
135 if (strcmp (buf, type->field (i).name ()) != 0)
136 return false;
137 field_number++;
138 }
139 }
140 return true;
141 }
142
143 /* See rust-lang.h. */
144
145 bool
146 rust_tuple_struct_type_p (struct type *type)
147 {
148 /* This is just an approximation until DWARF can represent Rust more
149 precisely. We exclude zero-length structs because they may not
150 be tuple structs, and there's no way to tell. */
151 return type->num_fields () > 0 && rust_underscore_fields (type);
152 }
153
154 /* Return true if TYPE is "slice-like"; false otherwise. */
155
156 static bool
157 rust_slice_type_p (const struct type *type)
158 {
159 if (type->code () == TYPE_CODE_STRUCT
160 && type->name () != NULL
161 && type->num_fields () == 2)
162 {
163 /* The order of fields doesn't matter. While it would be nice
164 to check for artificiality here, the Rust compiler doesn't
165 emit this information. */
166 const char *n1 = type->field (0).name ();
167 const char *n2 = type->field (1).name ();
168 return ((streq (n1, "data_ptr") && streq (n2, "length"))
169 || (streq (n2, "data_ptr") && streq (n1, "length")));
170 }
171 return false;
172 }
173
174 /* Return true if TYPE is a range type, otherwise false. */
175
176 static bool
177 rust_range_type_p (struct type *type)
178 {
179 if (type->code () != TYPE_CODE_STRUCT
180 || type->num_fields () > 2
181 || type->name () == NULL
182 || strstr (type->name (), "::Range") == NULL)
183 return false;
184
185 if (type->num_fields () == 0)
186 return true;
187
188 int field_num = 0;
189 if (strcmp (type->field (0).name (), "start") == 0)
190 {
191 if (type->num_fields () == 1)
192 return true;
193 field_num = 1;
194 }
195 else if (type->num_fields () == 2)
196 {
197 /* First field had to be "start". */
198 return false;
199 }
200
201 return strcmp (type->field (field_num).name (), "end") == 0;
202 }
203
204 /* Return true if TYPE is an inclusive range type, otherwise false.
205 This is only valid for types which are already known to be range
206 types. */
207
208 static bool
209 rust_inclusive_range_type_p (struct type *type)
210 {
211 return (strstr (type->name (), "::RangeInclusive") != NULL
212 || strstr (type->name (), "::RangeToInclusive") != NULL);
213 }
214
215 /* Return true if TYPE seems to be the type "u8", otherwise false. */
216
217 static bool
218 rust_u8_type_p (struct type *type)
219 {
220 return (type->code () == TYPE_CODE_INT
221 && type->is_unsigned ()
222 && type->length () == 1);
223 }
224
225 /* Return true if TYPE is a Rust character type. */
226
227 static bool
228 rust_chartype_p (struct type *type)
229 {
230 return (type->code () == TYPE_CODE_CHAR
231 && type->length () == 4
232 && type->is_unsigned ());
233 }
234
235 /* If VALUE represents a trait object pointer, return the underlying
236 pointer with the correct (i.e., runtime) type. Otherwise, return
237 NULL. */
238
239 static struct value *
240 rust_get_trait_object_pointer (struct value *value)
241 {
242 struct type *type = check_typedef (value->type ());
243
244 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
245 return NULL;
246
247 /* Try to be a bit resilient if the ABI changes. */
248 int vtable_field = 0;
249 for (int i = 0; i < 2; ++i)
250 {
251 if (strcmp (type->field (i).name (), "vtable") == 0)
252 vtable_field = i;
253 else if (strcmp (type->field (i).name (), "pointer") != 0)
254 return NULL;
255 }
256
257 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
258 struct symbol *symbol = find_symbol_at_address (vtable);
259 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
260 return NULL;
261
262 struct rust_vtable_symbol *vtable_sym
263 = static_cast<struct rust_vtable_symbol *> (symbol);
264 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
265 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
266 }
267
268 /* Find and possibly rewrite the unsized part of a slice-like type.
269
270 This function has two modes. If the out parameters are both NULL,
271 it will return true if an unsized member of IN_TYPE is found.
272
273 If the out parameters are both non-NULL, it will do the same, but
274 will also rewrite the unsized member's type to be an array of the
275 appropriate type. BOUND is the upper bound of the new array.
276
277 See convert_slice to understand the different kinds of unsized type
278 and how they are represented.
279 */
280 static bool
281 rewrite_slice_type (struct type *in_type, struct type **new_type,
282 LONGEST bound, ULONGEST *additional_length)
283 {
284 if (in_type->code () != TYPE_CODE_STRUCT)
285 return false;
286
287 unsigned nfields = in_type->num_fields ();
288 if (nfields == 0)
289 return false;
290
291 struct type *rewritten;
292 const field &field = in_type->field (nfields - 1);
293 struct type *field_type = field.type ();
294 if (field.loc_kind () == FIELD_LOC_KIND_BITPOS
295 && field.loc_bitpos () == 8 * in_type->length ())
296 {
297 if (additional_length == nullptr)
298 return true;
299 rewritten = lookup_array_range_type (field_type, 0, bound);
300 *additional_length = rewritten->length ();
301 }
302 else
303 {
304 if (!rewrite_slice_type (field_type, &rewritten, bound,
305 additional_length))
306 return false;
307 if (additional_length == nullptr)
308 return true;
309 }
310
311 struct type *result = copy_type (in_type);
312 result->copy_fields (in_type);
313 result->field (nfields - 1).set_type (rewritten);
314 result->set_length (result->length () + *additional_length);
315
316 *new_type = result;
317 return true;
318 }
319
320 /* Convert a Rust slice to its "true" representation.
321
322 The Rust compiler emits slices as "fat" pointers like:
323
324 struct { payload *data_ptr; usize length }
325
326 Any sort of unsized type is emitted this way.
327
328 If 'payload' is a struct type, then it must be searched to see if
329 the trailing field is unsized. This has to be done recursively (as
330 in, if the final field in the struct type itself has struct type,
331 then that type must be searched). In this scenario, the unsized
332 field can be recognized because it does not contribute to the
333 type's size.
334
335 If 'payload' does not have a trailing unsized type, or if it is not
336 of struct type, then this slice is "array-like". In this case
337 rewriting will return an array.
338 */
339 static struct value *
340 convert_slice (struct value *val)
341 {
342 struct type *type = check_typedef (val->type ());
343 /* This must have been checked by the caller. */
344 gdb_assert (rust_slice_type_p (type));
345
346 struct value *len = value_struct_elt (&val, {}, "length", nullptr,
347 "slice");
348 LONGEST llen = value_as_long (len);
349
350 struct value *ptr = value_struct_elt (&val, {}, "data_ptr", nullptr,
351 "slice");
352 struct type *original_type = ptr->type ()->target_type ();
353 ULONGEST new_length_storage = 0;
354 struct type *new_type = nullptr;
355 if (!rewrite_slice_type (original_type, &new_type, llen - 1,
356 &new_length_storage))
357 new_type = lookup_array_range_type (original_type, 0, llen - 1);
358
359 struct value *result = value::allocate_lazy (new_type);
360 result->set_lval (lval_memory);
361 result->set_address (value_as_address (ptr));
362 result->fetch_lazy ();
363
364 return result;
365 }
366
367 /* If TYPE is an array-like slice, return the element type; otherwise
368 return NULL. */
369 static struct type *
370 rust_array_like_element_type (struct type *type)
371 {
372 /* Caller must check this. */
373 gdb_assert (rust_slice_type_p (type));
374 for (int i = 0; i < type->num_fields (); ++i)
375 {
376 if (strcmp (type->field (i).name (), "data_ptr") == 0)
377 {
378 struct type *base_type = type->field (i).type ()->target_type ();
379 if (rewrite_slice_type (base_type, nullptr, 0, nullptr))
380 return nullptr;
381 return base_type;
382 }
383 }
384 return nullptr;
385 }
386
387
388
390 /* See language.h. */
391
392 void
393 rust_language::printstr (struct ui_file *stream, struct type *type,
394 const gdb_byte *string, unsigned int length,
395 const char *user_encoding, int force_ellipses,
396 const struct value_print_options *options) const
397 {
398 /* Rust always uses UTF-8, but let the caller override this if need
399 be. */
400 const char *encoding = user_encoding;
401 if (user_encoding == NULL || !*user_encoding)
402 {
403 /* In Rust strings, characters are "u8". */
404 if (rust_u8_type_p (type))
405 encoding = "UTF-8";
406 else
407 {
408 /* This is probably some C string, so let's let C deal with
409 it. */
410 language_defn::printstr (stream, type, string, length,
411 user_encoding, force_ellipses,
412 options);
413 return;
414 }
415 }
416
417 /* This is not ideal as it doesn't use our character printer. */
418 generic_printstr (stream, type, string, length, encoding, force_ellipses,
419 '"', 0, options);
420 }
421
422
423
425 static const struct generic_val_print_decorations rust_decorations =
426 {
427 /* Complex isn't used in Rust, but we provide C-ish values just in
428 case. */
429 "",
430 " + ",
431 " * I",
432 "true",
433 "false",
434 "()",
435 "[",
436 "]"
437 };
438
439 /* See rust-lang.h. */
440
441 struct value *
442 rust_slice_to_array (struct value *val)
443 {
444 val = convert_slice (val);
445 if (val->type ()->code () != TYPE_CODE_ARRAY)
446 return nullptr;
447 return val;
448 }
449
450 /* Helper function to print a slice. */
451
452 void
453 rust_language::val_print_slice
454 (struct value *val, struct ui_file *stream, int recurse,
455 const struct value_print_options *options) const
456 {
457 struct type *orig_type = check_typedef (val->type ());
458
459 val = convert_slice (val);
460 struct type *type = check_typedef (val->type ());
461
462 /* &str is handled here; but for all other slice types it is fine to
463 simply print the contents. */
464 if (orig_type->name () != nullptr
465 && strcmp (orig_type->name (), "&str") == 0)
466 {
467 LONGEST low_bound, high_bound;
468 if (get_array_bounds (type, &low_bound, &high_bound))
469 {
470 val_print_string (type->target_type (), "UTF-8",
471 val->address (), high_bound - low_bound + 1,
472 stream, options);
473 return;
474 }
475 }
476
477 /* Print the slice type here. This was gdb's historical behavior
478 (from before unsized types were generically handled) and helps
479 make it clear that the user is seeing a slice, not an array.
480 Only arrays must be handled as the other cases are handled by
481 value_print_inner. */
482 if (type->code () == TYPE_CODE_ARRAY)
483 {
484 type_print (orig_type, "", stream, -1);
485 gdb_printf (stream, " ");
486 }
487
488 value_print_inner (val, stream, recurse, options);
489 }
490
491 /* See rust-lang.h. */
492
493 void
494 rust_language::val_print_struct
495 (struct value *val, struct ui_file *stream, int recurse,
496 const struct value_print_options *options) const
497 {
498 int first_field;
499 struct type *type = check_typedef (val->type ());
500
501 if (rust_slice_type_p (type))
502 {
503 val_print_slice (val, stream, recurse, options);
504 return;
505 }
506
507 bool is_tuple = rust_tuple_type_p (type);
508 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
509 struct value_print_options opts;
510
511 if (!is_tuple)
512 {
513 if (type->name () != NULL)
514 gdb_printf (stream, "%s", type->name ());
515
516 if (type->num_fields () == 0)
517 return;
518
519 if (type->name () != NULL)
520 gdb_puts (" ", stream);
521 }
522
523 if (is_tuple || is_tuple_struct)
524 gdb_puts ("(", stream);
525 else
526 gdb_puts ("{", stream);
527
528 opts = *options;
529 opts.deref_ref = false;
530
531 first_field = 1;
532 for (int i = 0; i < type->num_fields (); ++i)
533 {
534 if (type->field (i).is_static ())
535 continue;
536
537 if (!first_field)
538 gdb_puts (",", stream);
539
540 if (options->prettyformat)
541 {
542 gdb_puts ("\n", stream);
543 print_spaces (2 + 2 * recurse, stream);
544 }
545 else if (!first_field)
546 gdb_puts (" ", stream);
547
548 first_field = 0;
549
550 if (!is_tuple && !is_tuple_struct)
551 {
552 fputs_styled (type->field (i).name (),
553 variable_name_style.style (), stream);
554 gdb_puts (": ", stream);
555 }
556
557 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
558 this);
559 }
560
561 if (options->prettyformat)
562 {
563 gdb_puts ("\n", stream);
564 print_spaces (2 * recurse, stream);
565 }
566
567 if (is_tuple || is_tuple_struct)
568 gdb_puts (")", stream);
569 else
570 gdb_puts ("}", stream);
571 }
572
573 /* See rust-lang.h. */
574
575 void
576 rust_language::print_enum (struct value *val, struct ui_file *stream,
577 int recurse,
578 const struct value_print_options *options) const
579 {
580 struct value_print_options opts = *options;
581 struct type *type = check_typedef (val->type ());
582
583 opts.deref_ref = false;
584
585 gdb_assert (rust_enum_p (type));
586 gdb::array_view<const gdb_byte> view
587 (val->contents_for_printing ().data (),
588 val->type ()->length ());
589 type = resolve_dynamic_type (type, view, val->address ());
590
591 if (rust_empty_enum_p (type))
592 {
593 /* Print the enum type name here to be more clear. */
594 gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
595 type->name (),
596 metadata_style.style ().ptr (), nullptr);
597 return;
598 }
599
600 int variant_fieldno = rust_enum_variant (type);
601 val = val->primitive_field (0, variant_fieldno, type);
602 struct type *variant_type = type->field (variant_fieldno).type ();
603
604 int nfields = variant_type->num_fields ();
605
606 bool is_tuple = rust_tuple_struct_type_p (variant_type);
607
608 gdb_printf (stream, "%s", variant_type->name ());
609 if (nfields == 0)
610 {
611 /* In case of a nullary variant like 'None', just output
612 the name. */
613 return;
614 }
615
616 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
617 if (is_tuple)
618 gdb_printf (stream, "(");
619 else
620 {
621 /* struct variant. */
622 gdb_printf (stream, "{");
623 }
624
625 bool first_field = true;
626 for (int j = 0; j < nfields; j++)
627 {
628 if (!first_field)
629 gdb_puts (", ", stream);
630 first_field = false;
631
632 if (!is_tuple)
633 gdb_printf (stream, "%ps: ",
634 styled_string (variable_name_style.style (),
635 variant_type->field (j).name ()));
636
637 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
638 this);
639 }
640
641 if (is_tuple)
642 gdb_puts (")", stream);
643 else
644 gdb_puts ("}", stream);
645 }
646
647 /* See language.h. */
648
649 void
650 rust_language::value_print_inner
651 (struct value *val, struct ui_file *stream, int recurse,
652 const struct value_print_options *options) const
653 {
654 struct value_print_options opts = *options;
655 opts.deref_ref = true;
656
657 if (opts.prettyformat == Val_prettyformat_default)
658 opts.prettyformat = (opts.prettyformat_structs
659 ? Val_prettyformat : Val_no_prettyformat);
660
661 struct type *type = check_typedef (val->type ());
662 switch (type->code ())
663 {
664 case TYPE_CODE_PTR:
665 {
666 LONGEST low_bound, high_bound;
667
668 if (type->target_type ()->code () == TYPE_CODE_ARRAY
669 && rust_u8_type_p (type->target_type ()->target_type ())
670 && get_array_bounds (type->target_type (), &low_bound,
671 &high_bound))
672 {
673 /* We have a pointer to a byte string, so just print
674 that. */
675 struct type *elttype = check_typedef (type->target_type ());
676 CORE_ADDR addr = value_as_address (val);
677 struct gdbarch *arch = type->arch ();
678
679 if (opts.addressprint)
680 {
681 gdb_puts (paddress (arch, addr), stream);
682 gdb_puts (" ", stream);
683 }
684
685 gdb_puts ("b", stream);
686 val_print_string (elttype->target_type (), "ASCII", addr,
687 high_bound - low_bound + 1, stream,
688 &opts);
689 break;
690 }
691 }
692 goto generic_print;
693
694 case TYPE_CODE_INT:
695 /* Recognize the unit type. */
696 if (type->is_unsigned () && type->length () == 0
697 && type->name () != NULL && strcmp (type->name (), "()") == 0)
698 {
699 gdb_puts ("()", stream);
700 break;
701 }
702 goto generic_print;
703
704 case TYPE_CODE_STRING:
705 {
706 LONGEST low_bound, high_bound;
707
708 if (!get_array_bounds (type, &low_bound, &high_bound))
709 error (_("Could not determine the array bounds"));
710
711 /* If we see a plain TYPE_CODE_STRING, then we're printing a
712 byte string, hence the choice of "ASCII" as the
713 encoding. */
714 gdb_puts ("b", stream);
715 printstr (stream, type->target_type (),
716 val->contents_for_printing ().data (),
717 high_bound - low_bound + 1, "ASCII", 0, &opts);
718 }
719 break;
720
721 case TYPE_CODE_ARRAY:
722 {
723 LONGEST low_bound, high_bound;
724
725 if (get_array_bounds (type, &low_bound, &high_bound)
726 && high_bound - low_bound + 1 == 0)
727 gdb_puts ("[]", stream);
728 else
729 goto generic_print;
730 }
731 break;
732
733 case TYPE_CODE_UNION:
734 /* Untagged unions are printed as if they are structs. Since
735 the field bit positions overlap in the debuginfo, the code
736 for printing a union is same as that for a struct, the only
737 difference is that the input type will have overlapping
738 fields. */
739 val_print_struct (val, stream, recurse, &opts);
740 break;
741
742 case TYPE_CODE_STRUCT:
743 if (rust_enum_p (type))
744 print_enum (val, stream, recurse, &opts);
745 else
746 val_print_struct (val, stream, recurse, &opts);
747 break;
748
749 default:
750 generic_print:
751 /* Nothing special yet. */
752 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
753 }
754 }
755
756 /* See language.h. */
757
758 void
759 rust_language::value_print
760 (struct value *val, struct ui_file *stream,
761 const struct value_print_options *options) const
762 {
763 value_print_options opts = *options;
764 opts.deref_ref = true;
765
766 struct type *type = check_typedef (val->type ());
767 if (type->is_pointer_or_reference ())
768 {
769 gdb_printf (stream, "(");
770 type_print (val->type (), "", stream, -1);
771 gdb_printf (stream, ") ");
772 }
773
774 return common_val_print (val, stream, 0, &opts, this);
775 }
776
777
778
780 static void
781 rust_internal_print_type (struct type *type, const char *varstring,
782 struct ui_file *stream, int show, int level,
783 const struct type_print_options *flags,
784 bool for_rust_enum, print_offset_data *podata);
785
786 /* Print a struct or union typedef. */
787 static void
788 rust_print_struct_def (struct type *type, const char *varstring,
789 struct ui_file *stream, int show, int level,
790 const struct type_print_options *flags,
791 bool for_rust_enum, print_offset_data *podata)
792 {
793 /* Print a tuple type simply. */
794 if (rust_tuple_type_p (type))
795 {
796 gdb_puts (type->name (), stream);
797 return;
798 }
799
800 /* If we see a base class, delegate to C. */
801 if (TYPE_N_BASECLASSES (type) > 0)
802 c_print_type (type, varstring, stream, show, level, language_rust, flags);
803
804 if (flags->print_offsets)
805 {
806 /* Temporarily bump the level so that the output lines up
807 correctly. */
808 level += 2;
809 }
810
811 /* Compute properties of TYPE here because, in the enum case, the
812 rest of the code ends up looking only at the variant part. */
813 const char *tagname = type->name ();
814 bool is_tuple_struct = rust_tuple_struct_type_p (type);
815 bool is_tuple = rust_tuple_type_p (type);
816 bool is_enum = rust_enum_p (type);
817
818 if (for_rust_enum)
819 {
820 /* Already printing an outer enum, so nothing to print here. */
821 }
822 else
823 {
824 /* This code path is also used by unions and enums. */
825 if (is_enum)
826 {
827 gdb_puts ("enum ", stream);
828 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
829 if (prop != nullptr && prop->kind () == PROP_TYPE)
830 type = prop->original_type ();
831 }
832 else if (type->code () == TYPE_CODE_STRUCT)
833 gdb_puts ("struct ", stream);
834 else
835 gdb_puts ("union ", stream);
836
837 if (tagname != NULL)
838 gdb_puts (tagname, stream);
839 }
840
841 if (type->num_fields () == 0 && !is_tuple)
842 return;
843 if (for_rust_enum && !flags->print_offsets)
844 gdb_puts (is_tuple_struct ? "(" : "{", stream);
845 else
846 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
847
848 /* When printing offsets, we rearrange the fields into storage
849 order. This lets us show holes more clearly. We work using
850 field indices here because it simplifies calls to
851 print_offset_data::update below. */
852 std::vector<int> fields;
853 for (int i = 0; i < type->num_fields (); ++i)
854 {
855 if (type->field (i).is_static ())
856 continue;
857 if (is_enum && type->field (i).is_artificial ())
858 continue;
859 fields.push_back (i);
860 }
861 if (flags->print_offsets)
862 std::sort (fields.begin (), fields.end (),
863 [&] (int a, int b)
864 {
865 return (type->field (a).loc_bitpos ()
866 < type->field (b).loc_bitpos ());
867 });
868
869 for (int i : fields)
870 {
871 QUIT;
872
873 gdb_assert (!type->field (i).is_static ());
874 gdb_assert (! (is_enum && type->field (i).is_artificial ()));
875
876 if (flags->print_offsets)
877 podata->update (type, i, stream);
878
879 /* We'd like to print "pub" here as needed, but rustc
880 doesn't emit the debuginfo, and our types don't have
881 cplus_struct_type attached. */
882
883 /* For a tuple struct we print the type but nothing
884 else. */
885 if (!for_rust_enum || flags->print_offsets)
886 print_spaces (level + 2, stream);
887 if (is_enum)
888 fputs_styled (type->field (i).name (), variable_name_style.style (),
889 stream);
890 else if (!is_tuple_struct)
891 gdb_printf (stream, "%ps: ",
892 styled_string (variable_name_style.style (),
893 type->field (i).name ()));
894
895 rust_internal_print_type (type->field (i).type (), NULL,
896 stream, (is_enum ? show : show - 1),
897 level + 2, flags, is_enum, podata);
898 if (!for_rust_enum || flags->print_offsets)
899 gdb_puts (",\n", stream);
900 /* Note that this check of "I" is ok because we only sorted the
901 fields by offset when print_offsets was set, so we won't take
902 this branch in that case. */
903 else if (i + 1 < type->num_fields ())
904 gdb_puts (", ", stream);
905 }
906
907 if (flags->print_offsets)
908 {
909 /* Undo the temporary level increase we did above. */
910 level -= 2;
911 podata->finish (type, level, stream);
912 print_spaces (print_offset_data::indentation, stream);
913 if (level == 0)
914 print_spaces (2, stream);
915 }
916 if (!for_rust_enum || flags->print_offsets)
917 print_spaces (level, stream);
918 gdb_puts (is_tuple_struct ? ")" : "}", stream);
919 }
920
921 /* la_print_type implementation for Rust. */
922
923 static void
924 rust_internal_print_type (struct type *type, const char *varstring,
925 struct ui_file *stream, int show, int level,
926 const struct type_print_options *flags,
927 bool for_rust_enum, print_offset_data *podata)
928 {
929 QUIT;
930 if (show <= 0
931 && type->name () != NULL)
932 {
933 /* Rust calls the unit type "void" in its debuginfo,
934 but we don't want to print it as that. */
935 if (type->code () == TYPE_CODE_VOID)
936 gdb_puts ("()", stream);
937 else
938 gdb_puts (type->name (), stream);
939 return;
940 }
941
942 type = check_typedef (type);
943 switch (type->code ())
944 {
945 case TYPE_CODE_VOID:
946 /* If we have an enum, we've already printed the type's
947 unqualified name, and there is nothing else to print
948 here. */
949 if (!for_rust_enum)
950 gdb_puts ("()", stream);
951 break;
952
953 case TYPE_CODE_FUNC:
954 /* Delegate varargs to the C printer. */
955 if (type->has_varargs ())
956 goto c_printer;
957
958 gdb_puts ("fn ", stream);
959 if (varstring != NULL)
960 gdb_puts (varstring, stream);
961 gdb_puts ("(", stream);
962 for (int i = 0; i < type->num_fields (); ++i)
963 {
964 QUIT;
965 if (i > 0)
966 gdb_puts (", ", stream);
967 rust_internal_print_type (type->field (i).type (), "", stream,
968 -1, 0, flags, false, podata);
969 }
970 gdb_puts (")", stream);
971 /* If it returns unit, we can omit the return type. */
972 if (type->target_type ()->code () != TYPE_CODE_VOID)
973 {
974 gdb_puts (" -> ", stream);
975 rust_internal_print_type (type->target_type (), "", stream,
976 -1, 0, flags, false, podata);
977 }
978 break;
979
980 case TYPE_CODE_ARRAY:
981 {
982 LONGEST low_bound, high_bound;
983
984 gdb_puts ("[", stream);
985 rust_internal_print_type (type->target_type (), NULL,
986 stream, show - 1, level, flags, false,
987 podata);
988
989 if (type->bounds ()->high.kind () == PROP_LOCEXPR
990 || type->bounds ()->high.kind () == PROP_LOCLIST)
991 gdb_printf (stream, "; variable length");
992 else if (get_array_bounds (type, &low_bound, &high_bound))
993 gdb_printf (stream, "; %s",
994 plongest (high_bound - low_bound + 1));
995 gdb_puts ("]", stream);
996 }
997 break;
998
999 case TYPE_CODE_UNION:
1000 case TYPE_CODE_STRUCT:
1001 rust_print_struct_def (type, varstring, stream, show, level, flags,
1002 for_rust_enum, podata);
1003 break;
1004
1005 case TYPE_CODE_ENUM:
1006 {
1007 int len = 0;
1008
1009 gdb_puts ("enum ", stream);
1010 if (type->name () != NULL)
1011 {
1012 gdb_puts (type->name (), stream);
1013 gdb_puts (" ", stream);
1014 len = strlen (type->name ());
1015 }
1016 gdb_puts ("{\n", stream);
1017
1018 for (int i = 0; i < type->num_fields (); ++i)
1019 {
1020 const char *name = type->field (i).name ();
1021
1022 QUIT;
1023
1024 if (len > 0
1025 && strncmp (name, type->name (), len) == 0
1026 && name[len] == ':'
1027 && name[len + 1] == ':')
1028 name += len + 2;
1029 gdb_printf (stream, "%*s%ps,\n",
1030 level + 2, "",
1031 styled_string (variable_name_style.style (),
1032 name));
1033 }
1034
1035 gdb_puts ("}", stream);
1036 }
1037 break;
1038
1039 case TYPE_CODE_PTR:
1040 {
1041 if (type->name () != nullptr)
1042 gdb_puts (type->name (), stream);
1043 else
1044 {
1045 /* We currently can't distinguish between pointers and
1046 references. */
1047 gdb_puts ("*mut ", stream);
1048 type_print (type->target_type (), "", stream, 0);
1049 }
1050 }
1051 break;
1052
1053 default:
1054 c_printer:
1055 c_print_type (type, varstring, stream, show, level, language_rust,
1056 flags);
1057 }
1058 }
1059
1060
1061
1063 /* Like arch_composite_type, but uses TYPE to decide how to allocate
1064 -- either on an obstack or on a gdbarch. */
1065
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)
1071 {
1072 struct type *result = type_allocator (original).new_type ();
1073 int i, nfields, bitpos;
1074
1075 nfields = 0;
1076 if (field1 != NULL)
1077 ++nfields;
1078 if (field2 != NULL)
1079 ++nfields;
1080
1081 result->set_code (TYPE_CODE_STRUCT);
1082 result->set_name (name);
1083
1084 result->alloc_fields (nfields);
1085
1086 i = 0;
1087 bitpos = 0;
1088 if (field1 != NULL)
1089 {
1090 struct field *field = &result->field (i);
1091
1092 field->set_loc_bitpos (bitpos);
1093 bitpos += type1->length () * TARGET_CHAR_BIT;
1094
1095 field->set_name (field1);
1096 field->set_type (type1);
1097 ++i;
1098 }
1099 if (field2 != NULL)
1100 {
1101 struct field *field = &result->field (i);
1102 unsigned align = type_align (type2);
1103
1104 if (align != 0)
1105 {
1106 int delta;
1107
1108 align *= TARGET_CHAR_BIT;
1109 delta = bitpos % align;
1110 if (delta != 0)
1111 bitpos += align - delta;
1112 }
1113 field->set_loc_bitpos (bitpos);
1114
1115 field->set_name (field2);
1116 field->set_type (type2);
1117 ++i;
1118 }
1119
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;
1124 }
1125
1126 /* See rust-lang.h. */
1127
1128 struct type *
1129 rust_slice_type (const char *name, struct type *elt_type,
1130 struct type *usize_type)
1131 {
1132 struct type *type;
1133
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);
1138
1139 return type;
1140 }
1141
1142
1143
1145 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1146
1147 struct value *
1148 rust_range (struct type *expect_type, struct expression *exp,
1149 enum noside noside, enum range_flag kind,
1150 struct value *low, struct value *high)
1151 {
1152 struct value *addrval, *result;
1153 CORE_ADDR addr;
1154 struct type *range_type;
1155 struct type *index_type;
1156 struct type *temp_type;
1157 const char *name;
1158
1159 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
1160
1161 if (low == NULL)
1162 {
1163 if (high == NULL)
1164 {
1165 index_type = NULL;
1166 name = "std::ops::RangeFull";
1167 }
1168 else
1169 {
1170 index_type = high->type ();
1171 name = (inclusive
1172 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1173 }
1174 }
1175 else
1176 {
1177 if (high == NULL)
1178 {
1179 index_type = low->type ();
1180 name = "std::ops::RangeFrom";
1181 }
1182 else
1183 {
1184 if (!types_equal (low->type (), high->type ()))
1185 error (_("Range expression with different types"));
1186 index_type = low->type ();
1187 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1188 }
1189 }
1190
1191 /* If we don't have an index type, just allocate this on the
1192 arch. Here any type will do. */
1193 temp_type = (index_type == NULL
1194 ? language_bool_type (exp->language_defn, exp->gdbarch)
1195 : index_type);
1196 /* It would be nicer to cache the range type. */
1197 range_type = rust_composite_type (temp_type, name,
1198 low == NULL ? NULL : "start", index_type,
1199 high == NULL ? NULL : "end", index_type);
1200
1201 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1202 return value::zero (range_type, lval_memory);
1203
1204 addrval = value_allocate_space_in_inferior (range_type->length ());
1205 addr = value_as_long (addrval);
1206 result = value_at_lazy (range_type, addr);
1207
1208 if (low != NULL)
1209 {
1210 struct value *start = value_struct_elt (&result, {}, "start", NULL,
1211 "range");
1212
1213 value_assign (start, low);
1214 }
1215
1216 if (high != NULL)
1217 {
1218 struct value *end = value_struct_elt (&result, {}, "end", NULL,
1219 "range");
1220
1221 value_assign (end, high);
1222 }
1223
1224 result = value_at_lazy (range_type, addr);
1225 return result;
1226 }
1227
1228 /* A helper function to compute the range and kind given a range
1229 value. TYPE is the type of the range value. RANGE is the range
1230 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1231 parameters might be filled in, or might not be, depending on the
1232 kind of range this is. KIND will always be set to the appropriate
1233 value describing the kind of range, and this can be used to
1234 determine whether LOW or HIGH are valid. */
1235
1236 static void
1237 rust_compute_range (struct type *type, struct value *range,
1238 LONGEST *low, LONGEST *high,
1239 range_flags *kind)
1240 {
1241 int i;
1242
1243 *low = 0;
1244 *high = 0;
1245 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1246
1247 if (type->num_fields () == 0)
1248 return;
1249
1250 i = 0;
1251 if (strcmp (type->field (0).name (), "start") == 0)
1252 {
1253 *kind = RANGE_HIGH_BOUND_DEFAULT;
1254 *low = value_as_long (value_field (range, 0));
1255 ++i;
1256 }
1257 if (type->num_fields () > i
1258 && strcmp (type->field (i).name (), "end") == 0)
1259 {
1260 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1261 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
1262 *high = value_as_long (value_field (range, i));
1263
1264 if (rust_inclusive_range_type_p (type))
1265 ++*high;
1266 }
1267 }
1268
1269 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1270
1271 struct value *
1272 rust_subscript (struct type *expect_type, struct expression *exp,
1273 enum noside noside, bool for_addr,
1274 struct value *lhs, struct value *rhs)
1275 {
1276 struct value *result;
1277 struct type *rhstype;
1278 LONGEST low, high_bound;
1279 /* Initialized to appease the compiler. */
1280 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1281 LONGEST high = 0;
1282 bool want_slice = false;
1283
1284 rhstype = check_typedef (rhs->type ());
1285 if (rust_range_type_p (rhstype))
1286 {
1287 if (!for_addr)
1288 error (_("Can't take slice of array without '&'"));
1289 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1290 want_slice = true;
1291 }
1292 else
1293 low = value_as_long (rhs);
1294
1295 struct type *type = check_typedef (lhs->type ());
1296 struct type *orig_type = type;
1297 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1298 {
1299 struct type *base_type = nullptr;
1300 if (type->code () == TYPE_CODE_ARRAY)
1301 base_type = type->target_type ();
1302 else if (rust_slice_type_p (type))
1303 {
1304 base_type = rust_array_like_element_type (type);
1305 if (base_type == nullptr)
1306 error (_("Cannot subscript non-array-like slice"));
1307 }
1308 else if (type->code () == TYPE_CODE_PTR)
1309 base_type = type->target_type ();
1310 else
1311 error (_("Cannot subscript non-array type"));
1312
1313 struct type *new_type;
1314 if (want_slice)
1315 {
1316 if (rust_slice_type_p (type))
1317 new_type = type;
1318 else
1319 {
1320 struct type *usize
1321 = language_lookup_primitive_type (exp->language_defn,
1322 exp->gdbarch,
1323 "usize");
1324 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1325 }
1326 }
1327 else
1328 new_type = base_type;
1329
1330 return value::zero (new_type, lhs->lval ());
1331 }
1332 else
1333 {
1334 LONGEST low_bound;
1335 struct value *base;
1336
1337 if (rust_slice_type_p (type))
1338 {
1339 lhs = convert_slice (lhs);
1340 type = check_typedef (lhs->type ());
1341 }
1342
1343 if (type->code () == TYPE_CODE_ARRAY)
1344 {
1345 base = lhs;
1346 if (!get_array_bounds (type, &low_bound, &high_bound))
1347 error (_("Can't compute array bounds"));
1348 if (low_bound != 0)
1349 error (_("Found array with non-zero lower bound"));
1350 ++high_bound;
1351 }
1352 else if (type->code () == TYPE_CODE_PTR)
1353 {
1354 base = lhs;
1355 low_bound = 0;
1356 high_bound = LONGEST_MAX;
1357 }
1358 else
1359 error (_("Cannot subscript non-array type"));
1360
1361 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
1362 low = low_bound;
1363 if (low < 0)
1364 error (_("Index less than zero"));
1365 if (low > high_bound)
1366 error (_("Index greater than length"));
1367
1368 result = value_subscript (base, low);
1369 }
1370
1371 if (for_addr)
1372 {
1373 if (want_slice)
1374 {
1375 struct type *usize, *slice;
1376 CORE_ADDR addr;
1377 struct value *addrval, *tem;
1378
1379 if (kind & RANGE_HIGH_BOUND_DEFAULT)
1380 high = high_bound;
1381 if (high < 0)
1382 error (_("High index less than zero"));
1383 if (low > high)
1384 error (_("Low index greater than high index"));
1385 if (high > high_bound)
1386 error (_("High index greater than length"));
1387
1388 usize = language_lookup_primitive_type (exp->language_defn,
1389 exp->gdbarch,
1390 "usize");
1391 /* Preserve the name for slice-of-slice; this lets
1392 string-printing work a bit more nicely. */
1393 const char *new_name = ((orig_type != nullptr
1394 && rust_slice_type_p (orig_type))
1395 ? orig_type->name () : "&[*gdb*]");
1396
1397 slice = rust_slice_type (new_name, result->type (), usize);
1398
1399 addrval = value_allocate_space_in_inferior (slice->length ());
1400 addr = value_as_long (addrval);
1401 tem = value_at_lazy (slice, addr);
1402
1403 value_assign (value_field (tem, 0), value_addr (result));
1404 value_assign (value_field (tem, 1),
1405 value_from_longest (usize, high - low));
1406
1407 result = value_at_lazy (slice, addr);
1408 }
1409 else
1410 result = value_addr (result);
1411 }
1412
1413 return result;
1414 }
1415
1416 namespace expr
1417 {
1418
1419 struct value *
1420 rust_unop_ind_operation::evaluate (struct type *expect_type,
1421 struct expression *exp,
1422 enum noside noside)
1423 {
1424 if (noside != EVAL_NORMAL)
1425 return unop_ind_operation::evaluate (expect_type, exp, noside);
1426
1427 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1428 noside);
1429 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1430 if (trait_ptr != NULL)
1431 value = trait_ptr;
1432
1433 return value_ind (value);
1434 }
1435
1436 } /* namespace expr */
1437
1438 /* A helper function for UNOP_COMPLEMENT. */
1439
1440 struct value *
1441 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1442 enum noside noside,
1443 enum exp_opcode opcode,
1444 struct value *value)
1445 {
1446 if (value->type ()->code () == TYPE_CODE_BOOL)
1447 return value_from_longest (value->type (), value_logical_not (value));
1448 return value_complement (value);
1449 }
1450
1451 /* A helper function for OP_ARRAY. */
1452
1453 struct value *
1454 eval_op_rust_array (struct type *expect_type, struct expression *exp,
1455 enum noside noside,
1456 enum exp_opcode opcode,
1457 struct value *elt, struct value *ncopies)
1458 {
1459 int copies = value_as_long (ncopies);
1460 if (copies < 0)
1461 error (_("Array with negative number of elements"));
1462
1463 if (noside == EVAL_NORMAL)
1464 return value_array (0, std::vector<value *> (copies, elt));
1465 else
1466 {
1467 struct type *arraytype
1468 = lookup_array_range_type (elt->type (), 0, copies - 1);
1469 return value::allocate (arraytype);
1470 }
1471 }
1472
1473 namespace expr
1474 {
1475
1476 struct value *
1477 rust_struct_anon::evaluate (struct type *expect_type,
1478 struct expression *exp,
1479 enum noside noside)
1480 {
1481 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1482 int field_number = std::get<0> (m_storage);
1483
1484 struct type *type = lhs->type ();
1485
1486 if (type->code () == TYPE_CODE_STRUCT)
1487 {
1488 struct type *outer_type = NULL;
1489
1490 if (rust_enum_p (type))
1491 {
1492 type = resolve_dynamic_type (type, lhs->contents (),
1493 lhs->address ());
1494
1495 if (rust_empty_enum_p (type))
1496 error (_("Cannot access field %d of empty enum %s"),
1497 field_number, type->name ());
1498
1499 int fieldno = rust_enum_variant (type);
1500 lhs = lhs->primitive_field (0, fieldno, type);
1501 outer_type = type;
1502 type = lhs->type ();
1503 }
1504
1505 /* Tuples and tuple structs */
1506 int nfields = type->num_fields ();
1507
1508 if (field_number >= nfields || field_number < 0)
1509 {
1510 if (outer_type != NULL)
1511 error(_("Cannot access field %d of variant %s::%s, "
1512 "there are only %d fields"),
1513 field_number, outer_type->name (),
1514 rust_last_path_segment (type->name ()),
1515 nfields);
1516 else
1517 error(_("Cannot access field %d of %s, "
1518 "there are only %d fields"),
1519 field_number, type->name (), nfields);
1520 }
1521
1522 /* Tuples are tuple structs too. */
1523 if (!rust_tuple_struct_type_p (type))
1524 {
1525 if (outer_type != NULL)
1526 error(_("Variant %s::%s is not a tuple variant"),
1527 outer_type->name (),
1528 rust_last_path_segment (type->name ()));
1529 else
1530 error(_("Attempting to access anonymous field %d "
1531 "of %s, which is not a tuple, tuple struct, or "
1532 "tuple-like variant"),
1533 field_number, type->name ());
1534 }
1535
1536 return lhs->primitive_field (0, field_number, type);
1537 }
1538 else
1539 error(_("Anonymous field access is only allowed on tuples, \
1540 tuple structs, and tuple-like enum variants"));
1541 }
1542
1543 struct value *
1544 rust_structop::evaluate (struct type *expect_type,
1545 struct expression *exp,
1546 enum noside noside)
1547 {
1548 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1549 const char *field_name = std::get<1> (m_storage).c_str ();
1550
1551 struct value *result;
1552 struct type *type = lhs->type ();
1553 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1554 {
1555 type = resolve_dynamic_type (type, lhs->contents (),
1556 lhs->address ());
1557
1558 if (rust_empty_enum_p (type))
1559 error (_("Cannot access field %s of empty enum %s"),
1560 field_name, type->name ());
1561
1562 int fieldno = rust_enum_variant (type);
1563 lhs = lhs->primitive_field (0, fieldno, type);
1564
1565 struct type *outer_type = type;
1566 type = lhs->type ();
1567 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1568 error (_("Attempting to access named field %s of tuple "
1569 "variant %s::%s, which has only anonymous fields"),
1570 field_name, outer_type->name (),
1571 rust_last_path_segment (type->name ()));
1572
1573 try
1574 {
1575 result = value_struct_elt (&lhs, {}, field_name,
1576 NULL, "structure");
1577 }
1578 catch (const gdb_exception_error &except)
1579 {
1580 error (_("Could not find field %s of struct variant %s::%s"),
1581 field_name, outer_type->name (),
1582 rust_last_path_segment (type->name ()));
1583 }
1584 }
1585 else
1586 {
1587 if (rust_slice_type_p (type))
1588 lhs = convert_slice (lhs);
1589 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1590 }
1591 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1592 result = value::zero (result->type (), result->lval ());
1593 return result;
1594 }
1595
1596 value *
1597 rust_aggregate_operation::evaluate (struct type *expect_type,
1598 struct expression *exp,
1599 enum noside noside)
1600 {
1601 struct type *type = std::get<0> (m_storage);
1602 CORE_ADDR addr = 0;
1603 struct value *addrval = NULL;
1604 value *result;
1605
1606 if (noside == EVAL_NORMAL)
1607 {
1608 addrval = value_allocate_space_in_inferior (type->length ());
1609 addr = value_as_long (addrval);
1610 result = value_at_lazy (type, addr);
1611 }
1612
1613 if (std::get<1> (m_storage) != nullptr)
1614 {
1615 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1616 noside);
1617
1618 if (noside == EVAL_NORMAL)
1619 {
1620 /* This isn't quite right but will do for the time
1621 being, seeing that we can't implement the Copy
1622 trait anyway. */
1623 value_assign (result, init);
1624 }
1625 }
1626
1627 for (const auto &item : std::get<2> (m_storage))
1628 {
1629 value *val = item.second->evaluate (nullptr, exp, noside);
1630 if (noside == EVAL_NORMAL)
1631 {
1632 const char *fieldname = item.first.c_str ();
1633 value *field = value_struct_elt (&result, {}, fieldname,
1634 nullptr, "structure");
1635 value_assign (field, val);
1636 }
1637 }
1638
1639 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1640 result = value::allocate (type);
1641 else
1642 result = value_at_lazy (type, addr);
1643
1644 return result;
1645 }
1646
1647 value *
1648 rust_structop::evaluate_funcall (struct type *expect_type,
1649 struct expression *exp,
1650 enum noside noside,
1651 const std::vector<operation_up> &ops)
1652 {
1653 std::vector<struct value *> args (ops.size () + 1);
1654
1655 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1656 type in order to look up the method. */
1657 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1658 /* We don't yet implement real Deref semantics. */
1659 while (args[0]->type ()->code () == TYPE_CODE_PTR)
1660 args[0] = value_ind (args[0]);
1661
1662 struct type *type = args[0]->type ();
1663 if ((type->code () != TYPE_CODE_STRUCT
1664 && type->code () != TYPE_CODE_UNION
1665 && type->code () != TYPE_CODE_ENUM)
1666 || rust_tuple_type_p (type))
1667 error (_("Method calls only supported on struct or enum types"));
1668 if (type->name () == NULL)
1669 error (_("Method call on nameless type"));
1670
1671 std::string name = (std::string (type->name ()) + "::"
1672 + std::get<1> (m_storage));
1673
1674 const struct block *block = get_selected_block (0);
1675 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1676 SEARCH_FUNCTION_DOMAIN,
1677 nullptr);
1678 if (sym.symbol == NULL)
1679 error (_("Could not find function named '%s'"), name.c_str ());
1680
1681 struct type *fn_type = sym.symbol->type ();
1682 if (fn_type->num_fields () == 0)
1683 error (_("Function '%s' takes no arguments"), name.c_str ());
1684
1685 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1686 args[0] = value_addr (args[0]);
1687
1688 value *function = address_of_variable (sym.symbol, block);
1689
1690 for (int i = 0; i < ops.size (); ++i)
1691 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1692
1693 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1694 return value::zero (fn_type->target_type (), not_lval);
1695 return call_function_by_hand (function, NULL, args);
1696 }
1697
1698 }
1699
1700
1701
1703 /* See language.h. */
1704
1705 void
1706 rust_language::language_arch_info (struct gdbarch *gdbarch,
1707 struct language_arch_info *lai) const
1708 {
1709 const struct builtin_type *builtin = builtin_type (gdbarch);
1710
1711 /* Helper function to allow shorter lines below. */
1712 auto add = [&] (struct type * t) -> struct type *
1713 {
1714 lai->add_primitive_type (t);
1715 return t;
1716 };
1717
1718 type_allocator alloc (gdbarch);
1719 struct type *bool_type
1720 = add (init_boolean_type (alloc, 8, 1, "bool"));
1721 add (init_character_type (alloc, 32, 1, "char"));
1722 add (init_integer_type (alloc, 8, 0, "i8"));
1723 struct type *u8_type
1724 = add (init_integer_type (alloc, 8, 1, "u8"));
1725 add (init_integer_type (alloc, 16, 0, "i16"));
1726 add (init_integer_type (alloc, 16, 1, "u16"));
1727 add (init_integer_type (alloc, 32, 0, "i32"));
1728 add (init_integer_type (alloc, 32, 1, "u32"));
1729 add (init_integer_type (alloc, 64, 0, "i64"));
1730 add (init_integer_type (alloc, 64, 1, "u64"));
1731 add (init_integer_type (alloc, 128, 0, "i128"));
1732 add (init_integer_type (alloc, 128, 1, "u128"));
1733
1734 unsigned int length = 8 * builtin->builtin_data_ptr->length ();
1735 add (init_integer_type (alloc, length, 0, "isize"));
1736 struct type *usize_type
1737 = add (init_integer_type (alloc, length, 1, "usize"));
1738
1739 add (init_float_type (alloc, 32, "f32", floatformats_ieee_single));
1740 add (init_float_type (alloc, 64, "f64", floatformats_ieee_double));
1741 add (init_integer_type (alloc, 0, 1, "()"));
1742
1743 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1744 add (rust_slice_type ("&str", tem, usize_type));
1745
1746 lai->set_bool_type (bool_type);
1747 lai->set_string_char_type (u8_type);
1748 }
1749
1750 /* See language.h. */
1751
1752 void
1753 rust_language::print_type (struct type *type, const char *varstring,
1754 struct ui_file *stream, int show, int level,
1755 const struct type_print_options *flags) const
1756 {
1757 print_offset_data podata (flags);
1758 rust_internal_print_type (type, varstring, stream, show, level,
1759 flags, false, &podata);
1760 }
1761
1762 /* See language.h. */
1763
1764 void
1765 rust_language::emitchar (int ch, struct type *chtype,
1766 struct ui_file *stream, int quoter) const
1767 {
1768 if (!rust_chartype_p (chtype))
1769 generic_emit_char (ch, chtype, stream, quoter,
1770 target_charset (chtype->arch ()));
1771 else if (ch == '\\' || ch == quoter)
1772 gdb_printf (stream, "\\%c", ch);
1773 else if (ch == '\n')
1774 gdb_puts ("\\n", stream);
1775 else if (ch == '\r')
1776 gdb_puts ("\\r", stream);
1777 else if (ch == '\t')
1778 gdb_puts ("\\t", stream);
1779 else if (ch == '\0')
1780 gdb_puts ("\\0", stream);
1781 else if (ch >= 32 && ch <= 127 && isprint ((unsigned char)ch))
1782 gdb_putc (ch, stream);
1783 else if (ch <= 255)
1784 gdb_printf (stream, "\\x%02x", ch);
1785 else
1786 gdb_printf (stream, "\\u{%06x}", ch);
1787 }
1788
1789 /* See language.h. */
1790
1791 bool
1792 rust_language::is_array_like (struct type *type) const
1793 {
1794 if (!rust_slice_type_p (type))
1795 return false;
1796 return rust_array_like_element_type (type) != nullptr;
1797 }
1798
1799 /* See language.h. */
1800
1801 bool
1802 rust_language::is_string_type_p (struct type *type) const
1803 {
1804 LONGEST low_bound, high_bound;
1805
1806 type = check_typedef (type);
1807 return ((type->code () == TYPE_CODE_STRING)
1808 || (type->code () == TYPE_CODE_PTR
1809 && (type->target_type ()->code () == TYPE_CODE_ARRAY
1810 && rust_u8_type_p (type->target_type ()->target_type ())
1811 && get_array_bounds (type->target_type (), &low_bound,
1812 &high_bound)))
1813 || (type->code () == TYPE_CODE_STRUCT
1814 && !rust_enum_p (type)
1815 && rust_slice_type_p (type)
1816 && strcmp (type->name (), "&str") == 0));
1817 }
1818
1819 /* See language.h. */
1820
1821 struct block_symbol
1822 rust_language::lookup_symbol_nonlocal
1823 (const char *name, const struct block *block,
1824 const domain_search_flags domain) const
1825 {
1826 struct block_symbol result = {};
1827
1828 const char *scope = block == nullptr ? "" : block->scope ();
1829 symbol_lookup_debug_printf
1830 ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
1831 name, host_address_to_string (block), scope,
1832 domain_name (domain).c_str ());
1833
1834 /* Look up bare names in the block's scope. */
1835 std::string scopedname;
1836 if (name[cp_find_first_component (name)] == '\0')
1837 {
1838 if (scope[0] != '\0')
1839 {
1840 scopedname = std::string (scope) + "::" + name;
1841 name = scopedname.c_str ();
1842 }
1843 else
1844 name = NULL;
1845 }
1846
1847 if (name != NULL)
1848 {
1849 result = lookup_symbol_in_static_block (name, block, domain);
1850 if (result.symbol == NULL)
1851 result = lookup_global_symbol (name, block, domain);
1852 }
1853 return result;
1854 }
1855
1856 /* Single instance of the Rust language class. */
1857
1858 static rust_language rust_language_defn;
1859