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