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