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