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