]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/rust-lang.c
gdb: Convert language la_emitchar field to a method
[thirdparty/binutils-gdb.git] / gdb / rust-lang.c
1 /* Rust language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2020 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 #include "defs.h"
21
22 #include <ctype.h>
23
24 #include "block.h"
25 #include "c-lang.h"
26 #include "charset.h"
27 #include "cp-support.h"
28 #include "demangle.h"
29 #include "gdbarch.h"
30 #include "infcall.h"
31 #include "objfiles.h"
32 #include "psymtab.h"
33 #include "rust-lang.h"
34 #include "typeprint.h"
35 #include "valprint.h"
36 #include "varobj.h"
37 #include <algorithm>
38 #include <string>
39 #include <vector>
40 #include "cli/cli-style.h"
41
42 /* See rust-lang.h. */
43
44 const char *
45 rust_last_path_segment (const char *path)
46 {
47 const char *result = strrchr (path, ':');
48
49 if (result == NULL)
50 return path;
51 return result + 1;
52 }
53
54 /* See rust-lang.h. */
55
56 std::string
57 rust_crate_for_block (const struct block *block)
58 {
59 const char *scope = block_scope (block);
60
61 if (scope[0] == '\0')
62 return std::string ();
63
64 return std::string (scope, cp_find_first_component (scope));
65 }
66
67 /* Return true if TYPE, which must be a struct type, represents a Rust
68 enum. */
69
70 static bool
71 rust_enum_p (struct type *type)
72 {
73 /* is_dynamic_type will return true if any field has a dynamic
74 attribute -- but we only want to check the top level. */
75 return TYPE_HAS_VARIANT_PARTS (type);
76 }
77
78 /* Return true if TYPE, which must be an already-resolved enum type,
79 has no variants. */
80
81 static bool
82 rust_empty_enum_p (const struct type *type)
83 {
84 return type->num_fields () == 0;
85 }
86
87 /* Given an already-resolved enum type and contents, find which
88 variant is active. */
89
90 static int
91 rust_enum_variant (struct type *type)
92 {
93 /* The active variant is simply the first non-artificial field. */
94 for (int i = 0; i < type->num_fields (); ++i)
95 if (!TYPE_FIELD_ARTIFICIAL (type, i))
96 return i;
97
98 /* Perhaps we could get here by trying to print an Ada variant
99 record in Rust mode. Unlikely, but an error is safer than an
100 assert. */
101 error (_("Could not find active enum variant"));
102 }
103
104 /* See rust-lang.h. */
105
106 bool
107 rust_tuple_type_p (struct type *type)
108 {
109 /* The current implementation is a bit of a hack, but there's
110 nothing else in the debuginfo to distinguish a tuple from a
111 struct. */
112 return (type->code () == TYPE_CODE_STRUCT
113 && type->name () != NULL
114 && type->name ()[0] == '(');
115 }
116
117 /* Return true if all non-static fields of a structlike type are in a
118 sequence like __0, __1, __2. */
119
120 static bool
121 rust_underscore_fields (struct type *type)
122 {
123 int i, field_number;
124
125 field_number = 0;
126
127 if (type->code () != TYPE_CODE_STRUCT)
128 return false;
129 for (i = 0; i < type->num_fields (); ++i)
130 {
131 if (!field_is_static (&type->field (i)))
132 {
133 char buf[20];
134
135 xsnprintf (buf, sizeof (buf), "__%d", field_number);
136 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
137 return false;
138 field_number++;
139 }
140 }
141 return true;
142 }
143
144 /* See rust-lang.h. */
145
146 bool
147 rust_tuple_struct_type_p (struct type *type)
148 {
149 /* This is just an approximation until DWARF can represent Rust more
150 precisely. We exclude zero-length structs because they may not
151 be tuple structs, and there's no way to tell. */
152 return type->num_fields () > 0 && rust_underscore_fields (type);
153 }
154
155 /* Return true if TYPE is a slice type, otherwise false. */
156
157 static bool
158 rust_slice_type_p (struct type *type)
159 {
160 return (type->code () == TYPE_CODE_STRUCT
161 && type->name () != NULL
162 && (strncmp (type->name (), "&[", 2) == 0
163 || strcmp (type->name (), "&str") == 0));
164 }
165
166 /* Return true if TYPE is a range type, otherwise false. */
167
168 static bool
169 rust_range_type_p (struct type *type)
170 {
171 int i;
172
173 if (type->code () != TYPE_CODE_STRUCT
174 || type->num_fields () > 2
175 || type->name () == NULL
176 || strstr (type->name (), "::Range") == NULL)
177 return false;
178
179 if (type->num_fields () == 0)
180 return true;
181
182 i = 0;
183 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
184 {
185 if (type->num_fields () == 1)
186 return true;
187 i = 1;
188 }
189 else if (type->num_fields () == 2)
190 {
191 /* First field had to be "start". */
192 return false;
193 }
194
195 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
196 }
197
198 /* Return true if TYPE is an inclusive range type, otherwise false.
199 This is only valid for types which are already known to be range
200 types. */
201
202 static bool
203 rust_inclusive_range_type_p (struct type *type)
204 {
205 return (strstr (type->name (), "::RangeInclusive") != NULL
206 || strstr (type->name (), "::RangeToInclusive") != NULL);
207 }
208
209 /* Return true if TYPE seems to be the type "u8", otherwise false. */
210
211 static bool
212 rust_u8_type_p (struct type *type)
213 {
214 return (type->code () == TYPE_CODE_INT
215 && TYPE_UNSIGNED (type)
216 && TYPE_LENGTH (type) == 1);
217 }
218
219 /* Return true if TYPE is a Rust character type. */
220
221 static bool
222 rust_chartype_p (struct type *type)
223 {
224 return (type->code () == TYPE_CODE_CHAR
225 && TYPE_LENGTH (type) == 4
226 && TYPE_UNSIGNED (type));
227 }
228
229 /* Return true if TYPE is a string type. */
230
231 static bool
232 rust_is_string_type_p (struct type *type)
233 {
234 LONGEST low_bound, high_bound;
235
236 type = check_typedef (type);
237 return ((type->code () == TYPE_CODE_STRING)
238 || (type->code () == TYPE_CODE_PTR
239 && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
240 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
241 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
242 &high_bound)))
243 || (type->code () == TYPE_CODE_STRUCT
244 && !rust_enum_p (type)
245 && rust_slice_type_p (type)
246 && strcmp (type->name (), "&str") == 0));
247 }
248
249 /* If VALUE represents a trait object pointer, return the underlying
250 pointer with the correct (i.e., runtime) type. Otherwise, return
251 NULL. */
252
253 static struct value *
254 rust_get_trait_object_pointer (struct value *value)
255 {
256 struct type *type = check_typedef (value_type (value));
257
258 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
259 return NULL;
260
261 /* Try to be a bit resilient if the ABI changes. */
262 int vtable_field = 0;
263 for (int i = 0; i < 2; ++i)
264 {
265 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
266 vtable_field = i;
267 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
268 return NULL;
269 }
270
271 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
272 struct symbol *symbol = find_symbol_at_address (vtable);
273 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
274 return NULL;
275
276 struct rust_vtable_symbol *vtable_sym
277 = static_cast<struct rust_vtable_symbol *> (symbol);
278 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
279 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
280 }
281
282 \f
283
284 /* la_printchar implementation for Rust. */
285
286 static void
287 rust_printchar (int c, struct type *type, struct ui_file *stream)
288 {
289 fputs_filtered ("'", stream);
290 LA_EMIT_CHAR (c, type, stream, '\'');
291 fputs_filtered ("'", stream);
292 }
293
294 /* la_printstr implementation for Rust. */
295
296 static void
297 rust_printstr (struct ui_file *stream, struct type *type,
298 const gdb_byte *string, unsigned int length,
299 const char *user_encoding, int force_ellipses,
300 const struct value_print_options *options)
301 {
302 /* Rust always uses UTF-8, but let the caller override this if need
303 be. */
304 const char *encoding = user_encoding;
305 if (user_encoding == NULL || !*user_encoding)
306 {
307 /* In Rust strings, characters are "u8". */
308 if (rust_u8_type_p (type))
309 encoding = "UTF-8";
310 else
311 {
312 /* This is probably some C string, so let's let C deal with
313 it. */
314 c_printstr (stream, type, string, length, user_encoding,
315 force_ellipses, options);
316 return;
317 }
318 }
319
320 /* This is not ideal as it doesn't use our character printer. */
321 generic_printstr (stream, type, string, length, encoding, force_ellipses,
322 '"', 0, options);
323 }
324
325 \f
326
327 static void rust_value_print_inner (struct value *val, struct ui_file *stream,
328 int recurse,
329 const struct value_print_options *options);
330
331 /* Helper function to print a string slice. */
332
333 static void
334 rust_val_print_str (struct ui_file *stream, struct value *val,
335 const struct value_print_options *options)
336 {
337 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
338 "slice");
339 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
340
341 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
342 value_as_address (base), value_as_long (len), stream,
343 options);
344 }
345
346 /* rust_val_print helper for structs and untagged unions. */
347
348 static void
349 val_print_struct (struct value *val, struct ui_file *stream, int recurse,
350 const struct value_print_options *options)
351 {
352 int i;
353 int first_field;
354 struct type *type = check_typedef (value_type (val));
355
356 if (rust_slice_type_p (type) && strcmp (type->name (), "&str") == 0)
357 {
358 /* If what we are printing here is actually a string within a
359 structure then VAL will be the original parent value, while TYPE
360 will be the type of the structure representing the string we want
361 to print.
362 However, RUST_VAL_PRINT_STR looks up the fields of the string
363 inside VAL, assuming that VAL is the string.
364 So, recreate VAL as a value representing just the string. */
365 val = value_at_lazy (type, value_address (val));
366 rust_val_print_str (stream, val, options);
367 return;
368 }
369
370 bool is_tuple = rust_tuple_type_p (type);
371 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
372 struct value_print_options opts;
373
374 if (!is_tuple)
375 {
376 if (type->name () != NULL)
377 fprintf_filtered (stream, "%s", type->name ());
378
379 if (type->num_fields () == 0)
380 return;
381
382 if (type->name () != NULL)
383 fputs_filtered (" ", stream);
384 }
385
386 if (is_tuple || is_tuple_struct)
387 fputs_filtered ("(", stream);
388 else
389 fputs_filtered ("{", stream);
390
391 opts = *options;
392 opts.deref_ref = 0;
393
394 first_field = 1;
395 for (i = 0; i < type->num_fields (); ++i)
396 {
397 if (field_is_static (&type->field (i)))
398 continue;
399
400 if (!first_field)
401 fputs_filtered (",", stream);
402
403 if (options->prettyformat)
404 {
405 fputs_filtered ("\n", stream);
406 print_spaces_filtered (2 + 2 * recurse, stream);
407 }
408 else if (!first_field)
409 fputs_filtered (" ", stream);
410
411 first_field = 0;
412
413 if (!is_tuple && !is_tuple_struct)
414 {
415 fputs_styled (TYPE_FIELD_NAME (type, i),
416 variable_name_style.style (), stream);
417 fputs_filtered (": ", stream);
418 }
419
420 rust_value_print_inner (value_field (val, i), stream, recurse + 1,
421 &opts);
422 }
423
424 if (options->prettyformat)
425 {
426 fputs_filtered ("\n", stream);
427 print_spaces_filtered (2 * recurse, stream);
428 }
429
430 if (is_tuple || is_tuple_struct)
431 fputs_filtered (")", stream);
432 else
433 fputs_filtered ("}", stream);
434 }
435
436 /* rust_val_print helper for discriminated unions (Rust enums). */
437
438 static void
439 rust_print_enum (struct value *val, struct ui_file *stream, int recurse,
440 const struct value_print_options *options)
441 {
442 struct value_print_options opts = *options;
443 struct type *type = check_typedef (value_type (val));
444
445 opts.deref_ref = 0;
446
447 gdb_assert (rust_enum_p (type));
448 gdb::array_view<const gdb_byte> view (value_contents_for_printing (val),
449 TYPE_LENGTH (value_type (val)));
450 type = resolve_dynamic_type (type, view, value_address (val));
451
452 if (rust_empty_enum_p (type))
453 {
454 /* Print the enum type name here to be more clear. */
455 fprintf_filtered (stream, _("%s {%p[<No data fields>%p]}"),
456 type->name (),
457 metadata_style.style ().ptr (), nullptr);
458 return;
459 }
460
461 int variant_fieldno = rust_enum_variant (type);
462 val = value_field (val, variant_fieldno);
463 struct type *variant_type = type->field (variant_fieldno).type ();
464
465 int nfields = variant_type->num_fields ();
466
467 bool is_tuple = rust_tuple_struct_type_p (variant_type);
468
469 fprintf_filtered (stream, "%s", variant_type->name ());
470 if (nfields == 0)
471 {
472 /* In case of a nullary variant like 'None', just output
473 the name. */
474 return;
475 }
476
477 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
478 if (is_tuple)
479 fprintf_filtered (stream, "(");
480 else
481 {
482 /* struct variant. */
483 fprintf_filtered (stream, "{");
484 }
485
486 bool first_field = true;
487 for (int j = 0; j < variant_type->num_fields (); j++)
488 {
489 if (!first_field)
490 fputs_filtered (", ", stream);
491 first_field = false;
492
493 if (!is_tuple)
494 fprintf_filtered (stream, "%ps: ",
495 styled_string (variable_name_style.style (),
496 TYPE_FIELD_NAME (variant_type, j)));
497
498 rust_value_print_inner (value_field (val, j), stream, recurse + 1,
499 &opts);
500 }
501
502 if (is_tuple)
503 fputs_filtered (")", stream);
504 else
505 fputs_filtered ("}", stream);
506 }
507
508 static const struct generic_val_print_decorations rust_decorations =
509 {
510 /* Complex isn't used in Rust, but we provide C-ish values just in
511 case. */
512 "",
513 " + ",
514 " * I",
515 "true",
516 "false",
517 "()",
518 "[",
519 "]"
520 };
521
522 /* la_value_print_inner implementation for Rust. */
523 static void
524 rust_value_print_inner (struct value *val, struct ui_file *stream,
525 int recurse,
526 const struct value_print_options *options)
527 {
528 struct value_print_options opts = *options;
529 opts.deref_ref = 1;
530
531 if (opts.prettyformat == Val_prettyformat_default)
532 opts.prettyformat = (opts.prettyformat_structs
533 ? Val_prettyformat : Val_no_prettyformat);
534
535 struct type *type = check_typedef (value_type (val));
536 switch (type->code ())
537 {
538 case TYPE_CODE_PTR:
539 {
540 LONGEST low_bound, high_bound;
541
542 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
543 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
544 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
545 &high_bound))
546 {
547 /* We have a pointer to a byte string, so just print
548 that. */
549 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
550 CORE_ADDR addr = value_as_address (val);
551 struct gdbarch *arch = get_type_arch (type);
552
553 if (opts.addressprint)
554 {
555 fputs_filtered (paddress (arch, addr), stream);
556 fputs_filtered (" ", stream);
557 }
558
559 fputs_filtered ("b", stream);
560 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
561 high_bound - low_bound + 1, stream,
562 &opts);
563 break;
564 }
565 }
566 goto generic_print;
567
568 case TYPE_CODE_METHODPTR:
569 case TYPE_CODE_MEMBERPTR:
570 c_value_print_inner (val, stream, recurse, &opts);
571 break;
572
573 case TYPE_CODE_INT:
574 /* Recognize the unit type. */
575 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
576 && type->name () != NULL && strcmp (type->name (), "()") == 0)
577 {
578 fputs_filtered ("()", stream);
579 break;
580 }
581 goto generic_print;
582
583 case TYPE_CODE_STRING:
584 {
585 LONGEST low_bound, high_bound;
586
587 if (!get_array_bounds (type, &low_bound, &high_bound))
588 error (_("Could not determine the array bounds"));
589
590 /* If we see a plain TYPE_CODE_STRING, then we're printing a
591 byte string, hence the choice of "ASCII" as the
592 encoding. */
593 fputs_filtered ("b", stream);
594 rust_printstr (stream, TYPE_TARGET_TYPE (type),
595 value_contents_for_printing (val),
596 high_bound - low_bound + 1, "ASCII", 0, &opts);
597 }
598 break;
599
600 case TYPE_CODE_ARRAY:
601 {
602 LONGEST low_bound, high_bound;
603
604 if (get_array_bounds (type, &low_bound, &high_bound)
605 && high_bound - low_bound + 1 == 0)
606 fputs_filtered ("[]", stream);
607 else
608 goto generic_print;
609 }
610 break;
611
612 case TYPE_CODE_UNION:
613 /* Untagged unions are printed as if they are structs. Since
614 the field bit positions overlap in the debuginfo, the code
615 for printing a union is same as that for a struct, the only
616 difference is that the input type will have overlapping
617 fields. */
618 val_print_struct (val, stream, recurse, &opts);
619 break;
620
621 case TYPE_CODE_STRUCT:
622 if (rust_enum_p (type))
623 rust_print_enum (val, stream, recurse, &opts);
624 else
625 val_print_struct (val, stream, recurse, &opts);
626 break;
627
628 default:
629 generic_print:
630 /* Nothing special yet. */
631 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
632 }
633 }
634
635 \f
636
637 static void
638 rust_internal_print_type (struct type *type, const char *varstring,
639 struct ui_file *stream, int show, int level,
640 const struct type_print_options *flags,
641 bool for_rust_enum, print_offset_data *podata);
642
643 /* Print a struct or union typedef. */
644 static void
645 rust_print_struct_def (struct type *type, const char *varstring,
646 struct ui_file *stream, int show, int level,
647 const struct type_print_options *flags,
648 bool for_rust_enum, print_offset_data *podata)
649 {
650 /* Print a tuple type simply. */
651 if (rust_tuple_type_p (type))
652 {
653 fputs_filtered (type->name (), stream);
654 return;
655 }
656
657 /* If we see a base class, delegate to C. */
658 if (TYPE_N_BASECLASSES (type) > 0)
659 c_print_type (type, varstring, stream, show, level, flags);
660
661 if (flags->print_offsets)
662 {
663 /* Temporarily bump the level so that the output lines up
664 correctly. */
665 level += 2;
666 }
667
668 /* Compute properties of TYPE here because, in the enum case, the
669 rest of the code ends up looking only at the variant part. */
670 const char *tagname = type->name ();
671 bool is_tuple_struct = rust_tuple_struct_type_p (type);
672 bool is_tuple = rust_tuple_type_p (type);
673 bool is_enum = rust_enum_p (type);
674
675 if (for_rust_enum)
676 {
677 /* Already printing an outer enum, so nothing to print here. */
678 }
679 else
680 {
681 /* This code path is also used by unions and enums. */
682 if (is_enum)
683 {
684 fputs_filtered ("enum ", stream);
685 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
686 if (prop != nullptr && prop->kind == PROP_TYPE)
687 type = prop->data.original_type;
688 }
689 else if (type->code () == TYPE_CODE_STRUCT)
690 fputs_filtered ("struct ", stream);
691 else
692 fputs_filtered ("union ", stream);
693
694 if (tagname != NULL)
695 fputs_filtered (tagname, stream);
696 }
697
698 if (type->num_fields () == 0 && !is_tuple)
699 return;
700 if (for_rust_enum && !flags->print_offsets)
701 fputs_filtered (is_tuple_struct ? "(" : "{", stream);
702 else
703 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
704
705 /* When printing offsets, we rearrange the fields into storage
706 order. This lets us show holes more clearly. We work using
707 field indices here because it simplifies calls to
708 print_offset_data::update below. */
709 std::vector<int> fields;
710 for (int i = 0; i < type->num_fields (); ++i)
711 {
712 if (field_is_static (&type->field (i)))
713 continue;
714 if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
715 continue;
716 fields.push_back (i);
717 }
718 if (flags->print_offsets)
719 std::sort (fields.begin (), fields.end (),
720 [&] (int a, int b)
721 {
722 return (TYPE_FIELD_BITPOS (type, a)
723 < TYPE_FIELD_BITPOS (type, b));
724 });
725
726 for (int i : fields)
727 {
728 QUIT;
729
730 gdb_assert (!field_is_static (&type->field (i)));
731 gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
732
733 if (flags->print_offsets)
734 podata->update (type, i, stream);
735
736 /* We'd like to print "pub" here as needed, but rustc
737 doesn't emit the debuginfo, and our types don't have
738 cplus_struct_type attached. */
739
740 /* For a tuple struct we print the type but nothing
741 else. */
742 if (!for_rust_enum || flags->print_offsets)
743 print_spaces_filtered (level + 2, stream);
744 if (is_enum)
745 fputs_styled (TYPE_FIELD_NAME (type, i), variable_name_style.style (),
746 stream);
747 else if (!is_tuple_struct)
748 fprintf_filtered (stream, "%ps: ",
749 styled_string (variable_name_style.style (),
750 TYPE_FIELD_NAME (type, i)));
751
752 rust_internal_print_type (type->field (i).type (), NULL,
753 stream, (is_enum ? show : show - 1),
754 level + 2, flags, is_enum, podata);
755 if (!for_rust_enum || flags->print_offsets)
756 fputs_filtered (",\n", stream);
757 /* Note that this check of "I" is ok because we only sorted the
758 fields by offset when print_offsets was set, so we won't take
759 this branch in that case. */
760 else if (i + 1 < type->num_fields ())
761 fputs_filtered (", ", stream);
762 }
763
764 if (flags->print_offsets)
765 {
766 /* Undo the temporary level increase we did above. */
767 level -= 2;
768 podata->finish (type, level, stream);
769 print_spaces_filtered (print_offset_data::indentation, stream);
770 if (level == 0)
771 print_spaces_filtered (2, stream);
772 }
773 if (!for_rust_enum || flags->print_offsets)
774 print_spaces_filtered (level, stream);
775 fputs_filtered (is_tuple_struct ? ")" : "}", stream);
776 }
777
778 /* la_print_typedef implementation for Rust. */
779
780 static void
781 rust_print_typedef (struct type *type,
782 struct symbol *new_symbol,
783 struct ui_file *stream)
784 {
785 type = check_typedef (type);
786 fprintf_filtered (stream, "type %s = ", new_symbol->print_name ());
787 type_print (type, "", stream, 0);
788 fprintf_filtered (stream, ";");
789 }
790
791 /* la_print_type implementation for Rust. */
792
793 static void
794 rust_internal_print_type (struct type *type, const char *varstring,
795 struct ui_file *stream, int show, int level,
796 const struct type_print_options *flags,
797 bool for_rust_enum, print_offset_data *podata)
798 {
799 QUIT;
800 if (show <= 0
801 && type->name () != NULL)
802 {
803 /* Rust calls the unit type "void" in its debuginfo,
804 but we don't want to print it as that. */
805 if (type->code () == TYPE_CODE_VOID)
806 fputs_filtered ("()", stream);
807 else
808 fputs_filtered (type->name (), stream);
809 return;
810 }
811
812 type = check_typedef (type);
813 switch (type->code ())
814 {
815 case TYPE_CODE_VOID:
816 /* If we have an enum, we've already printed the type's
817 unqualified name, and there is nothing else to print
818 here. */
819 if (!for_rust_enum)
820 fputs_filtered ("()", stream);
821 break;
822
823 case TYPE_CODE_FUNC:
824 /* Delegate varargs to the C printer. */
825 if (TYPE_VARARGS (type))
826 goto c_printer;
827
828 fputs_filtered ("fn ", stream);
829 if (varstring != NULL)
830 fputs_filtered (varstring, stream);
831 fputs_filtered ("(", stream);
832 for (int i = 0; i < type->num_fields (); ++i)
833 {
834 QUIT;
835 if (i > 0)
836 fputs_filtered (", ", stream);
837 rust_internal_print_type (type->field (i).type (), "", stream,
838 -1, 0, flags, false, podata);
839 }
840 fputs_filtered (")", stream);
841 /* If it returns unit, we can omit the return type. */
842 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
843 {
844 fputs_filtered (" -> ", stream);
845 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
846 -1, 0, flags, false, podata);
847 }
848 break;
849
850 case TYPE_CODE_ARRAY:
851 {
852 LONGEST low_bound, high_bound;
853
854 fputs_filtered ("[", stream);
855 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
856 stream, show - 1, level, flags, false,
857 podata);
858
859 if (TYPE_HIGH_BOUND_KIND (type->index_type ()) == PROP_LOCEXPR
860 || TYPE_HIGH_BOUND_KIND (type->index_type ()) == PROP_LOCLIST)
861 fprintf_filtered (stream, "; variable length");
862 else if (get_array_bounds (type, &low_bound, &high_bound))
863 fprintf_filtered (stream, "; %s",
864 plongest (high_bound - low_bound + 1));
865 fputs_filtered ("]", stream);
866 }
867 break;
868
869 case TYPE_CODE_UNION:
870 case TYPE_CODE_STRUCT:
871 rust_print_struct_def (type, varstring, stream, show, level, flags,
872 for_rust_enum, podata);
873 break;
874
875 case TYPE_CODE_ENUM:
876 {
877 int len = 0;
878
879 fputs_filtered ("enum ", stream);
880 if (type->name () != NULL)
881 {
882 fputs_filtered (type->name (), stream);
883 fputs_filtered (" ", stream);
884 len = strlen (type->name ());
885 }
886 fputs_filtered ("{\n", stream);
887
888 for (int i = 0; i < type->num_fields (); ++i)
889 {
890 const char *name = TYPE_FIELD_NAME (type, i);
891
892 QUIT;
893
894 if (len > 0
895 && strncmp (name, type->name (), len) == 0
896 && name[len] == ':'
897 && name[len + 1] == ':')
898 name += len + 2;
899 fprintfi_filtered (level + 2, stream, "%ps,\n",
900 styled_string (variable_name_style.style (),
901 name));
902 }
903
904 fputs_filtered ("}", stream);
905 }
906 break;
907
908 case TYPE_CODE_PTR:
909 {
910 if (type->name () != nullptr)
911 fputs_filtered (type->name (), stream);
912 else
913 {
914 /* We currently can't distinguish between pointers and
915 references. */
916 fputs_filtered ("*mut ", stream);
917 type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
918 }
919 }
920 break;
921
922 default:
923 c_printer:
924 c_print_type (type, varstring, stream, show, level, flags);
925 }
926 }
927
928 \f
929
930 /* Like arch_composite_type, but uses TYPE to decide how to allocate
931 -- either on an obstack or on a gdbarch. */
932
933 static struct type *
934 rust_composite_type (struct type *original,
935 const char *name,
936 const char *field1, struct type *type1,
937 const char *field2, struct type *type2)
938 {
939 struct type *result = alloc_type_copy (original);
940 int i, nfields, bitpos;
941
942 nfields = 0;
943 if (field1 != NULL)
944 ++nfields;
945 if (field2 != NULL)
946 ++nfields;
947
948 result->set_code (TYPE_CODE_STRUCT);
949 result->set_name (name);
950
951 result->set_num_fields (nfields);
952 result->set_fields
953 ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
954
955 i = 0;
956 bitpos = 0;
957 if (field1 != NULL)
958 {
959 struct field *field = &result->field (i);
960
961 SET_FIELD_BITPOS (*field, bitpos);
962 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
963
964 FIELD_NAME (*field) = field1;
965 field->set_type (type1);
966 ++i;
967 }
968 if (field2 != NULL)
969 {
970 struct field *field = &result->field (i);
971 unsigned align = type_align (type2);
972
973 if (align != 0)
974 {
975 int delta;
976
977 align *= TARGET_CHAR_BIT;
978 delta = bitpos % align;
979 if (delta != 0)
980 bitpos += align - delta;
981 }
982 SET_FIELD_BITPOS (*field, bitpos);
983
984 FIELD_NAME (*field) = field2;
985 field->set_type (type2);
986 ++i;
987 }
988
989 if (i > 0)
990 TYPE_LENGTH (result)
991 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
992 TYPE_LENGTH (result->field (i - 1).type ()));
993 return result;
994 }
995
996 /* See rust-lang.h. */
997
998 struct type *
999 rust_slice_type (const char *name, struct type *elt_type,
1000 struct type *usize_type)
1001 {
1002 struct type *type;
1003
1004 elt_type = lookup_pointer_type (elt_type);
1005 type = rust_composite_type (elt_type, name,
1006 "data_ptr", elt_type,
1007 "length", usize_type);
1008
1009 return type;
1010 }
1011
1012 enum rust_primitive_types
1013 {
1014 rust_primitive_bool,
1015 rust_primitive_char,
1016 rust_primitive_i8,
1017 rust_primitive_u8,
1018 rust_primitive_i16,
1019 rust_primitive_u16,
1020 rust_primitive_i32,
1021 rust_primitive_u32,
1022 rust_primitive_i64,
1023 rust_primitive_u64,
1024 rust_primitive_isize,
1025 rust_primitive_usize,
1026 rust_primitive_f32,
1027 rust_primitive_f64,
1028 rust_primitive_unit,
1029 rust_primitive_str,
1030 nr_rust_primitive_types
1031 };
1032
1033 \f
1034
1035 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1036
1037 static struct value *
1038 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1039 {
1040 int i;
1041 int num_args = exp->elts[*pos + 1].longconst;
1042 const char *method;
1043 struct value *function, *result, *arg0;
1044 struct type *type, *fn_type;
1045 const struct block *block;
1046 struct block_symbol sym;
1047
1048 /* For an ordinary function call we can simply defer to the
1049 generic implementation. */
1050 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1051 return evaluate_subexp_standard (NULL, exp, pos, noside);
1052
1053 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1054 *pos += 4;
1055 method = &exp->elts[*pos + 1].string;
1056 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1057
1058 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1059 type in order to look up the method. */
1060 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1061
1062 if (noside == EVAL_SKIP)
1063 {
1064 for (i = 0; i < num_args; ++i)
1065 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1066 return arg0;
1067 }
1068
1069 std::vector<struct value *> args (num_args + 1);
1070 args[0] = arg0;
1071
1072 /* We don't yet implement real Deref semantics. */
1073 while (value_type (args[0])->code () == TYPE_CODE_PTR)
1074 args[0] = value_ind (args[0]);
1075
1076 type = value_type (args[0]);
1077 if ((type->code () != TYPE_CODE_STRUCT
1078 && type->code () != TYPE_CODE_UNION
1079 && type->code () != TYPE_CODE_ENUM)
1080 || rust_tuple_type_p (type))
1081 error (_("Method calls only supported on struct or enum types"));
1082 if (type->name () == NULL)
1083 error (_("Method call on nameless type"));
1084
1085 std::string name = std::string (type->name ()) + "::" + method;
1086
1087 block = get_selected_block (0);
1088 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1089 if (sym.symbol == NULL)
1090 error (_("Could not find function named '%s'"), name.c_str ());
1091
1092 fn_type = SYMBOL_TYPE (sym.symbol);
1093 if (fn_type->num_fields () == 0)
1094 error (_("Function '%s' takes no arguments"), name.c_str ());
1095
1096 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1097 args[0] = value_addr (args[0]);
1098
1099 function = address_of_variable (sym.symbol, block);
1100
1101 for (i = 0; i < num_args; ++i)
1102 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1103
1104 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1105 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1106 else
1107 result = call_function_by_hand (function, NULL, args);
1108 return result;
1109 }
1110
1111 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1112
1113 static struct value *
1114 rust_range (struct expression *exp, int *pos, enum noside noside)
1115 {
1116 enum range_type kind;
1117 struct value *low = NULL, *high = NULL;
1118 struct value *addrval, *result;
1119 CORE_ADDR addr;
1120 struct type *range_type;
1121 struct type *index_type;
1122 struct type *temp_type;
1123 const char *name;
1124
1125 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1126 *pos += 3;
1127
1128 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
1129 || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1130 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1131 if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
1132 || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1133 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1134 bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
1135
1136 if (noside == EVAL_SKIP)
1137 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1138
1139 if (low == NULL)
1140 {
1141 if (high == NULL)
1142 {
1143 index_type = NULL;
1144 name = "std::ops::RangeFull";
1145 }
1146 else
1147 {
1148 index_type = value_type (high);
1149 name = (inclusive
1150 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1151 }
1152 }
1153 else
1154 {
1155 if (high == NULL)
1156 {
1157 index_type = value_type (low);
1158 name = "std::ops::RangeFrom";
1159 }
1160 else
1161 {
1162 if (!types_equal (value_type (low), value_type (high)))
1163 error (_("Range expression with different types"));
1164 index_type = value_type (low);
1165 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1166 }
1167 }
1168
1169 /* If we don't have an index type, just allocate this on the
1170 arch. Here any type will do. */
1171 temp_type = (index_type == NULL
1172 ? language_bool_type (exp->language_defn, exp->gdbarch)
1173 : index_type);
1174 /* It would be nicer to cache the range type. */
1175 range_type = rust_composite_type (temp_type, name,
1176 low == NULL ? NULL : "start", index_type,
1177 high == NULL ? NULL : "end", index_type);
1178
1179 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1180 return value_zero (range_type, lval_memory);
1181
1182 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1183 addr = value_as_long (addrval);
1184 result = value_at_lazy (range_type, addr);
1185
1186 if (low != NULL)
1187 {
1188 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1189 "range");
1190
1191 value_assign (start, low);
1192 }
1193
1194 if (high != NULL)
1195 {
1196 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1197 "range");
1198
1199 value_assign (end, high);
1200 }
1201
1202 result = value_at_lazy (range_type, addr);
1203 return result;
1204 }
1205
1206 /* A helper function to compute the range and kind given a range
1207 value. TYPE is the type of the range value. RANGE is the range
1208 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1209 parameters might be filled in, or might not be, depending on the
1210 kind of range this is. KIND will always be set to the appropriate
1211 value describing the kind of range, and this can be used to
1212 determine whether LOW or HIGH are valid. */
1213
1214 static void
1215 rust_compute_range (struct type *type, struct value *range,
1216 LONGEST *low, LONGEST *high,
1217 enum range_type *kind)
1218 {
1219 int i;
1220
1221 *low = 0;
1222 *high = 0;
1223 *kind = BOTH_BOUND_DEFAULT;
1224
1225 if (type->num_fields () == 0)
1226 return;
1227
1228 i = 0;
1229 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1230 {
1231 *kind = HIGH_BOUND_DEFAULT;
1232 *low = value_as_long (value_field (range, 0));
1233 ++i;
1234 }
1235 if (type->num_fields () > i
1236 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1237 {
1238 *kind = (*kind == BOTH_BOUND_DEFAULT
1239 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1240 *high = value_as_long (value_field (range, i));
1241
1242 if (rust_inclusive_range_type_p (type))
1243 ++*high;
1244 }
1245 }
1246
1247 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1248
1249 static struct value *
1250 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1251 int for_addr)
1252 {
1253 struct value *lhs, *rhs, *result;
1254 struct type *rhstype;
1255 LONGEST low, high_bound;
1256 /* Initialized to appease the compiler. */
1257 enum range_type kind = BOTH_BOUND_DEFAULT;
1258 LONGEST high = 0;
1259 int want_slice = 0;
1260
1261 ++*pos;
1262 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1263 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1264
1265 if (noside == EVAL_SKIP)
1266 return lhs;
1267
1268 rhstype = check_typedef (value_type (rhs));
1269 if (rust_range_type_p (rhstype))
1270 {
1271 if (!for_addr)
1272 error (_("Can't take slice of array without '&'"));
1273 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1274 want_slice = 1;
1275 }
1276 else
1277 low = value_as_long (rhs);
1278
1279 struct type *type = check_typedef (value_type (lhs));
1280 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1281 {
1282 struct type *base_type = nullptr;
1283 if (type->code () == TYPE_CODE_ARRAY)
1284 base_type = TYPE_TARGET_TYPE (type);
1285 else if (rust_slice_type_p (type))
1286 {
1287 for (int i = 0; i < type->num_fields (); ++i)
1288 {
1289 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1290 {
1291 base_type = TYPE_TARGET_TYPE (type->field (i).type ());
1292 break;
1293 }
1294 }
1295 if (base_type == nullptr)
1296 error (_("Could not find 'data_ptr' in slice type"));
1297 }
1298 else if (type->code () == TYPE_CODE_PTR)
1299 base_type = TYPE_TARGET_TYPE (type);
1300 else
1301 error (_("Cannot subscript non-array type"));
1302
1303 struct type *new_type;
1304 if (want_slice)
1305 {
1306 if (rust_slice_type_p (type))
1307 new_type = type;
1308 else
1309 {
1310 struct type *usize
1311 = language_lookup_primitive_type (exp->language_defn,
1312 exp->gdbarch,
1313 "usize");
1314 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1315 }
1316 }
1317 else
1318 new_type = base_type;
1319
1320 return value_zero (new_type, VALUE_LVAL (lhs));
1321 }
1322 else
1323 {
1324 LONGEST low_bound;
1325 struct value *base;
1326
1327 if (type->code () == TYPE_CODE_ARRAY)
1328 {
1329 base = lhs;
1330 if (!get_array_bounds (type, &low_bound, &high_bound))
1331 error (_("Can't compute array bounds"));
1332 if (low_bound != 0)
1333 error (_("Found array with non-zero lower bound"));
1334 ++high_bound;
1335 }
1336 else if (rust_slice_type_p (type))
1337 {
1338 struct value *len;
1339
1340 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1341 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1342 low_bound = 0;
1343 high_bound = value_as_long (len);
1344 }
1345 else if (type->code () == TYPE_CODE_PTR)
1346 {
1347 base = lhs;
1348 low_bound = 0;
1349 high_bound = LONGEST_MAX;
1350 }
1351 else
1352 error (_("Cannot subscript non-array type"));
1353
1354 if (want_slice
1355 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1356 low = low_bound;
1357 if (low < 0)
1358 error (_("Index less than zero"));
1359 if (low > high_bound)
1360 error (_("Index greater than length"));
1361
1362 result = value_subscript (base, low);
1363 }
1364
1365 if (for_addr)
1366 {
1367 if (want_slice)
1368 {
1369 struct type *usize, *slice;
1370 CORE_ADDR addr;
1371 struct value *addrval, *tem;
1372
1373 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1374 high = high_bound;
1375 if (high < 0)
1376 error (_("High index less than zero"));
1377 if (low > high)
1378 error (_("Low index greater than high index"));
1379 if (high > high_bound)
1380 error (_("High index greater than length"));
1381
1382 usize = language_lookup_primitive_type (exp->language_defn,
1383 exp->gdbarch,
1384 "usize");
1385 const char *new_name = ((type != nullptr
1386 && rust_slice_type_p (type))
1387 ? type->name () : "&[*gdb*]");
1388
1389 slice = rust_slice_type (new_name, value_type (result), usize);
1390
1391 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1392 addr = value_as_long (addrval);
1393 tem = value_at_lazy (slice, addr);
1394
1395 value_assign (value_field (tem, 0), value_addr (result));
1396 value_assign (value_field (tem, 1),
1397 value_from_longest (usize, high - low));
1398
1399 result = value_at_lazy (slice, addr);
1400 }
1401 else
1402 result = value_addr (result);
1403 }
1404
1405 return result;
1406 }
1407
1408 /* evaluate_exp implementation for Rust. */
1409
1410 static struct value *
1411 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1412 int *pos, enum noside noside)
1413 {
1414 struct value *result;
1415
1416 switch (exp->elts[*pos].opcode)
1417 {
1418 case UNOP_IND:
1419 {
1420 if (noside != EVAL_NORMAL)
1421 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1422 else
1423 {
1424 ++*pos;
1425 struct value *value = evaluate_subexp (expect_type, exp, pos,
1426 noside);
1427
1428 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1429 if (trait_ptr != NULL)
1430 value = trait_ptr;
1431
1432 result = value_ind (value);
1433 }
1434 }
1435 break;
1436
1437 case UNOP_COMPLEMENT:
1438 {
1439 struct value *value;
1440
1441 ++*pos;
1442 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1443 if (noside == EVAL_SKIP)
1444 {
1445 /* Preserving the type is enough. */
1446 return value;
1447 }
1448 if (value_type (value)->code () == TYPE_CODE_BOOL)
1449 result = value_from_longest (value_type (value),
1450 value_logical_not (value));
1451 else
1452 result = value_complement (value);
1453 }
1454 break;
1455
1456 case BINOP_SUBSCRIPT:
1457 result = rust_subscript (exp, pos, noside, 0);
1458 break;
1459
1460 case OP_FUNCALL:
1461 result = rust_evaluate_funcall (exp, pos, noside);
1462 break;
1463
1464 case OP_AGGREGATE:
1465 {
1466 int pc = (*pos)++;
1467 struct type *type = exp->elts[pc + 1].type;
1468 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1469 int i;
1470 CORE_ADDR addr = 0;
1471 struct value *addrval = NULL;
1472
1473 *pos += 3;
1474
1475 if (noside == EVAL_NORMAL)
1476 {
1477 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1478 addr = value_as_long (addrval);
1479 result = value_at_lazy (type, addr);
1480 }
1481
1482 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1483 {
1484 struct value *init;
1485
1486 ++*pos;
1487 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1488 if (noside == EVAL_NORMAL)
1489 {
1490 /* This isn't quite right but will do for the time
1491 being, seeing that we can't implement the Copy
1492 trait anyway. */
1493 value_assign (result, init);
1494 }
1495
1496 --arglen;
1497 }
1498
1499 gdb_assert (arglen % 2 == 0);
1500 for (i = 0; i < arglen; i += 2)
1501 {
1502 int len;
1503 const char *fieldname;
1504 struct value *value, *field;
1505
1506 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1507 ++*pos;
1508 len = longest_to_int (exp->elts[*pos].longconst);
1509 ++*pos;
1510 fieldname = &exp->elts[*pos].string;
1511 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1512
1513 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1514 if (noside == EVAL_NORMAL)
1515 {
1516 field = value_struct_elt (&result, NULL, fieldname, NULL,
1517 "structure");
1518 value_assign (field, value);
1519 }
1520 }
1521
1522 if (noside == EVAL_SKIP)
1523 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1524 1);
1525 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1526 result = allocate_value (type);
1527 else
1528 result = value_at_lazy (type, addr);
1529 }
1530 break;
1531
1532 case OP_RUST_ARRAY:
1533 {
1534 (*pos)++;
1535 int copies;
1536 struct value *elt;
1537 struct value *ncopies;
1538
1539 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1540 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1541 copies = value_as_long (ncopies);
1542 if (copies < 0)
1543 error (_("Array with negative number of elements"));
1544
1545 if (noside == EVAL_NORMAL)
1546 {
1547 int i;
1548 std::vector<struct value *> eltvec (copies);
1549
1550 for (i = 0; i < copies; ++i)
1551 eltvec[i] = elt;
1552 result = value_array (0, copies - 1, eltvec.data ());
1553 }
1554 else
1555 {
1556 struct type *arraytype
1557 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1558 result = allocate_value (arraytype);
1559 }
1560 }
1561 break;
1562
1563 case STRUCTOP_ANONYMOUS:
1564 {
1565 /* Anonymous field access, i.e. foo.1. */
1566 struct value *lhs;
1567 int pc, field_number, nfields;
1568 struct type *type;
1569
1570 pc = (*pos)++;
1571 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1572 (*pos) += 2;
1573 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1574
1575 type = value_type (lhs);
1576
1577 if (type->code () == TYPE_CODE_STRUCT)
1578 {
1579 struct type *outer_type = NULL;
1580
1581 if (rust_enum_p (type))
1582 {
1583 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1584 TYPE_LENGTH (type));
1585 type = resolve_dynamic_type (type, view, value_address (lhs));
1586
1587 if (rust_empty_enum_p (type))
1588 error (_("Cannot access field %d of empty enum %s"),
1589 field_number, type->name ());
1590
1591 int fieldno = rust_enum_variant (type);
1592 lhs = value_primitive_field (lhs, 0, fieldno, type);
1593 outer_type = type;
1594 type = value_type (lhs);
1595 }
1596
1597 /* Tuples and tuple structs */
1598 nfields = type->num_fields ();
1599
1600 if (field_number >= nfields || field_number < 0)
1601 {
1602 if (outer_type != NULL)
1603 error(_("Cannot access field %d of variant %s::%s, "
1604 "there are only %d fields"),
1605 field_number, outer_type->name (),
1606 rust_last_path_segment (type->name ()),
1607 nfields);
1608 else
1609 error(_("Cannot access field %d of %s, "
1610 "there are only %d fields"),
1611 field_number, type->name (), nfields);
1612 }
1613
1614 /* Tuples are tuple structs too. */
1615 if (!rust_tuple_struct_type_p (type))
1616 {
1617 if (outer_type != NULL)
1618 error(_("Variant %s::%s is not a tuple variant"),
1619 outer_type->name (),
1620 rust_last_path_segment (type->name ()));
1621 else
1622 error(_("Attempting to access anonymous field %d "
1623 "of %s, which is not a tuple, tuple struct, or "
1624 "tuple-like variant"),
1625 field_number, type->name ());
1626 }
1627
1628 result = value_primitive_field (lhs, 0, field_number, type);
1629 }
1630 else
1631 error(_("Anonymous field access is only allowed on tuples, \
1632 tuple structs, and tuple-like enum variants"));
1633 }
1634 break;
1635
1636 case STRUCTOP_STRUCT:
1637 {
1638 struct value *lhs;
1639 struct type *type;
1640 int tem, pc;
1641
1642 pc = (*pos)++;
1643 tem = longest_to_int (exp->elts[pc + 1].longconst);
1644 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1645 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1646
1647 const char *field_name = &exp->elts[pc + 2].string;
1648 type = value_type (lhs);
1649 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1650 {
1651 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1652 TYPE_LENGTH (type));
1653 type = resolve_dynamic_type (type, view, value_address (lhs));
1654
1655 if (rust_empty_enum_p (type))
1656 error (_("Cannot access field %s of empty enum %s"),
1657 field_name, type->name ());
1658
1659 int fieldno = rust_enum_variant (type);
1660 lhs = value_primitive_field (lhs, 0, fieldno, type);
1661
1662 struct type *outer_type = type;
1663 type = value_type (lhs);
1664 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1665 error (_("Attempting to access named field %s of tuple "
1666 "variant %s::%s, which has only anonymous fields"),
1667 field_name, outer_type->name (),
1668 rust_last_path_segment (type->name ()));
1669
1670 try
1671 {
1672 result = value_struct_elt (&lhs, NULL, field_name,
1673 NULL, "structure");
1674 }
1675 catch (const gdb_exception_error &except)
1676 {
1677 error (_("Could not find field %s of struct variant %s::%s"),
1678 field_name, outer_type->name (),
1679 rust_last_path_segment (type->name ()));
1680 }
1681 }
1682 else
1683 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1684 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1685 result = value_zero (value_type (result), VALUE_LVAL (result));
1686 }
1687 break;
1688
1689 case OP_RANGE:
1690 result = rust_range (exp, pos, noside);
1691 break;
1692
1693 case UNOP_ADDR:
1694 /* We might have &array[range], in which case we need to make a
1695 slice. */
1696 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1697 {
1698 ++*pos;
1699 result = rust_subscript (exp, pos, noside, 1);
1700 break;
1701 }
1702 /* Fall through. */
1703 default:
1704 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1705 break;
1706 }
1707
1708 return result;
1709 }
1710
1711 /* operator_length implementation for Rust. */
1712
1713 static void
1714 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1715 int *argsp)
1716 {
1717 int oplen = 1;
1718 int args = 0;
1719
1720 switch (exp->elts[pc - 1].opcode)
1721 {
1722 case OP_AGGREGATE:
1723 /* We handle aggregate as a type and argument count. The first
1724 argument might be OP_OTHERS. After that the arguments
1725 alternate: first an OP_NAME, then an expression. */
1726 oplen = 4;
1727 args = longest_to_int (exp->elts[pc - 2].longconst);
1728 break;
1729
1730 case OP_OTHERS:
1731 oplen = 1;
1732 args = 1;
1733 break;
1734
1735 case STRUCTOP_ANONYMOUS:
1736 oplen = 3;
1737 args = 1;
1738 break;
1739
1740 case OP_RUST_ARRAY:
1741 oplen = 1;
1742 args = 2;
1743 break;
1744
1745 default:
1746 operator_length_standard (exp, pc, oplenp, argsp);
1747 return;
1748 }
1749
1750 *oplenp = oplen;
1751 *argsp = args;
1752 }
1753
1754 /* op_name implementation for Rust. */
1755
1756 static const char *
1757 rust_op_name (enum exp_opcode opcode)
1758 {
1759 switch (opcode)
1760 {
1761 case OP_AGGREGATE:
1762 return "OP_AGGREGATE";
1763 case OP_OTHERS:
1764 return "OP_OTHERS";
1765 default:
1766 return op_name_standard (opcode);
1767 }
1768 }
1769
1770 /* dump_subexp_body implementation for Rust. */
1771
1772 static int
1773 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1774 int elt)
1775 {
1776 switch (exp->elts[elt].opcode)
1777 {
1778 case OP_AGGREGATE:
1779 {
1780 int length = longest_to_int (exp->elts[elt + 2].longconst);
1781 int i;
1782
1783 fprintf_filtered (stream, "Type @");
1784 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1785 fprintf_filtered (stream, " (");
1786 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1787 fprintf_filtered (stream, "), length %d", length);
1788
1789 elt += 4;
1790 for (i = 0; i < length; ++i)
1791 elt = dump_subexp (exp, stream, elt);
1792 }
1793 break;
1794
1795 case OP_STRING:
1796 case OP_NAME:
1797 {
1798 LONGEST len = exp->elts[elt + 1].longconst;
1799
1800 fprintf_filtered (stream, "%s: %s",
1801 (exp->elts[elt].opcode == OP_STRING
1802 ? "string" : "name"),
1803 &exp->elts[elt + 2].string);
1804 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1805 }
1806 break;
1807
1808 case OP_OTHERS:
1809 elt = dump_subexp (exp, stream, elt + 1);
1810 break;
1811
1812 case STRUCTOP_ANONYMOUS:
1813 {
1814 int field_number;
1815
1816 field_number = longest_to_int (exp->elts[elt + 1].longconst);
1817
1818 fprintf_filtered (stream, "Field number: %d", field_number);
1819 elt = dump_subexp (exp, stream, elt + 3);
1820 }
1821 break;
1822
1823 case OP_RUST_ARRAY:
1824 ++elt;
1825 break;
1826
1827 default:
1828 elt = dump_subexp_body_standard (exp, stream, elt);
1829 break;
1830 }
1831
1832 return elt;
1833 }
1834
1835 /* print_subexp implementation for Rust. */
1836
1837 static void
1838 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1839 enum precedence prec)
1840 {
1841 switch (exp->elts[*pos].opcode)
1842 {
1843 case OP_AGGREGATE:
1844 {
1845 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1846 int i;
1847
1848 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1849 fputs_filtered (" { ", stream);
1850
1851 *pos += 4;
1852 for (i = 0; i < length; ++i)
1853 {
1854 rust_print_subexp (exp, pos, stream, prec);
1855 fputs_filtered (", ", stream);
1856 }
1857 fputs_filtered (" }", stream);
1858 }
1859 break;
1860
1861 case OP_NAME:
1862 {
1863 LONGEST len = exp->elts[*pos + 1].longconst;
1864
1865 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1866 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1867 }
1868 break;
1869
1870 case OP_OTHERS:
1871 {
1872 fputs_filtered ("<<others>> (", stream);
1873 ++*pos;
1874 rust_print_subexp (exp, pos, stream, prec);
1875 fputs_filtered (")", stream);
1876 }
1877 break;
1878
1879 case STRUCTOP_ANONYMOUS:
1880 {
1881 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1882
1883 (*pos) += 3;
1884 print_subexp (exp, pos, stream, PREC_SUFFIX);
1885 fprintf_filtered (stream, ".%d", tem);
1886 }
1887 break;
1888
1889 case OP_RUST_ARRAY:
1890 ++*pos;
1891 fprintf_filtered (stream, "[");
1892 rust_print_subexp (exp, pos, stream, prec);
1893 fprintf_filtered (stream, "; ");
1894 rust_print_subexp (exp, pos, stream, prec);
1895 fprintf_filtered (stream, "]");
1896 break;
1897
1898 default:
1899 print_subexp_standard (exp, pos, stream, prec);
1900 break;
1901 }
1902 }
1903
1904 /* operator_check implementation for Rust. */
1905
1906 static int
1907 rust_operator_check (struct expression *exp, int pos,
1908 int (*objfile_func) (struct objfile *objfile,
1909 void *data),
1910 void *data)
1911 {
1912 switch (exp->elts[pos].opcode)
1913 {
1914 case OP_AGGREGATE:
1915 {
1916 struct type *type = exp->elts[pos + 1].type;
1917 struct objfile *objfile = TYPE_OBJFILE (type);
1918
1919 if (objfile != NULL && (*objfile_func) (objfile, data))
1920 return 1;
1921 }
1922 break;
1923
1924 case OP_OTHERS:
1925 case OP_NAME:
1926 case OP_RUST_ARRAY:
1927 break;
1928
1929 default:
1930 return operator_check_standard (exp, pos, objfile_func, data);
1931 }
1932
1933 return 0;
1934 }
1935
1936 \f
1937
1938 static const struct exp_descriptor exp_descriptor_rust =
1939 {
1940 rust_print_subexp,
1941 rust_operator_length,
1942 rust_operator_check,
1943 rust_op_name,
1944 rust_dump_subexp_body,
1945 rust_evaluate_subexp
1946 };
1947
1948 static const char *rust_extensions[] =
1949 {
1950 ".rs", NULL
1951 };
1952
1953 /* Constant data representing the Rust language. */
1954
1955 extern const struct language_data rust_language_data =
1956 {
1957 "rust",
1958 "Rust",
1959 language_rust,
1960 range_check_on,
1961 case_sensitive_on,
1962 array_row_major,
1963 macro_expansion_no,
1964 rust_extensions,
1965 &exp_descriptor_rust,
1966 rust_printchar, /* Print a character constant */
1967 rust_printstr, /* Function to print string constant */
1968 rust_print_typedef, /* Print a typedef using appropriate syntax */
1969 NULL, /* name_of_this */
1970 false, /* la_store_sym_names_in_linkage_form_p */
1971 c_op_print_tab, /* expression operators for printing */
1972 1, /* c-style arrays */
1973 0, /* String lower bound */
1974 &default_varobj_ops,
1975 rust_is_string_type_p,
1976 "{...}" /* la_struct_too_deep_ellipsis */
1977 };
1978
1979 /* Class representing the Rust language. */
1980
1981 class rust_language : public language_defn
1982 {
1983 public:
1984 rust_language ()
1985 : language_defn (language_rust, rust_language_data)
1986 { /* Nothing. */ }
1987
1988 /* See language.h. */
1989 void language_arch_info (struct gdbarch *gdbarch,
1990 struct language_arch_info *lai) const override
1991 {
1992 const struct builtin_type *builtin = builtin_type (gdbarch);
1993
1994 struct type **types
1995 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1996 struct type *);
1997
1998 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1999 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
2000 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
2001 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
2002 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
2003 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
2004 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
2005 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
2006 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
2007 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
2008
2009 unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
2010 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
2011 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
2012
2013 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
2014 floatformats_ieee_single);
2015 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
2016 floatformats_ieee_double);
2017
2018 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
2019
2020 struct type *tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
2021 types[rust_primitive_str] = rust_slice_type ("&str", tem,
2022 types[rust_primitive_usize]);
2023
2024 lai->primitive_type_vector = types;
2025 lai->bool_type_default = types[rust_primitive_bool];
2026 lai->string_char_type = types[rust_primitive_u8];
2027 }
2028
2029 /* See language.h. */
2030 bool sniff_from_mangled_name (const char *mangled,
2031 char **demangled) const override
2032 {
2033 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2034 return *demangled != NULL;
2035 }
2036
2037 /* See language.h. */
2038
2039 char *demangle (const char *mangled, int options) const override
2040 {
2041 return gdb_demangle (mangled, options);
2042 }
2043
2044 /* See language.h. */
2045
2046 void print_type (struct type *type, const char *varstring,
2047 struct ui_file *stream, int show, int level,
2048 const struct type_print_options *flags) const override
2049 {
2050 print_offset_data podata;
2051 rust_internal_print_type (type, varstring, stream, show, level,
2052 flags, false, &podata);
2053 }
2054
2055 /* See language.h. */
2056
2057 gdb::unique_xmalloc_ptr<char> watch_location_expression
2058 (struct type *type, CORE_ADDR addr) const override
2059 {
2060 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2061 std::string name = type_to_string (type);
2062 return gdb::unique_xmalloc_ptr<char>
2063 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2064 name.c_str ()));
2065 }
2066
2067 /* See language.h. */
2068
2069 void value_print_inner
2070 (struct value *val, struct ui_file *stream, int recurse,
2071 const struct value_print_options *options) const override
2072 {
2073 return rust_value_print_inner (val, stream, recurse, options);
2074 }
2075
2076 /* See language.h. */
2077
2078 struct block_symbol lookup_symbol_nonlocal
2079 (const char *name, const struct block *block,
2080 const domain_enum domain) const override
2081 {
2082 struct block_symbol result = {};
2083
2084 if (symbol_lookup_debug)
2085 {
2086 fprintf_unfiltered (gdb_stdlog,
2087 "rust_lookup_symbol_non_local"
2088 " (%s, %s (scope %s), %s)\n",
2089 name, host_address_to_string (block),
2090 block_scope (block), domain_name (domain));
2091 }
2092
2093 /* Look up bare names in the block's scope. */
2094 std::string scopedname;
2095 if (name[cp_find_first_component (name)] == '\0')
2096 {
2097 const char *scope = block_scope (block);
2098
2099 if (scope[0] != '\0')
2100 {
2101 scopedname = std::string (scope) + "::" + name;
2102 name = scopedname.c_str ();
2103 }
2104 else
2105 name = NULL;
2106 }
2107
2108 if (name != NULL)
2109 {
2110 result = lookup_symbol_in_static_block (name, block, domain);
2111 if (result.symbol == NULL)
2112 result = lookup_global_symbol (name, block, domain);
2113 }
2114 return result;
2115 }
2116
2117 /* See language.h. */
2118
2119 int parser (struct parser_state *ps) const override
2120 {
2121 return rust_parse (ps);
2122 }
2123
2124 /* See language.h. */
2125
2126 void emitchar (int ch, struct type *chtype,
2127 struct ui_file *stream, int quoter) const override
2128 {
2129 if (!rust_chartype_p (chtype))
2130 generic_emit_char (ch, chtype, stream, quoter,
2131 target_charset (get_type_arch (chtype)));
2132 else if (ch == '\\' || ch == quoter)
2133 fprintf_filtered (stream, "\\%c", ch);
2134 else if (ch == '\n')
2135 fputs_filtered ("\\n", stream);
2136 else if (ch == '\r')
2137 fputs_filtered ("\\r", stream);
2138 else if (ch == '\t')
2139 fputs_filtered ("\\t", stream);
2140 else if (ch == '\0')
2141 fputs_filtered ("\\0", stream);
2142 else if (ch >= 32 && ch <= 127 && isprint (ch))
2143 fputc_filtered (ch, stream);
2144 else if (ch <= 255)
2145 fprintf_filtered (stream, "\\x%02x", ch);
2146 else
2147 fprintf_filtered (stream, "\\u{%06x}", ch);
2148 }
2149 };
2150
2151 /* Single instance of the Rust language class. */
2152
2153 static rust_language rust_language_defn;