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