gdb: Convert language la_print_typedef 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_type implementation for Rust. */
769
770 static void
771 rust_internal_print_type (struct type *type, const char *varstring,
772 struct ui_file *stream, int show, int level,
773 const struct type_print_options *flags,
774 bool for_rust_enum, print_offset_data *podata)
775 {
776 QUIT;
777 if (show <= 0
778 && type->name () != NULL)
779 {
780 /* Rust calls the unit type "void" in its debuginfo,
781 but we don't want to print it as that. */
782 if (type->code () == TYPE_CODE_VOID)
783 fputs_filtered ("()", stream);
784 else
785 fputs_filtered (type->name (), stream);
786 return;
787 }
788
789 type = check_typedef (type);
790 switch (type->code ())
791 {
792 case TYPE_CODE_VOID:
793 /* If we have an enum, we've already printed the type's
794 unqualified name, and there is nothing else to print
795 here. */
796 if (!for_rust_enum)
797 fputs_filtered ("()", stream);
798 break;
799
800 case TYPE_CODE_FUNC:
801 /* Delegate varargs to the C printer. */
802 if (TYPE_VARARGS (type))
803 goto c_printer;
804
805 fputs_filtered ("fn ", stream);
806 if (varstring != NULL)
807 fputs_filtered (varstring, stream);
808 fputs_filtered ("(", stream);
809 for (int i = 0; i < type->num_fields (); ++i)
810 {
811 QUIT;
812 if (i > 0)
813 fputs_filtered (", ", stream);
814 rust_internal_print_type (type->field (i).type (), "", stream,
815 -1, 0, flags, false, podata);
816 }
817 fputs_filtered (")", stream);
818 /* If it returns unit, we can omit the return type. */
819 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
820 {
821 fputs_filtered (" -> ", stream);
822 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
823 -1, 0, flags, false, podata);
824 }
825 break;
826
827 case TYPE_CODE_ARRAY:
828 {
829 LONGEST low_bound, high_bound;
830
831 fputs_filtered ("[", stream);
832 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
833 stream, show - 1, level, flags, false,
834 podata);
835
836 if (TYPE_HIGH_BOUND_KIND (type->index_type ()) == PROP_LOCEXPR
837 || TYPE_HIGH_BOUND_KIND (type->index_type ()) == PROP_LOCLIST)
838 fprintf_filtered (stream, "; variable length");
839 else if (get_array_bounds (type, &low_bound, &high_bound))
840 fprintf_filtered (stream, "; %s",
841 plongest (high_bound - low_bound + 1));
842 fputs_filtered ("]", stream);
843 }
844 break;
845
846 case TYPE_CODE_UNION:
847 case TYPE_CODE_STRUCT:
848 rust_print_struct_def (type, varstring, stream, show, level, flags,
849 for_rust_enum, podata);
850 break;
851
852 case TYPE_CODE_ENUM:
853 {
854 int len = 0;
855
856 fputs_filtered ("enum ", stream);
857 if (type->name () != NULL)
858 {
859 fputs_filtered (type->name (), stream);
860 fputs_filtered (" ", stream);
861 len = strlen (type->name ());
862 }
863 fputs_filtered ("{\n", stream);
864
865 for (int i = 0; i < type->num_fields (); ++i)
866 {
867 const char *name = TYPE_FIELD_NAME (type, i);
868
869 QUIT;
870
871 if (len > 0
872 && strncmp (name, type->name (), len) == 0
873 && name[len] == ':'
874 && name[len + 1] == ':')
875 name += len + 2;
876 fprintfi_filtered (level + 2, stream, "%ps,\n",
877 styled_string (variable_name_style.style (),
878 name));
879 }
880
881 fputs_filtered ("}", stream);
882 }
883 break;
884
885 case TYPE_CODE_PTR:
886 {
887 if (type->name () != nullptr)
888 fputs_filtered (type->name (), stream);
889 else
890 {
891 /* We currently can't distinguish between pointers and
892 references. */
893 fputs_filtered ("*mut ", stream);
894 type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
895 }
896 }
897 break;
898
899 default:
900 c_printer:
901 c_print_type (type, varstring, stream, show, level, flags);
902 }
903 }
904
905 \f
906
907 /* Like arch_composite_type, but uses TYPE to decide how to allocate
908 -- either on an obstack or on a gdbarch. */
909
910 static struct type *
911 rust_composite_type (struct type *original,
912 const char *name,
913 const char *field1, struct type *type1,
914 const char *field2, struct type *type2)
915 {
916 struct type *result = alloc_type_copy (original);
917 int i, nfields, bitpos;
918
919 nfields = 0;
920 if (field1 != NULL)
921 ++nfields;
922 if (field2 != NULL)
923 ++nfields;
924
925 result->set_code (TYPE_CODE_STRUCT);
926 result->set_name (name);
927
928 result->set_num_fields (nfields);
929 result->set_fields
930 ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
931
932 i = 0;
933 bitpos = 0;
934 if (field1 != NULL)
935 {
936 struct field *field = &result->field (i);
937
938 SET_FIELD_BITPOS (*field, bitpos);
939 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
940
941 FIELD_NAME (*field) = field1;
942 field->set_type (type1);
943 ++i;
944 }
945 if (field2 != NULL)
946 {
947 struct field *field = &result->field (i);
948 unsigned align = type_align (type2);
949
950 if (align != 0)
951 {
952 int delta;
953
954 align *= TARGET_CHAR_BIT;
955 delta = bitpos % align;
956 if (delta != 0)
957 bitpos += align - delta;
958 }
959 SET_FIELD_BITPOS (*field, bitpos);
960
961 FIELD_NAME (*field) = field2;
962 field->set_type (type2);
963 ++i;
964 }
965
966 if (i > 0)
967 TYPE_LENGTH (result)
968 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
969 TYPE_LENGTH (result->field (i - 1).type ()));
970 return result;
971 }
972
973 /* See rust-lang.h. */
974
975 struct type *
976 rust_slice_type (const char *name, struct type *elt_type,
977 struct type *usize_type)
978 {
979 struct type *type;
980
981 elt_type = lookup_pointer_type (elt_type);
982 type = rust_composite_type (elt_type, name,
983 "data_ptr", elt_type,
984 "length", usize_type);
985
986 return type;
987 }
988
989 enum rust_primitive_types
990 {
991 rust_primitive_bool,
992 rust_primitive_char,
993 rust_primitive_i8,
994 rust_primitive_u8,
995 rust_primitive_i16,
996 rust_primitive_u16,
997 rust_primitive_i32,
998 rust_primitive_u32,
999 rust_primitive_i64,
1000 rust_primitive_u64,
1001 rust_primitive_isize,
1002 rust_primitive_usize,
1003 rust_primitive_f32,
1004 rust_primitive_f64,
1005 rust_primitive_unit,
1006 rust_primitive_str,
1007 nr_rust_primitive_types
1008 };
1009
1010 \f
1011
1012 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1013
1014 static struct value *
1015 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1016 {
1017 int i;
1018 int num_args = exp->elts[*pos + 1].longconst;
1019 const char *method;
1020 struct value *function, *result, *arg0;
1021 struct type *type, *fn_type;
1022 const struct block *block;
1023 struct block_symbol sym;
1024
1025 /* For an ordinary function call we can simply defer to the
1026 generic implementation. */
1027 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1028 return evaluate_subexp_standard (NULL, exp, pos, noside);
1029
1030 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1031 *pos += 4;
1032 method = &exp->elts[*pos + 1].string;
1033 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1034
1035 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1036 type in order to look up the method. */
1037 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1038
1039 if (noside == EVAL_SKIP)
1040 {
1041 for (i = 0; i < num_args; ++i)
1042 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1043 return arg0;
1044 }
1045
1046 std::vector<struct value *> args (num_args + 1);
1047 args[0] = arg0;
1048
1049 /* We don't yet implement real Deref semantics. */
1050 while (value_type (args[0])->code () == TYPE_CODE_PTR)
1051 args[0] = value_ind (args[0]);
1052
1053 type = value_type (args[0]);
1054 if ((type->code () != TYPE_CODE_STRUCT
1055 && type->code () != TYPE_CODE_UNION
1056 && type->code () != TYPE_CODE_ENUM)
1057 || rust_tuple_type_p (type))
1058 error (_("Method calls only supported on struct or enum types"));
1059 if (type->name () == NULL)
1060 error (_("Method call on nameless type"));
1061
1062 std::string name = std::string (type->name ()) + "::" + method;
1063
1064 block = get_selected_block (0);
1065 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1066 if (sym.symbol == NULL)
1067 error (_("Could not find function named '%s'"), name.c_str ());
1068
1069 fn_type = SYMBOL_TYPE (sym.symbol);
1070 if (fn_type->num_fields () == 0)
1071 error (_("Function '%s' takes no arguments"), name.c_str ());
1072
1073 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1074 args[0] = value_addr (args[0]);
1075
1076 function = address_of_variable (sym.symbol, block);
1077
1078 for (i = 0; i < num_args; ++i)
1079 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1080
1081 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1082 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1083 else
1084 result = call_function_by_hand (function, NULL, args);
1085 return result;
1086 }
1087
1088 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1089
1090 static struct value *
1091 rust_range (struct expression *exp, int *pos, enum noside noside)
1092 {
1093 enum range_type kind;
1094 struct value *low = NULL, *high = NULL;
1095 struct value *addrval, *result;
1096 CORE_ADDR addr;
1097 struct type *range_type;
1098 struct type *index_type;
1099 struct type *temp_type;
1100 const char *name;
1101
1102 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1103 *pos += 3;
1104
1105 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT
1106 || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1107 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1108 if (kind == LOW_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT_EXCLUSIVE
1109 || kind == NONE_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT_EXCLUSIVE)
1110 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1111 bool inclusive = (kind == NONE_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT);
1112
1113 if (noside == EVAL_SKIP)
1114 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1115
1116 if (low == NULL)
1117 {
1118 if (high == NULL)
1119 {
1120 index_type = NULL;
1121 name = "std::ops::RangeFull";
1122 }
1123 else
1124 {
1125 index_type = value_type (high);
1126 name = (inclusive
1127 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1128 }
1129 }
1130 else
1131 {
1132 if (high == NULL)
1133 {
1134 index_type = value_type (low);
1135 name = "std::ops::RangeFrom";
1136 }
1137 else
1138 {
1139 if (!types_equal (value_type (low), value_type (high)))
1140 error (_("Range expression with different types"));
1141 index_type = value_type (low);
1142 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1143 }
1144 }
1145
1146 /* If we don't have an index type, just allocate this on the
1147 arch. Here any type will do. */
1148 temp_type = (index_type == NULL
1149 ? language_bool_type (exp->language_defn, exp->gdbarch)
1150 : index_type);
1151 /* It would be nicer to cache the range type. */
1152 range_type = rust_composite_type (temp_type, name,
1153 low == NULL ? NULL : "start", index_type,
1154 high == NULL ? NULL : "end", index_type);
1155
1156 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1157 return value_zero (range_type, lval_memory);
1158
1159 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1160 addr = value_as_long (addrval);
1161 result = value_at_lazy (range_type, addr);
1162
1163 if (low != NULL)
1164 {
1165 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1166 "range");
1167
1168 value_assign (start, low);
1169 }
1170
1171 if (high != NULL)
1172 {
1173 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1174 "range");
1175
1176 value_assign (end, high);
1177 }
1178
1179 result = value_at_lazy (range_type, addr);
1180 return result;
1181 }
1182
1183 /* A helper function to compute the range and kind given a range
1184 value. TYPE is the type of the range value. RANGE is the range
1185 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1186 parameters might be filled in, or might not be, depending on the
1187 kind of range this is. KIND will always be set to the appropriate
1188 value describing the kind of range, and this can be used to
1189 determine whether LOW or HIGH are valid. */
1190
1191 static void
1192 rust_compute_range (struct type *type, struct value *range,
1193 LONGEST *low, LONGEST *high,
1194 enum range_type *kind)
1195 {
1196 int i;
1197
1198 *low = 0;
1199 *high = 0;
1200 *kind = BOTH_BOUND_DEFAULT;
1201
1202 if (type->num_fields () == 0)
1203 return;
1204
1205 i = 0;
1206 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1207 {
1208 *kind = HIGH_BOUND_DEFAULT;
1209 *low = value_as_long (value_field (range, 0));
1210 ++i;
1211 }
1212 if (type->num_fields () > i
1213 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1214 {
1215 *kind = (*kind == BOTH_BOUND_DEFAULT
1216 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1217 *high = value_as_long (value_field (range, i));
1218
1219 if (rust_inclusive_range_type_p (type))
1220 ++*high;
1221 }
1222 }
1223
1224 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1225
1226 static struct value *
1227 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1228 int for_addr)
1229 {
1230 struct value *lhs, *rhs, *result;
1231 struct type *rhstype;
1232 LONGEST low, high_bound;
1233 /* Initialized to appease the compiler. */
1234 enum range_type kind = BOTH_BOUND_DEFAULT;
1235 LONGEST high = 0;
1236 int want_slice = 0;
1237
1238 ++*pos;
1239 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1240 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1241
1242 if (noside == EVAL_SKIP)
1243 return lhs;
1244
1245 rhstype = check_typedef (value_type (rhs));
1246 if (rust_range_type_p (rhstype))
1247 {
1248 if (!for_addr)
1249 error (_("Can't take slice of array without '&'"));
1250 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1251 want_slice = 1;
1252 }
1253 else
1254 low = value_as_long (rhs);
1255
1256 struct type *type = check_typedef (value_type (lhs));
1257 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1258 {
1259 struct type *base_type = nullptr;
1260 if (type->code () == TYPE_CODE_ARRAY)
1261 base_type = TYPE_TARGET_TYPE (type);
1262 else if (rust_slice_type_p (type))
1263 {
1264 for (int i = 0; i < type->num_fields (); ++i)
1265 {
1266 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1267 {
1268 base_type = TYPE_TARGET_TYPE (type->field (i).type ());
1269 break;
1270 }
1271 }
1272 if (base_type == nullptr)
1273 error (_("Could not find 'data_ptr' in slice type"));
1274 }
1275 else if (type->code () == TYPE_CODE_PTR)
1276 base_type = TYPE_TARGET_TYPE (type);
1277 else
1278 error (_("Cannot subscript non-array type"));
1279
1280 struct type *new_type;
1281 if (want_slice)
1282 {
1283 if (rust_slice_type_p (type))
1284 new_type = type;
1285 else
1286 {
1287 struct type *usize
1288 = language_lookup_primitive_type (exp->language_defn,
1289 exp->gdbarch,
1290 "usize");
1291 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1292 }
1293 }
1294 else
1295 new_type = base_type;
1296
1297 return value_zero (new_type, VALUE_LVAL (lhs));
1298 }
1299 else
1300 {
1301 LONGEST low_bound;
1302 struct value *base;
1303
1304 if (type->code () == TYPE_CODE_ARRAY)
1305 {
1306 base = lhs;
1307 if (!get_array_bounds (type, &low_bound, &high_bound))
1308 error (_("Can't compute array bounds"));
1309 if (low_bound != 0)
1310 error (_("Found array with non-zero lower bound"));
1311 ++high_bound;
1312 }
1313 else if (rust_slice_type_p (type))
1314 {
1315 struct value *len;
1316
1317 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1318 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1319 low_bound = 0;
1320 high_bound = value_as_long (len);
1321 }
1322 else if (type->code () == TYPE_CODE_PTR)
1323 {
1324 base = lhs;
1325 low_bound = 0;
1326 high_bound = LONGEST_MAX;
1327 }
1328 else
1329 error (_("Cannot subscript non-array type"));
1330
1331 if (want_slice
1332 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1333 low = low_bound;
1334 if (low < 0)
1335 error (_("Index less than zero"));
1336 if (low > high_bound)
1337 error (_("Index greater than length"));
1338
1339 result = value_subscript (base, low);
1340 }
1341
1342 if (for_addr)
1343 {
1344 if (want_slice)
1345 {
1346 struct type *usize, *slice;
1347 CORE_ADDR addr;
1348 struct value *addrval, *tem;
1349
1350 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1351 high = high_bound;
1352 if (high < 0)
1353 error (_("High index less than zero"));
1354 if (low > high)
1355 error (_("Low index greater than high index"));
1356 if (high > high_bound)
1357 error (_("High index greater than length"));
1358
1359 usize = language_lookup_primitive_type (exp->language_defn,
1360 exp->gdbarch,
1361 "usize");
1362 const char *new_name = ((type != nullptr
1363 && rust_slice_type_p (type))
1364 ? type->name () : "&[*gdb*]");
1365
1366 slice = rust_slice_type (new_name, value_type (result), usize);
1367
1368 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1369 addr = value_as_long (addrval);
1370 tem = value_at_lazy (slice, addr);
1371
1372 value_assign (value_field (tem, 0), value_addr (result));
1373 value_assign (value_field (tem, 1),
1374 value_from_longest (usize, high - low));
1375
1376 result = value_at_lazy (slice, addr);
1377 }
1378 else
1379 result = value_addr (result);
1380 }
1381
1382 return result;
1383 }
1384
1385 /* evaluate_exp implementation for Rust. */
1386
1387 static struct value *
1388 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1389 int *pos, enum noside noside)
1390 {
1391 struct value *result;
1392
1393 switch (exp->elts[*pos].opcode)
1394 {
1395 case UNOP_IND:
1396 {
1397 if (noside != EVAL_NORMAL)
1398 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1399 else
1400 {
1401 ++*pos;
1402 struct value *value = evaluate_subexp (expect_type, exp, pos,
1403 noside);
1404
1405 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1406 if (trait_ptr != NULL)
1407 value = trait_ptr;
1408
1409 result = value_ind (value);
1410 }
1411 }
1412 break;
1413
1414 case UNOP_COMPLEMENT:
1415 {
1416 struct value *value;
1417
1418 ++*pos;
1419 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1420 if (noside == EVAL_SKIP)
1421 {
1422 /* Preserving the type is enough. */
1423 return value;
1424 }
1425 if (value_type (value)->code () == TYPE_CODE_BOOL)
1426 result = value_from_longest (value_type (value),
1427 value_logical_not (value));
1428 else
1429 result = value_complement (value);
1430 }
1431 break;
1432
1433 case BINOP_SUBSCRIPT:
1434 result = rust_subscript (exp, pos, noside, 0);
1435 break;
1436
1437 case OP_FUNCALL:
1438 result = rust_evaluate_funcall (exp, pos, noside);
1439 break;
1440
1441 case OP_AGGREGATE:
1442 {
1443 int pc = (*pos)++;
1444 struct type *type = exp->elts[pc + 1].type;
1445 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1446 int i;
1447 CORE_ADDR addr = 0;
1448 struct value *addrval = NULL;
1449
1450 *pos += 3;
1451
1452 if (noside == EVAL_NORMAL)
1453 {
1454 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1455 addr = value_as_long (addrval);
1456 result = value_at_lazy (type, addr);
1457 }
1458
1459 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1460 {
1461 struct value *init;
1462
1463 ++*pos;
1464 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1465 if (noside == EVAL_NORMAL)
1466 {
1467 /* This isn't quite right but will do for the time
1468 being, seeing that we can't implement the Copy
1469 trait anyway. */
1470 value_assign (result, init);
1471 }
1472
1473 --arglen;
1474 }
1475
1476 gdb_assert (arglen % 2 == 0);
1477 for (i = 0; i < arglen; i += 2)
1478 {
1479 int len;
1480 const char *fieldname;
1481 struct value *value, *field;
1482
1483 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1484 ++*pos;
1485 len = longest_to_int (exp->elts[*pos].longconst);
1486 ++*pos;
1487 fieldname = &exp->elts[*pos].string;
1488 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1489
1490 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1491 if (noside == EVAL_NORMAL)
1492 {
1493 field = value_struct_elt (&result, NULL, fieldname, NULL,
1494 "structure");
1495 value_assign (field, value);
1496 }
1497 }
1498
1499 if (noside == EVAL_SKIP)
1500 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1501 1);
1502 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1503 result = allocate_value (type);
1504 else
1505 result = value_at_lazy (type, addr);
1506 }
1507 break;
1508
1509 case OP_RUST_ARRAY:
1510 {
1511 (*pos)++;
1512 int copies;
1513 struct value *elt;
1514 struct value *ncopies;
1515
1516 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1517 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1518 copies = value_as_long (ncopies);
1519 if (copies < 0)
1520 error (_("Array with negative number of elements"));
1521
1522 if (noside == EVAL_NORMAL)
1523 {
1524 int i;
1525 std::vector<struct value *> eltvec (copies);
1526
1527 for (i = 0; i < copies; ++i)
1528 eltvec[i] = elt;
1529 result = value_array (0, copies - 1, eltvec.data ());
1530 }
1531 else
1532 {
1533 struct type *arraytype
1534 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1535 result = allocate_value (arraytype);
1536 }
1537 }
1538 break;
1539
1540 case STRUCTOP_ANONYMOUS:
1541 {
1542 /* Anonymous field access, i.e. foo.1. */
1543 struct value *lhs;
1544 int pc, field_number, nfields;
1545 struct type *type;
1546
1547 pc = (*pos)++;
1548 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1549 (*pos) += 2;
1550 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1551
1552 type = value_type (lhs);
1553
1554 if (type->code () == TYPE_CODE_STRUCT)
1555 {
1556 struct type *outer_type = NULL;
1557
1558 if (rust_enum_p (type))
1559 {
1560 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1561 TYPE_LENGTH (type));
1562 type = resolve_dynamic_type (type, view, value_address (lhs));
1563
1564 if (rust_empty_enum_p (type))
1565 error (_("Cannot access field %d of empty enum %s"),
1566 field_number, type->name ());
1567
1568 int fieldno = rust_enum_variant (type);
1569 lhs = value_primitive_field (lhs, 0, fieldno, type);
1570 outer_type = type;
1571 type = value_type (lhs);
1572 }
1573
1574 /* Tuples and tuple structs */
1575 nfields = type->num_fields ();
1576
1577 if (field_number >= nfields || field_number < 0)
1578 {
1579 if (outer_type != NULL)
1580 error(_("Cannot access field %d of variant %s::%s, "
1581 "there are only %d fields"),
1582 field_number, outer_type->name (),
1583 rust_last_path_segment (type->name ()),
1584 nfields);
1585 else
1586 error(_("Cannot access field %d of %s, "
1587 "there are only %d fields"),
1588 field_number, type->name (), nfields);
1589 }
1590
1591 /* Tuples are tuple structs too. */
1592 if (!rust_tuple_struct_type_p (type))
1593 {
1594 if (outer_type != NULL)
1595 error(_("Variant %s::%s is not a tuple variant"),
1596 outer_type->name (),
1597 rust_last_path_segment (type->name ()));
1598 else
1599 error(_("Attempting to access anonymous field %d "
1600 "of %s, which is not a tuple, tuple struct, or "
1601 "tuple-like variant"),
1602 field_number, type->name ());
1603 }
1604
1605 result = value_primitive_field (lhs, 0, field_number, type);
1606 }
1607 else
1608 error(_("Anonymous field access is only allowed on tuples, \
1609 tuple structs, and tuple-like enum variants"));
1610 }
1611 break;
1612
1613 case STRUCTOP_STRUCT:
1614 {
1615 struct value *lhs;
1616 struct type *type;
1617 int tem, pc;
1618
1619 pc = (*pos)++;
1620 tem = longest_to_int (exp->elts[pc + 1].longconst);
1621 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1622 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1623
1624 const char *field_name = &exp->elts[pc + 2].string;
1625 type = value_type (lhs);
1626 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1627 {
1628 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1629 TYPE_LENGTH (type));
1630 type = resolve_dynamic_type (type, view, value_address (lhs));
1631
1632 if (rust_empty_enum_p (type))
1633 error (_("Cannot access field %s of empty enum %s"),
1634 field_name, type->name ());
1635
1636 int fieldno = rust_enum_variant (type);
1637 lhs = value_primitive_field (lhs, 0, fieldno, type);
1638
1639 struct type *outer_type = type;
1640 type = value_type (lhs);
1641 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1642 error (_("Attempting to access named field %s of tuple "
1643 "variant %s::%s, which has only anonymous fields"),
1644 field_name, outer_type->name (),
1645 rust_last_path_segment (type->name ()));
1646
1647 try
1648 {
1649 result = value_struct_elt (&lhs, NULL, field_name,
1650 NULL, "structure");
1651 }
1652 catch (const gdb_exception_error &except)
1653 {
1654 error (_("Could not find field %s of struct variant %s::%s"),
1655 field_name, outer_type->name (),
1656 rust_last_path_segment (type->name ()));
1657 }
1658 }
1659 else
1660 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1661 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1662 result = value_zero (value_type (result), VALUE_LVAL (result));
1663 }
1664 break;
1665
1666 case OP_RANGE:
1667 result = rust_range (exp, pos, noside);
1668 break;
1669
1670 case UNOP_ADDR:
1671 /* We might have &array[range], in which case we need to make a
1672 slice. */
1673 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1674 {
1675 ++*pos;
1676 result = rust_subscript (exp, pos, noside, 1);
1677 break;
1678 }
1679 /* Fall through. */
1680 default:
1681 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1682 break;
1683 }
1684
1685 return result;
1686 }
1687
1688 /* operator_length implementation for Rust. */
1689
1690 static void
1691 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1692 int *argsp)
1693 {
1694 int oplen = 1;
1695 int args = 0;
1696
1697 switch (exp->elts[pc - 1].opcode)
1698 {
1699 case OP_AGGREGATE:
1700 /* We handle aggregate as a type and argument count. The first
1701 argument might be OP_OTHERS. After that the arguments
1702 alternate: first an OP_NAME, then an expression. */
1703 oplen = 4;
1704 args = longest_to_int (exp->elts[pc - 2].longconst);
1705 break;
1706
1707 case OP_OTHERS:
1708 oplen = 1;
1709 args = 1;
1710 break;
1711
1712 case STRUCTOP_ANONYMOUS:
1713 oplen = 3;
1714 args = 1;
1715 break;
1716
1717 case OP_RUST_ARRAY:
1718 oplen = 1;
1719 args = 2;
1720 break;
1721
1722 default:
1723 operator_length_standard (exp, pc, oplenp, argsp);
1724 return;
1725 }
1726
1727 *oplenp = oplen;
1728 *argsp = args;
1729 }
1730
1731 /* op_name implementation for Rust. */
1732
1733 static const char *
1734 rust_op_name (enum exp_opcode opcode)
1735 {
1736 switch (opcode)
1737 {
1738 case OP_AGGREGATE:
1739 return "OP_AGGREGATE";
1740 case OP_OTHERS:
1741 return "OP_OTHERS";
1742 default:
1743 return op_name_standard (opcode);
1744 }
1745 }
1746
1747 /* dump_subexp_body implementation for Rust. */
1748
1749 static int
1750 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1751 int elt)
1752 {
1753 switch (exp->elts[elt].opcode)
1754 {
1755 case OP_AGGREGATE:
1756 {
1757 int length = longest_to_int (exp->elts[elt + 2].longconst);
1758 int i;
1759
1760 fprintf_filtered (stream, "Type @");
1761 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1762 fprintf_filtered (stream, " (");
1763 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1764 fprintf_filtered (stream, "), length %d", length);
1765
1766 elt += 4;
1767 for (i = 0; i < length; ++i)
1768 elt = dump_subexp (exp, stream, elt);
1769 }
1770 break;
1771
1772 case OP_STRING:
1773 case OP_NAME:
1774 {
1775 LONGEST len = exp->elts[elt + 1].longconst;
1776
1777 fprintf_filtered (stream, "%s: %s",
1778 (exp->elts[elt].opcode == OP_STRING
1779 ? "string" : "name"),
1780 &exp->elts[elt + 2].string);
1781 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1782 }
1783 break;
1784
1785 case OP_OTHERS:
1786 elt = dump_subexp (exp, stream, elt + 1);
1787 break;
1788
1789 case STRUCTOP_ANONYMOUS:
1790 {
1791 int field_number;
1792
1793 field_number = longest_to_int (exp->elts[elt + 1].longconst);
1794
1795 fprintf_filtered (stream, "Field number: %d", field_number);
1796 elt = dump_subexp (exp, stream, elt + 3);
1797 }
1798 break;
1799
1800 case OP_RUST_ARRAY:
1801 ++elt;
1802 break;
1803
1804 default:
1805 elt = dump_subexp_body_standard (exp, stream, elt);
1806 break;
1807 }
1808
1809 return elt;
1810 }
1811
1812 /* print_subexp implementation for Rust. */
1813
1814 static void
1815 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1816 enum precedence prec)
1817 {
1818 switch (exp->elts[*pos].opcode)
1819 {
1820 case OP_AGGREGATE:
1821 {
1822 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1823 int i;
1824
1825 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1826 fputs_filtered (" { ", stream);
1827
1828 *pos += 4;
1829 for (i = 0; i < length; ++i)
1830 {
1831 rust_print_subexp (exp, pos, stream, prec);
1832 fputs_filtered (", ", stream);
1833 }
1834 fputs_filtered (" }", stream);
1835 }
1836 break;
1837
1838 case OP_NAME:
1839 {
1840 LONGEST len = exp->elts[*pos + 1].longconst;
1841
1842 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1843 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1844 }
1845 break;
1846
1847 case OP_OTHERS:
1848 {
1849 fputs_filtered ("<<others>> (", stream);
1850 ++*pos;
1851 rust_print_subexp (exp, pos, stream, prec);
1852 fputs_filtered (")", stream);
1853 }
1854 break;
1855
1856 case STRUCTOP_ANONYMOUS:
1857 {
1858 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1859
1860 (*pos) += 3;
1861 print_subexp (exp, pos, stream, PREC_SUFFIX);
1862 fprintf_filtered (stream, ".%d", tem);
1863 }
1864 break;
1865
1866 case OP_RUST_ARRAY:
1867 ++*pos;
1868 fprintf_filtered (stream, "[");
1869 rust_print_subexp (exp, pos, stream, prec);
1870 fprintf_filtered (stream, "; ");
1871 rust_print_subexp (exp, pos, stream, prec);
1872 fprintf_filtered (stream, "]");
1873 break;
1874
1875 default:
1876 print_subexp_standard (exp, pos, stream, prec);
1877 break;
1878 }
1879 }
1880
1881 /* operator_check implementation for Rust. */
1882
1883 static int
1884 rust_operator_check (struct expression *exp, int pos,
1885 int (*objfile_func) (struct objfile *objfile,
1886 void *data),
1887 void *data)
1888 {
1889 switch (exp->elts[pos].opcode)
1890 {
1891 case OP_AGGREGATE:
1892 {
1893 struct type *type = exp->elts[pos + 1].type;
1894 struct objfile *objfile = TYPE_OBJFILE (type);
1895
1896 if (objfile != NULL && (*objfile_func) (objfile, data))
1897 return 1;
1898 }
1899 break;
1900
1901 case OP_OTHERS:
1902 case OP_NAME:
1903 case OP_RUST_ARRAY:
1904 break;
1905
1906 default:
1907 return operator_check_standard (exp, pos, objfile_func, data);
1908 }
1909
1910 return 0;
1911 }
1912
1913 \f
1914
1915 static const struct exp_descriptor exp_descriptor_rust =
1916 {
1917 rust_print_subexp,
1918 rust_operator_length,
1919 rust_operator_check,
1920 rust_op_name,
1921 rust_dump_subexp_body,
1922 rust_evaluate_subexp
1923 };
1924
1925 static const char *rust_extensions[] =
1926 {
1927 ".rs", NULL
1928 };
1929
1930 /* Constant data representing the Rust language. */
1931
1932 extern const struct language_data rust_language_data =
1933 {
1934 "rust",
1935 "Rust",
1936 language_rust,
1937 range_check_on,
1938 case_sensitive_on,
1939 array_row_major,
1940 macro_expansion_no,
1941 rust_extensions,
1942 &exp_descriptor_rust,
1943 NULL, /* name_of_this */
1944 false, /* la_store_sym_names_in_linkage_form_p */
1945 c_op_print_tab, /* expression operators for printing */
1946 1, /* c-style arrays */
1947 0, /* String lower bound */
1948 &default_varobj_ops,
1949 rust_is_string_type_p,
1950 "{...}" /* la_struct_too_deep_ellipsis */
1951 };
1952
1953 /* Class representing the Rust language. */
1954
1955 class rust_language : public language_defn
1956 {
1957 public:
1958 rust_language ()
1959 : language_defn (language_rust, rust_language_data)
1960 { /* Nothing. */ }
1961
1962 /* See language.h. */
1963 void language_arch_info (struct gdbarch *gdbarch,
1964 struct language_arch_info *lai) const override
1965 {
1966 const struct builtin_type *builtin = builtin_type (gdbarch);
1967
1968 struct type **types
1969 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1970 struct type *);
1971
1972 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1973 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1974 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1975 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1976 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1977 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1978 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1979 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1980 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1981 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1982
1983 unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1984 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1985 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1986
1987 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1988 floatformats_ieee_single);
1989 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1990 floatformats_ieee_double);
1991
1992 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1993
1994 struct type *tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1995 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1996 types[rust_primitive_usize]);
1997
1998 lai->primitive_type_vector = types;
1999 lai->bool_type_default = types[rust_primitive_bool];
2000 lai->string_char_type = types[rust_primitive_u8];
2001 }
2002
2003 /* See language.h. */
2004 bool sniff_from_mangled_name (const char *mangled,
2005 char **demangled) const override
2006 {
2007 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2008 return *demangled != NULL;
2009 }
2010
2011 /* See language.h. */
2012
2013 char *demangle (const char *mangled, int options) const override
2014 {
2015 return gdb_demangle (mangled, options);
2016 }
2017
2018 /* See language.h. */
2019
2020 void print_type (struct type *type, const char *varstring,
2021 struct ui_file *stream, int show, int level,
2022 const struct type_print_options *flags) const override
2023 {
2024 print_offset_data podata;
2025 rust_internal_print_type (type, varstring, stream, show, level,
2026 flags, false, &podata);
2027 }
2028
2029 /* See language.h. */
2030
2031 gdb::unique_xmalloc_ptr<char> watch_location_expression
2032 (struct type *type, CORE_ADDR addr) const override
2033 {
2034 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2035 std::string name = type_to_string (type);
2036 return gdb::unique_xmalloc_ptr<char>
2037 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2038 name.c_str ()));
2039 }
2040
2041 /* See language.h. */
2042
2043 void value_print_inner
2044 (struct value *val, struct ui_file *stream, int recurse,
2045 const struct value_print_options *options) const override
2046 {
2047 return rust_value_print_inner (val, stream, recurse, options);
2048 }
2049
2050 /* See language.h. */
2051
2052 struct block_symbol lookup_symbol_nonlocal
2053 (const char *name, const struct block *block,
2054 const domain_enum domain) const override
2055 {
2056 struct block_symbol result = {};
2057
2058 if (symbol_lookup_debug)
2059 {
2060 fprintf_unfiltered (gdb_stdlog,
2061 "rust_lookup_symbol_non_local"
2062 " (%s, %s (scope %s), %s)\n",
2063 name, host_address_to_string (block),
2064 block_scope (block), domain_name (domain));
2065 }
2066
2067 /* Look up bare names in the block's scope. */
2068 std::string scopedname;
2069 if (name[cp_find_first_component (name)] == '\0')
2070 {
2071 const char *scope = block_scope (block);
2072
2073 if (scope[0] != '\0')
2074 {
2075 scopedname = std::string (scope) + "::" + name;
2076 name = scopedname.c_str ();
2077 }
2078 else
2079 name = NULL;
2080 }
2081
2082 if (name != NULL)
2083 {
2084 result = lookup_symbol_in_static_block (name, block, domain);
2085 if (result.symbol == NULL)
2086 result = lookup_global_symbol (name, block, domain);
2087 }
2088 return result;
2089 }
2090
2091 /* See language.h. */
2092
2093 int parser (struct parser_state *ps) const override
2094 {
2095 return rust_parse (ps);
2096 }
2097
2098 /* See language.h. */
2099
2100 void emitchar (int ch, struct type *chtype,
2101 struct ui_file *stream, int quoter) const override
2102 {
2103 if (!rust_chartype_p (chtype))
2104 generic_emit_char (ch, chtype, stream, quoter,
2105 target_charset (get_type_arch (chtype)));
2106 else if (ch == '\\' || ch == quoter)
2107 fprintf_filtered (stream, "\\%c", ch);
2108 else if (ch == '\n')
2109 fputs_filtered ("\\n", stream);
2110 else if (ch == '\r')
2111 fputs_filtered ("\\r", stream);
2112 else if (ch == '\t')
2113 fputs_filtered ("\\t", stream);
2114 else if (ch == '\0')
2115 fputs_filtered ("\\0", stream);
2116 else if (ch >= 32 && ch <= 127 && isprint (ch))
2117 fputc_filtered (ch, stream);
2118 else if (ch <= 255)
2119 fprintf_filtered (stream, "\\x%02x", ch);
2120 else
2121 fprintf_filtered (stream, "\\u{%06x}", ch);
2122 }
2123
2124 /* See language.h. */
2125
2126 void printchar (int ch, struct type *chtype,
2127 struct ui_file *stream) const override
2128 {
2129 fputs_filtered ("'", stream);
2130 LA_EMIT_CHAR (ch, chtype, stream, '\'');
2131 fputs_filtered ("'", stream);
2132 }
2133
2134 /* See language.h. */
2135
2136 void printstr (struct ui_file *stream, struct type *elttype,
2137 const gdb_byte *string, unsigned int length,
2138 const char *encoding, int force_ellipses,
2139 const struct value_print_options *options) const override
2140 {
2141 rust_printstr (stream, elttype, string, length, encoding,
2142 force_ellipses, options);
2143 }
2144
2145 /* See language.h. */
2146
2147 void print_typedef (struct type *type, struct symbol *new_symbol,
2148 struct ui_file *stream) const override
2149 {
2150 type = check_typedef (type);
2151 fprintf_filtered (stream, "type %s = ", new_symbol->print_name ());
2152 type_print (type, "", stream, 0);
2153 fprintf_filtered (stream, ";");
2154 }
2155 };
2156
2157 /* Single instance of the Rust language class. */
2158
2159 static rust_language rust_language_defn;
This page took 0.101029 seconds and 4 git commands to generate.