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