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