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