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