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