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