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