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