Print void types correctly in Rust
[deliverable/binutils-gdb.git] / gdb / rust-lang.c
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"
28 #include "demangle.h"
29 #include "gdbarch.h"
30 #include "infcall.h"
31 #include "objfiles.h"
32 #include "rust-lang.h"
33 #include "valprint.h"
34 #include "varobj.h"
35
36 extern 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
42 static const char *
43 rust_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
56 char *
57 rust_crate_for_block (const struct block *block)
58 {
59 const char *scope = block_scope (block);
60
61 if (scope[0] == '\0')
62 return NULL;
63
64 return xstrndup (scope, cp_find_first_component (scope));
65 }
66
67 /* Information about the discriminant/variant of an enum */
68
69 struct 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
96 static struct disr_info
97 rust_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 {
124 char *name, *tail, *token;
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
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 {
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 }
156 if (fieldno >= TYPE_NFIELDS (member_type))
157 error (_("%s refers to field after end of member type"),
158 RUST_ENUM_PREFIX);
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)))
165 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
166 value = unpack_long (member_type, valaddr + embedded_offset);
167
168 if (value == 0)
169 {
170 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
171 ret.name = concat (TYPE_NAME (type), "::", token, (char *) NULL);
172 }
173 else
174 {
175 ret.field_no = RUST_ENCODED_ENUM_REAL;
176 ret.name = concat (TYPE_NAME (type), "::",
177 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))),
178 (char *) NULL);
179 }
180
181 do_cleanups (cleanup);
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
246 int
247 rust_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
261 static int
262 rust_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
292 int
293 rust_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
300 static int
301 rust_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
309 static int
310 rust_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
319 static int
320 rust_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
351 static int
352 rust_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
361 static int
362 rust_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
373 static void
374 rust_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
399 static void
400 rust_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
409 static void
410 rust_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
440 static 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 "()",
450 "[",
451 "]"
452 };
453
454 /* la_val_print implementation for Rust. */
455
456 static void
457 rust_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
708 static void
709 rust_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
721 static void
722 rust_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 /* Rust calls the unit type "void" in its debuginfo,
733 but we don't want to print it as that. */
734 if (TYPE_CODE (type) == TYPE_CODE_VOID)
735 fputs_filtered ("()", stream);
736 else
737 fputs_filtered (TYPE_NAME (type), stream);
738 return;
739 }
740
741 type = check_typedef (type);
742 switch (TYPE_CODE (type))
743 {
744 case TYPE_CODE_VOID:
745 fputs_filtered ("()", stream);
746 break;
747
748 case TYPE_CODE_FUNC:
749 /* Delegate varargs to the C printer. */
750 if (TYPE_VARARGS (type))
751 goto c_printer;
752
753 fputs_filtered ("fn ", stream);
754 if (varstring != NULL)
755 fputs_filtered (varstring, stream);
756 fputs_filtered ("(", stream);
757 for (i = 0; i < TYPE_NFIELDS (type); ++i)
758 {
759 QUIT;
760 if (i > 0)
761 fputs_filtered (", ", stream);
762 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
763 flags);
764 }
765 fputs_filtered (")", stream);
766 /* If it returns unit, we can omit the return type. */
767 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
768 {
769 fputs_filtered (" -> ", stream);
770 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
771 }
772 break;
773
774 case TYPE_CODE_ARRAY:
775 {
776 LONGEST low_bound, high_bound;
777
778 fputs_filtered ("[", stream);
779 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
780 stream, show - 1, level, flags);
781 fputs_filtered ("; ", stream);
782
783 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
784 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
785 fprintf_filtered (stream, "variable length");
786 else if (get_array_bounds (type, &low_bound, &high_bound))
787 fprintf_filtered (stream, "%s",
788 plongest (high_bound - low_bound + 1));
789 fputs_filtered ("]", stream);
790 }
791 break;
792
793 case TYPE_CODE_STRUCT:
794 {
795 int is_tuple_struct;
796
797 /* Print a tuple type simply. */
798 if (rust_tuple_type_p (type))
799 {
800 fputs_filtered (TYPE_TAG_NAME (type), stream);
801 break;
802 }
803
804 /* If we see a base class, delegate to C. */
805 if (TYPE_N_BASECLASSES (type) > 0)
806 goto c_printer;
807
808 fputs_filtered ("struct ", stream);
809 if (TYPE_TAG_NAME (type) != NULL)
810 fputs_filtered (TYPE_TAG_NAME (type), stream);
811
812 is_tuple_struct = rust_tuple_struct_type_p (type);
813
814 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
815 break;
816 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
817
818 for (i = 0; i < TYPE_NFIELDS (type); ++i)
819 {
820 const char *name;
821
822 QUIT;
823 if (field_is_static (&TYPE_FIELD (type, i)))
824 continue;
825
826 /* We'd like to print "pub" here as needed, but rustc
827 doesn't emit the debuginfo, and our types don't have
828 cplus_struct_type attached. */
829
830 /* For a tuple struct we print the type but nothing
831 else. */
832 print_spaces_filtered (level + 2, stream);
833 if (!is_tuple_struct)
834 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
835
836 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
837 stream, show - 1, level + 2,
838 flags);
839 fputs_filtered (",\n", stream);
840 }
841
842 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
843 }
844 break;
845
846 case TYPE_CODE_ENUM:
847 {
848 int i, len = 0;
849
850 fputs_filtered ("enum ", stream);
851 if (TYPE_TAG_NAME (type) != NULL)
852 {
853 fputs_filtered (TYPE_TAG_NAME (type), stream);
854 fputs_filtered (" ", stream);
855 len = strlen (TYPE_TAG_NAME (type));
856 }
857 fputs_filtered ("{\n", stream);
858
859 for (i = 0; i < TYPE_NFIELDS (type); ++i)
860 {
861 const char *name = TYPE_FIELD_NAME (type, i);
862
863 QUIT;
864
865 if (len > 0
866 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
867 && name[len] == ':'
868 && name[len + 1] == ':')
869 name += len + 2;
870 fprintfi_filtered (level + 2, stream, "%s,\n", name);
871 }
872
873 fputs_filtered ("}", stream);
874 }
875 break;
876
877 case TYPE_CODE_UNION:
878 {
879 /* ADT enums. */
880 int i, len = 0;
881 /* Skip the discriminant field. */
882 int skip_to = 1;
883
884 fputs_filtered ("enum ", stream);
885 if (TYPE_TAG_NAME (type) != NULL)
886 {
887 fputs_filtered (TYPE_TAG_NAME (type), stream);
888 fputs_filtered (" ", stream);
889 len = strlen (TYPE_TAG_NAME (type));
890 }
891 fputs_filtered ("{\n", stream);
892
893 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
894 strlen (RUST_ENUM_PREFIX)) == 0)
895 {
896 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
897 if (zero_field != NULL && strlen (zero_field) > 1)
898 {
899 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
900 /* There is no explicit discriminant field, skip nothing. */
901 skip_to = 0;
902 }
903 }
904
905 for (i = 0; i < TYPE_NFIELDS (type); ++i)
906 {
907 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
908 const char *name
909 = rust_last_path_segment (TYPE_NAME (variant_type));
910
911 fprintfi_filtered (level + 2, stream, "%s", name);
912
913 if (TYPE_NFIELDS (variant_type) > skip_to)
914 {
915 int first = 1;
916 int is_tuple = rust_tuple_variant_type_p (variant_type);
917 int j;
918
919 fputs_filtered (is_tuple ? "(" : "{", stream);
920 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
921 {
922 if (first)
923 first = 0;
924 else
925 fputs_filtered (", ", stream);
926
927 if (!is_tuple)
928 fprintf_filtered (stream, "%s: ",
929 TYPE_FIELD_NAME (variant_type, j));
930
931 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
932 stream, show - 1, level + 2,
933 flags);
934 }
935 fputs_filtered (is_tuple ? ")" : "}", stream);
936 }
937
938 fputs_filtered (",\n", stream);
939 }
940
941 fputs_filtered ("}", stream);
942 }
943 break;
944
945 default:
946 c_printer:
947 c_print_type (type, varstring, stream, show, level, flags);
948 }
949 }
950
951 \f
952
953 /* Compute the alignment of the type T. */
954
955 static int
956 rust_type_alignment (struct type *t)
957 {
958 t = check_typedef (t);
959 switch (TYPE_CODE (t))
960 {
961 default:
962 error (_("Could not compute alignment of type"));
963
964 case TYPE_CODE_PTR:
965 case TYPE_CODE_ENUM:
966 case TYPE_CODE_INT:
967 case TYPE_CODE_FLT:
968 case TYPE_CODE_REF:
969 case TYPE_CODE_CHAR:
970 case TYPE_CODE_BOOL:
971 return TYPE_LENGTH (t);
972
973 case TYPE_CODE_ARRAY:
974 case TYPE_CODE_COMPLEX:
975 return rust_type_alignment (TYPE_TARGET_TYPE (t));
976
977 case TYPE_CODE_STRUCT:
978 case TYPE_CODE_UNION:
979 {
980 int i;
981 int align = 1;
982
983 for (i = 0; i < TYPE_NFIELDS (t); ++i)
984 {
985 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
986 if (a > align)
987 align = a;
988 }
989 return align;
990 }
991 }
992 }
993
994 /* Like arch_composite_type, but uses TYPE to decide how to allocate
995 -- either on an obstack or on a gdbarch. */
996
997 static struct type *
998 rust_composite_type (struct type *original,
999 const char *name,
1000 const char *field1, struct type *type1,
1001 const char *field2, struct type *type2)
1002 {
1003 struct type *result = alloc_type_copy (original);
1004 int i, nfields, bitpos;
1005
1006 nfields = 0;
1007 if (field1 != NULL)
1008 ++nfields;
1009 if (field2 != NULL)
1010 ++nfields;
1011
1012 TYPE_CODE (result) = TYPE_CODE_STRUCT;
1013 TYPE_NAME (result) = name;
1014 TYPE_TAG_NAME (result) = name;
1015
1016 TYPE_NFIELDS (result) = nfields;
1017 TYPE_FIELDS (result)
1018 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1019
1020 i = 0;
1021 bitpos = 0;
1022 if (field1 != NULL)
1023 {
1024 struct field *field = &TYPE_FIELD (result, i);
1025
1026 SET_FIELD_BITPOS (*field, bitpos);
1027 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1028
1029 FIELD_NAME (*field) = field1;
1030 FIELD_TYPE (*field) = type1;
1031 ++i;
1032 }
1033 if (field2 != NULL)
1034 {
1035 struct field *field = &TYPE_FIELD (result, i);
1036 int align = rust_type_alignment (type2);
1037
1038 if (align != 0)
1039 {
1040 int delta;
1041
1042 align *= TARGET_CHAR_BIT;
1043 delta = bitpos % align;
1044 if (delta != 0)
1045 bitpos += align - delta;
1046 }
1047 SET_FIELD_BITPOS (*field, bitpos);
1048
1049 FIELD_NAME (*field) = field2;
1050 FIELD_TYPE (*field) = type2;
1051 ++i;
1052 }
1053
1054 if (i > 0)
1055 TYPE_LENGTH (result)
1056 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1057 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1058 return result;
1059 }
1060
1061 /* See rust-lang.h. */
1062
1063 struct type *
1064 rust_slice_type (const char *name, struct type *elt_type,
1065 struct type *usize_type)
1066 {
1067 struct type *type;
1068
1069 elt_type = lookup_pointer_type (elt_type);
1070 type = rust_composite_type (elt_type, name,
1071 "data_ptr", elt_type,
1072 "length", usize_type);
1073
1074 return type;
1075 }
1076
1077 enum rust_primitive_types
1078 {
1079 rust_primitive_bool,
1080 rust_primitive_char,
1081 rust_primitive_i8,
1082 rust_primitive_u8,
1083 rust_primitive_i16,
1084 rust_primitive_u16,
1085 rust_primitive_i32,
1086 rust_primitive_u32,
1087 rust_primitive_i64,
1088 rust_primitive_u64,
1089 rust_primitive_isize,
1090 rust_primitive_usize,
1091 rust_primitive_f32,
1092 rust_primitive_f64,
1093 rust_primitive_unit,
1094 rust_primitive_str,
1095 nr_rust_primitive_types
1096 };
1097
1098 /* la_language_arch_info implementation for Rust. */
1099
1100 static void
1101 rust_language_arch_info (struct gdbarch *gdbarch,
1102 struct language_arch_info *lai)
1103 {
1104 const struct builtin_type *builtin = builtin_type (gdbarch);
1105 struct type *tem;
1106 struct type **types;
1107 unsigned int length;
1108
1109 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1110 struct type *);
1111
1112 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1113 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1114 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1115 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1116 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1117 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1118 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1119 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1120 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1121 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1122
1123 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1124 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1125 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1126
1127 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32", NULL);
1128 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64", NULL);
1129
1130 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1131
1132 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1133 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1134 types[rust_primitive_usize]);
1135
1136 lai->primitive_type_vector = types;
1137 lai->bool_type_default = types[rust_primitive_bool];
1138 lai->string_char_type = types[rust_primitive_u8];
1139 }
1140
1141 \f
1142
1143 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1144
1145 static struct value *
1146 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1147 {
1148 int i;
1149 int num_args = exp->elts[*pos + 1].longconst;
1150 const char *method;
1151 char *name;
1152 struct value *function, *result, *arg0;
1153 struct value **args;
1154 struct cleanup *cleanup;
1155 struct type *type, *fn_type;
1156 const struct block *block;
1157 struct block_symbol sym;
1158
1159 /* For an ordinary function call we can simply defer to the
1160 generic implementation. */
1161 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1162 return evaluate_subexp_standard (NULL, exp, pos, noside);
1163
1164 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1165 *pos += 4;
1166 method = &exp->elts[*pos + 1].string;
1167 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1168
1169 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1170 type in order to look up the method. */
1171 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1172
1173 if (noside == EVAL_SKIP)
1174 {
1175 for (i = 0; i < num_args; ++i)
1176 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1177 return arg0;
1178 }
1179
1180 args = XNEWVEC (struct value *, num_args + 1);
1181 cleanup = make_cleanup (xfree, args);
1182 args[0] = arg0;
1183
1184 /* We don't yet implement real Deref semantics. */
1185 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1186 args[0] = value_ind (args[0]);
1187
1188 type = value_type (args[0]);
1189 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1190 && TYPE_CODE (type) != TYPE_CODE_UNION
1191 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1192 || rust_tuple_type_p (type))
1193 error (_("Method calls only supported on struct or enum types"));
1194 if (TYPE_TAG_NAME (type) == NULL)
1195 error (_("Method call on nameless type"));
1196
1197 name = concat (TYPE_TAG_NAME (type), "::", method, (char *) NULL);
1198 make_cleanup (xfree, name);
1199
1200 block = get_selected_block (0);
1201 sym = lookup_symbol (name, block, VAR_DOMAIN, NULL);
1202 if (sym.symbol == NULL)
1203 error (_("Could not find function named '%s'"), name);
1204
1205 fn_type = SYMBOL_TYPE (sym.symbol);
1206 if (TYPE_NFIELDS (fn_type) == 0)
1207 error (_("Function '%s' takes no arguments"), name);
1208
1209 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1210 args[0] = value_addr (args[0]);
1211
1212 function = address_of_variable (sym.symbol, block);
1213
1214 for (i = 0; i < num_args; ++i)
1215 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1216
1217 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1218 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1219 else
1220 result = call_function_by_hand (function, num_args + 1, args);
1221 do_cleanups (cleanup);
1222 return result;
1223 }
1224
1225 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1226
1227 static struct value *
1228 rust_range (struct expression *exp, int *pos, enum noside noside)
1229 {
1230 enum range_type kind;
1231 struct value *low = NULL, *high = NULL;
1232 struct value *addrval, *result;
1233 CORE_ADDR addr;
1234 struct type *range_type;
1235 struct type *index_type;
1236 struct type *temp_type;
1237 const char *name;
1238
1239 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1240 *pos += 3;
1241
1242 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1243 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1244 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1245 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1246
1247 if (noside == EVAL_SKIP)
1248 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1249
1250 if (low == NULL)
1251 {
1252 if (high == NULL)
1253 {
1254 index_type = NULL;
1255 name = "std::ops::RangeFull";
1256 }
1257 else
1258 {
1259 index_type = value_type (high);
1260 name = "std::ops::RangeTo";
1261 }
1262 }
1263 else
1264 {
1265 if (high == NULL)
1266 {
1267 index_type = value_type (low);
1268 name = "std::ops::RangeFrom";
1269 }
1270 else
1271 {
1272 if (!types_equal (value_type (low), value_type (high)))
1273 error (_("Range expression with different types"));
1274 index_type = value_type (low);
1275 name = "std::ops::Range";
1276 }
1277 }
1278
1279 /* If we don't have an index type, just allocate this on the
1280 arch. Here any type will do. */
1281 temp_type = (index_type == NULL
1282 ? language_bool_type (exp->language_defn, exp->gdbarch)
1283 : index_type);
1284 /* It would be nicer to cache the range type. */
1285 range_type = rust_composite_type (temp_type, name,
1286 low == NULL ? NULL : "start", index_type,
1287 high == NULL ? NULL : "end", index_type);
1288
1289 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1290 return value_zero (range_type, lval_memory);
1291
1292 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1293 addr = value_as_long (addrval);
1294 result = value_at_lazy (range_type, addr);
1295
1296 if (low != NULL)
1297 {
1298 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1299 "range");
1300
1301 value_assign (start, low);
1302 }
1303
1304 if (high != NULL)
1305 {
1306 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1307 "range");
1308
1309 value_assign (end, high);
1310 }
1311
1312 result = value_at_lazy (range_type, addr);
1313 return result;
1314 }
1315
1316 /* A helper function to compute the range and kind given a range
1317 value. TYPE is the type of the range value. RANGE is the range
1318 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1319 parameters might be filled in, or might not be, depending on the
1320 kind of range this is. KIND will always be set to the appropriate
1321 value describing the kind of range, and this can be used to
1322 determine whether LOW or HIGH are valid. */
1323
1324 static void
1325 rust_compute_range (struct type *type, struct value *range,
1326 LONGEST *low, LONGEST *high,
1327 enum range_type *kind)
1328 {
1329 int i;
1330
1331 *low = 0;
1332 *high = 0;
1333 *kind = BOTH_BOUND_DEFAULT;
1334
1335 if (TYPE_NFIELDS (type) == 0)
1336 return;
1337
1338 i = 0;
1339 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1340 {
1341 *kind = HIGH_BOUND_DEFAULT;
1342 *low = value_as_long (value_field (range, 0));
1343 ++i;
1344 }
1345 if (TYPE_NFIELDS (type) > i
1346 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1347 {
1348 *kind = (*kind == BOTH_BOUND_DEFAULT
1349 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1350 *high = value_as_long (value_field (range, i));
1351 }
1352 }
1353
1354 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1355
1356 static struct value *
1357 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1358 int for_addr)
1359 {
1360 struct value *lhs, *rhs, *result;
1361 struct type *rhstype;
1362 LONGEST low, high_bound;
1363 /* Initialized to appease the compiler. */
1364 enum range_type kind = BOTH_BOUND_DEFAULT;
1365 LONGEST high = 0;
1366 int want_slice = 0;
1367
1368 ++*pos;
1369 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1370 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1371
1372 if (noside == EVAL_SKIP)
1373 return lhs;
1374
1375 rhstype = check_typedef (value_type (rhs));
1376 if (rust_range_type_p (rhstype))
1377 {
1378 if (!for_addr)
1379 error (_("Can't take slice of array without '&'"));
1380 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1381 want_slice = 1;
1382 }
1383 else
1384 low = value_as_long (rhs);
1385
1386 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1387 {
1388 struct type *type = check_typedef (value_type (lhs));
1389
1390 result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
1391 }
1392 else
1393 {
1394 LONGEST low_bound;
1395 struct value *base;
1396 struct type *type = check_typedef (value_type (lhs));
1397
1398 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1399 {
1400 base = lhs;
1401 if (!get_array_bounds (type, &low_bound, &high_bound))
1402 error (_("Can't compute array bounds"));
1403 if (low_bound != 0)
1404 error (_("Found array with non-zero lower bound"));
1405 ++high_bound;
1406 }
1407 else if (rust_slice_type_p (type))
1408 {
1409 struct value *len;
1410
1411 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1412 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1413 low_bound = 0;
1414 high_bound = value_as_long (len);
1415 }
1416 else
1417 error (_("Cannot subscript non-array type"));
1418
1419 if (want_slice
1420 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1421 low = low_bound;
1422 if (low < 0)
1423 error (_("Index less than zero"));
1424 if (low > high_bound)
1425 error (_("Index greater than length"));
1426
1427 result = value_subscript (base, low);
1428 }
1429
1430 if (for_addr)
1431 {
1432 if (want_slice)
1433 {
1434 struct type *usize, *slice;
1435 CORE_ADDR addr;
1436 struct value *addrval, *tem;
1437
1438 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1439 high = high_bound;
1440 if (high < 0)
1441 error (_("High index less than zero"));
1442 if (low > high)
1443 error (_("Low index greater than high index"));
1444 if (high > high_bound)
1445 error (_("High index greater than length"));
1446
1447 usize = language_lookup_primitive_type (exp->language_defn,
1448 exp->gdbarch,
1449 "usize");
1450 slice = rust_slice_type ("&[*gdb*]", value_type (result),
1451 usize);
1452
1453 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1454 addr = value_as_long (addrval);
1455 tem = value_at_lazy (slice, addr);
1456
1457 value_assign (value_field (tem, 0), value_addr (result));
1458 value_assign (value_field (tem, 1),
1459 value_from_longest (usize, high - low));
1460
1461 result = value_at_lazy (slice, addr);
1462 }
1463 else
1464 result = value_addr (result);
1465 }
1466
1467 return result;
1468 }
1469
1470 /* evaluate_exp implementation for Rust. */
1471
1472 static struct value *
1473 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1474 int *pos, enum noside noside)
1475 {
1476 struct value *result;
1477
1478 switch (exp->elts[*pos].opcode)
1479 {
1480 case UNOP_COMPLEMENT:
1481 {
1482 struct value *value;
1483
1484 ++*pos;
1485 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1486 if (noside == EVAL_SKIP)
1487 {
1488 /* Preserving the type is enough. */
1489 return value;
1490 }
1491 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1492 result = value_from_longest (value_type (value),
1493 value_logical_not (value));
1494 else
1495 result = value_complement (value);
1496 }
1497 break;
1498
1499 case BINOP_SUBSCRIPT:
1500 result = rust_subscript (exp, pos, noside, 0);
1501 break;
1502
1503 case OP_FUNCALL:
1504 result = rust_evaluate_funcall (exp, pos, noside);
1505 break;
1506
1507 case OP_AGGREGATE:
1508 {
1509 int pc = (*pos)++;
1510 struct type *type = exp->elts[pc + 1].type;
1511 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1512 int i;
1513 CORE_ADDR addr = 0;
1514 struct value *addrval = NULL;
1515
1516 *pos += 3;
1517
1518 if (noside == EVAL_NORMAL)
1519 {
1520 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1521 addr = value_as_long (addrval);
1522 result = value_at_lazy (type, addr);
1523 }
1524
1525 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1526 {
1527 struct value *init;
1528
1529 ++*pos;
1530 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1531 if (noside == EVAL_NORMAL)
1532 {
1533 /* This isn't quite right but will do for the time
1534 being, seeing that we can't implement the Copy
1535 trait anyway. */
1536 value_assign (result, init);
1537 }
1538
1539 --arglen;
1540 }
1541
1542 gdb_assert (arglen % 2 == 0);
1543 for (i = 0; i < arglen; i += 2)
1544 {
1545 int len;
1546 const char *fieldname;
1547 struct value *value, *field;
1548
1549 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1550 ++*pos;
1551 len = longest_to_int (exp->elts[*pos].longconst);
1552 ++*pos;
1553 fieldname = &exp->elts[*pos].string;
1554 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1555
1556 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1557 if (noside == EVAL_NORMAL)
1558 {
1559 field = value_struct_elt (&result, NULL, fieldname, NULL,
1560 "structure");
1561 value_assign (field, value);
1562 }
1563 }
1564
1565 if (noside == EVAL_SKIP)
1566 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1567 1);
1568 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1569 result = allocate_value (type);
1570 else
1571 result = value_at_lazy (type, addr);
1572 }
1573 break;
1574
1575 case OP_RUST_ARRAY:
1576 {
1577 int pc = (*pos)++;
1578 int copies;
1579 struct value *elt;
1580 struct value *ncopies;
1581
1582 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1583 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1584 copies = value_as_long (ncopies);
1585 if (copies < 0)
1586 error (_("Array with negative number of elements"));
1587
1588 if (noside == EVAL_NORMAL)
1589 {
1590 CORE_ADDR addr;
1591 int i;
1592 struct value **eltvec = XNEWVEC (struct value *, copies);
1593 struct cleanup *cleanup = make_cleanup (xfree, eltvec);
1594
1595 for (i = 0; i < copies; ++i)
1596 eltvec[i] = elt;
1597 result = value_array (0, copies - 1, eltvec);
1598
1599 do_cleanups (cleanup);
1600 }
1601 else
1602 {
1603 struct type *arraytype
1604 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1605 result = allocate_value (arraytype);
1606 }
1607 }
1608 break;
1609
1610 case STRUCTOP_ANONYMOUS:
1611 {
1612 /* Anonymous field access, i.e. foo.1. */
1613 struct value *lhs;
1614 int pc, field_number, nfields;
1615 struct type *type, *variant_type;
1616 struct disr_info disr;
1617
1618 pc = (*pos)++;
1619 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1620 (*pos) += 2;
1621 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1622
1623 type = value_type (lhs);
1624 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1625 {
1626 struct cleanup *cleanup;
1627
1628 disr = rust_get_disr_info (type, value_contents (lhs),
1629 value_embedded_offset (lhs),
1630 value_address (lhs), lhs);
1631
1632 cleanup = make_cleanup (xfree, disr.name);
1633
1634 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1635 {
1636 variant_type = NULL;
1637 nfields = 0;
1638 }
1639 else
1640 {
1641 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1642 nfields = TYPE_NFIELDS (variant_type);
1643 }
1644
1645 if (!disr.is_encoded)
1646 ++field_number;
1647
1648 if (field_number >= nfields || field_number < 0)
1649 error(_("Cannot access field %d of variant %s, \
1650 there are only %d fields"),
1651 disr.is_encoded ? field_number : field_number - 1,
1652 disr.name,
1653 disr.is_encoded ? nfields : nfields - 1);
1654
1655 if (!(disr.is_encoded
1656 ? rust_tuple_struct_type_p (variant_type)
1657 : rust_tuple_variant_type_p (variant_type)))
1658 error(_("Variant %s is not a tuple variant"), disr.name);
1659
1660 result = value_primitive_field (lhs, 0, field_number,
1661 variant_type);
1662 do_cleanups (cleanup);
1663 }
1664 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1665 {
1666 /* Tuples and tuple structs */
1667 nfields = TYPE_NFIELDS(type);
1668
1669 if (field_number >= nfields || field_number < 0)
1670 error(_("Cannot access field %d of %s, there are only %d fields"),
1671 field_number, TYPE_TAG_NAME (type), nfields);
1672
1673 /* Tuples are tuple structs too. */
1674 if (!rust_tuple_struct_type_p (type))
1675 error(_("Attempting to access anonymous field %d of %s, which is \
1676 not a tuple, tuple struct, or tuple-like variant"),
1677 field_number, TYPE_TAG_NAME (type));
1678
1679 result = value_primitive_field (lhs, 0, field_number, type);
1680 }
1681 else
1682 error(_("Anonymous field access is only allowed on tuples, \
1683 tuple structs, and tuple-like enum variants"));
1684 }
1685 break;
1686
1687 case STRUCTOP_STRUCT:
1688 {
1689 struct value* lhs;
1690 struct type *type;
1691 int tem, pc;
1692
1693 pc = (*pos)++;
1694 tem = longest_to_int (exp->elts[pc + 1].longconst);
1695 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1696 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1697
1698 type = value_type (lhs);
1699
1700 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1701 {
1702 int i, start;
1703 struct disr_info disr;
1704 struct cleanup* cleanup;
1705 struct type* variant_type;
1706 char* field_name;
1707
1708 field_name = &exp->elts[pc + 2].string;
1709
1710 disr = rust_get_disr_info (type, value_contents (lhs),
1711 value_embedded_offset (lhs),
1712 value_address (lhs), lhs);
1713
1714 cleanup = make_cleanup (xfree, disr.name);
1715
1716 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1717 error(_("Could not find field %s of struct variant %s"),
1718 field_name, disr.name);
1719
1720 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1721
1722 if (variant_type == NULL
1723 || rust_tuple_variant_type_p (variant_type))
1724 error(_("Attempting to access named field %s of tuple variant %s, \
1725 which has only anonymous fields"),
1726 field_name, disr.name);
1727
1728 start = disr.is_encoded ? 0 : 1;
1729 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1730 {
1731 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1732 field_name) == 0) {
1733 result = value_primitive_field (lhs, 0, i, variant_type);
1734 break;
1735 }
1736 }
1737
1738 if (i == TYPE_NFIELDS (variant_type))
1739 /* We didn't find it. */
1740 error(_("Could not find field %s of struct variant %s"),
1741 field_name, disr.name);
1742
1743 do_cleanups (cleanup);
1744 }
1745 else
1746 {
1747 *pos = pc;
1748 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1749 }
1750 }
1751 break;
1752
1753 case OP_RANGE:
1754 result = rust_range (exp, pos, noside);
1755 break;
1756
1757 case UNOP_ADDR:
1758 /* We might have &array[range], in which case we need to make a
1759 slice. */
1760 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1761 {
1762 ++*pos;
1763 result = rust_subscript (exp, pos, noside, 1);
1764 break;
1765 }
1766 /* Fall through. */
1767 default:
1768 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1769 break;
1770 }
1771
1772 return result;
1773 }
1774
1775 /* operator_length implementation for Rust. */
1776
1777 static void
1778 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1779 int *argsp)
1780 {
1781 int oplen = 1;
1782 int args = 0;
1783
1784 switch (exp->elts[pc - 1].opcode)
1785 {
1786 case OP_AGGREGATE:
1787 /* We handle aggregate as a type and argument count. The first
1788 argument might be OP_OTHERS. After that the arguments
1789 alternate: first an OP_NAME, then an expression. */
1790 oplen = 4;
1791 args = longest_to_int (exp->elts[pc - 2].longconst);
1792 break;
1793
1794 case OP_OTHERS:
1795 oplen = 1;
1796 args = 1;
1797 break;
1798
1799 case STRUCTOP_ANONYMOUS:
1800 oplen = 3;
1801 args = 1;
1802 break;
1803
1804 case OP_RUST_ARRAY:
1805 oplen = 1;
1806 args = 2;
1807 break;
1808
1809 default:
1810 operator_length_standard (exp, pc, oplenp, argsp);
1811 return;
1812 }
1813
1814 *oplenp = oplen;
1815 *argsp = args;
1816 }
1817
1818 /* op_name implementation for Rust. */
1819
1820 static char *
1821 rust_op_name (enum exp_opcode opcode)
1822 {
1823 switch (opcode)
1824 {
1825 case OP_AGGREGATE:
1826 return "OP_AGGREGATE";
1827 case OP_OTHERS:
1828 return "OP_OTHERS";
1829 default:
1830 return op_name_standard (opcode);
1831 }
1832 }
1833
1834 /* dump_subexp_body implementation for Rust. */
1835
1836 static int
1837 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1838 int elt)
1839 {
1840 switch (exp->elts[elt].opcode)
1841 {
1842 case OP_AGGREGATE:
1843 {
1844 int length = longest_to_int (exp->elts[elt + 2].longconst);
1845 int i;
1846
1847 fprintf_filtered (stream, "Type @");
1848 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1849 fprintf_filtered (stream, " (");
1850 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1851 fprintf_filtered (stream, "), length %d", length);
1852
1853 elt += 4;
1854 for (i = 0; i < length; ++i)
1855 elt = dump_subexp (exp, stream, elt);
1856 }
1857 break;
1858
1859 case OP_STRING:
1860 case OP_NAME:
1861 {
1862 LONGEST len = exp->elts[elt + 1].longconst;
1863
1864 fprintf_filtered (stream, "%s: %s",
1865 (exp->elts[elt].opcode == OP_STRING
1866 ? "string" : "name"),
1867 &exp->elts[elt + 2].string);
1868 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1869 }
1870 break;
1871
1872 case OP_OTHERS:
1873 elt = dump_subexp (exp, stream, elt + 1);
1874 break;
1875
1876 case STRUCTOP_ANONYMOUS:
1877 {
1878 int field_number;
1879
1880 field_number = longest_to_int (exp->elts[elt].longconst);
1881
1882 fprintf_filtered (stream, "Field number: %d", field_number);
1883 elt = dump_subexp (exp, stream, elt + 2);
1884 }
1885 break;
1886
1887 case OP_RUST_ARRAY:
1888 break;
1889
1890 default:
1891 elt = dump_subexp_body_standard (exp, stream, elt);
1892 break;
1893 }
1894
1895 return elt;
1896 }
1897
1898 /* print_subexp implementation for Rust. */
1899
1900 static void
1901 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1902 enum precedence prec)
1903 {
1904 switch (exp->elts[*pos].opcode)
1905 {
1906 case OP_AGGREGATE:
1907 {
1908 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1909 int i;
1910
1911 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1912 fputs_filtered (" { ", stream);
1913
1914 *pos += 4;
1915 for (i = 0; i < length; ++i)
1916 {
1917 rust_print_subexp (exp, pos, stream, prec);
1918 fputs_filtered (", ", stream);
1919 }
1920 fputs_filtered (" }", stream);
1921 }
1922 break;
1923
1924 case OP_NAME:
1925 {
1926 LONGEST len = exp->elts[*pos + 1].longconst;
1927
1928 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1929 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1930 }
1931 break;
1932
1933 case OP_OTHERS:
1934 {
1935 fputs_filtered ("<<others>> (", stream);
1936 ++*pos;
1937 rust_print_subexp (exp, pos, stream, prec);
1938 fputs_filtered (")", stream);
1939 }
1940 break;
1941
1942 case STRUCTOP_ANONYMOUS:
1943 {
1944 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1945
1946 (*pos) += 3;
1947 print_subexp (exp, pos, stream, PREC_SUFFIX);
1948 fprintf_filtered (stream, ".%d", tem);
1949 }
1950 return;
1951
1952 case OP_RUST_ARRAY:
1953 ++*pos;
1954 fprintf_filtered (stream, "[");
1955 rust_print_subexp (exp, pos, stream, prec);
1956 fprintf_filtered (stream, "; ");
1957 rust_print_subexp (exp, pos, stream, prec);
1958 fprintf_filtered (stream, "]");
1959 break;
1960
1961 default:
1962 print_subexp_standard (exp, pos, stream, prec);
1963 break;
1964 }
1965 }
1966
1967 /* operator_check implementation for Rust. */
1968
1969 static int
1970 rust_operator_check (struct expression *exp, int pos,
1971 int (*objfile_func) (struct objfile *objfile,
1972 void *data),
1973 void *data)
1974 {
1975 switch (exp->elts[pos].opcode)
1976 {
1977 case OP_AGGREGATE:
1978 {
1979 struct type *type = exp->elts[pos + 1].type;
1980 struct objfile *objfile = TYPE_OBJFILE (type);
1981
1982 if (objfile != NULL && (*objfile_func) (objfile, data))
1983 return 1;
1984 }
1985 break;
1986
1987 case OP_OTHERS:
1988 case OP_NAME:
1989 case OP_RUST_ARRAY:
1990 break;
1991
1992 default:
1993 return operator_check_standard (exp, pos, objfile_func, data);
1994 }
1995
1996 return 0;
1997 }
1998
1999 \f
2000
2001 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
2002
2003 static struct block_symbol
2004 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2005 const char *name,
2006 const struct block *block,
2007 const domain_enum domain)
2008 {
2009 struct block_symbol result = {NULL, NULL};
2010
2011 if (symbol_lookup_debug)
2012 {
2013 fprintf_unfiltered (gdb_stdlog,
2014 "rust_lookup_symbol_non_local"
2015 " (%s, %s (scope %s), %s)\n",
2016 name, host_address_to_string (block),
2017 block_scope (block), domain_name (domain));
2018 }
2019
2020 /* Look up bare names in the block's scope. */
2021 if (name[cp_find_first_component (name)] == '\0')
2022 {
2023 const char *scope = block_scope (block);
2024
2025 if (scope[0] != '\0')
2026 {
2027 char *scopedname = concat (scope, "::", name, (char *) NULL);
2028 struct cleanup *cleanup = make_cleanup (xfree, scopedname);
2029
2030 result = lookup_symbol_in_static_block (scopedname, block,
2031 domain);
2032 if (result.symbol == NULL)
2033 result = lookup_global_symbol (scopedname, block, domain);
2034 do_cleanups (cleanup);
2035 }
2036 }
2037 return result;
2038 }
2039
2040 \f
2041
2042 /* la_sniff_from_mangled_name for Rust. */
2043
2044 static int
2045 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2046 {
2047 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2048 return *demangled != NULL;
2049 }
2050
2051 \f
2052
2053 static const struct exp_descriptor exp_descriptor_rust =
2054 {
2055 rust_print_subexp,
2056 rust_operator_length,
2057 rust_operator_check,
2058 rust_op_name,
2059 rust_dump_subexp_body,
2060 rust_evaluate_subexp
2061 };
2062
2063 static const char *rust_extensions[] =
2064 {
2065 ".rs", NULL
2066 };
2067
2068 static const struct language_defn rust_language_defn =
2069 {
2070 "rust",
2071 "Rust",
2072 language_rust,
2073 range_check_on,
2074 case_sensitive_on,
2075 array_row_major,
2076 macro_expansion_no,
2077 rust_extensions,
2078 &exp_descriptor_rust,
2079 rust_parse,
2080 rustyyerror,
2081 null_post_parser,
2082 rust_printchar, /* Print a character constant */
2083 rust_printstr, /* Function to print string constant */
2084 rust_emitchar, /* Print a single char */
2085 rust_print_type, /* Print a type using appropriate syntax */
2086 rust_print_typedef, /* Print a typedef using appropriate syntax */
2087 rust_val_print, /* Print a value using appropriate syntax */
2088 c_value_print, /* Print a top-level value */
2089 default_read_var_value, /* la_read_var_value */
2090 NULL, /* Language specific skip_trampoline */
2091 NULL, /* name_of_this */
2092 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2093 basic_lookup_transparent_type,/* lookup_transparent_type */
2094 gdb_demangle, /* Language specific symbol demangler */
2095 rust_sniff_from_mangled_name,
2096 NULL, /* Language specific
2097 class_name_from_physname */
2098 c_op_print_tab, /* expression operators for printing */
2099 1, /* c-style arrays */
2100 0, /* String lower bound */
2101 default_word_break_characters,
2102 default_make_symbol_completion_list,
2103 rust_language_arch_info,
2104 default_print_array_index,
2105 default_pass_by_reference,
2106 c_get_string,
2107 NULL, /* la_get_symbol_name_cmp */
2108 iterate_over_symbols,
2109 &default_varobj_ops,
2110 NULL,
2111 NULL,
2112 LANG_MAGIC
2113 };
2114
2115 void
2116 _initialize_rust_language (void)
2117 {
2118 add_language (&rust_language_defn);
2119 }
This page took 0.090497 seconds and 5 git commands to generate.