Add missing format for built-in floating-point types
[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"
8b302db8 28#include "demangle.h"
c44af4eb
TT
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 {
9bf74fb2 124 char *tail, *token, *name, *saveptr = NULL;
c44af4eb
TT
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
b5a4b3c5
MG
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. */
a405c228
MG
148 for (token = strtok_r (tail, "$", &saveptr);
149 token != NULL;
150 token = strtok_r (NULL, "$", &saveptr))
b5a4b3c5 151 {
9e8a8ea8
TT
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 }
b5a4b3c5 158 if (fieldno >= TYPE_NFIELDS (member_type))
9e8a8ea8
TT
159 error (_("%s refers to field after end of member type"),
160 RUST_ENUM_PREFIX);
b5a4b3c5
MG
161
162 embedded_offset += TYPE_FIELD_BITPOS (member_type, fieldno) / 8;
163 member_type = TYPE_FIELD_TYPE (member_type, fieldno);
164 }
165
a405c228 166 if (token == NULL)
c44af4eb 167 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
9e8a8ea8 168 value = unpack_long (member_type, valaddr + embedded_offset);
c44af4eb 169
c44af4eb
TT
170 if (value == 0)
171 {
172 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
b5a4b3c5 173 ret.name = concat (TYPE_NAME (type), "::", token, (char *) NULL);
c44af4eb
TT
174 }
175 else
176 {
177 ret.field_no = RUST_ENCODED_ENUM_REAL;
178 ret.name = concat (TYPE_NAME (type), "::",
b5a4b3c5 179 rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))),
c44af4eb
TT
180 (char *) NULL);
181 }
182
b5a4b3c5 183 do_cleanups (cleanup);
c44af4eb
TT
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
248int
249rust_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
263static int
264rust_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
294int
295rust_tuple_struct_type_p (struct type *type)
296{
12df5c00
TT
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);
c44af4eb
TT
301}
302
303/* Return true if a variant TYPE is a tuple variant, false otherwise. */
304
305static int
306rust_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
314static int
315rust_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
324static int
325rust_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
356static int
357rust_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
366static int
367rust_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
378static void
379rust_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
404static void
405rust_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
414static void
415rust_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
445static 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",
921d8f54 454 "()",
c44af4eb
TT
455 "[",
456 "]"
457};
458
459/* la_val_print implementation for Rust. */
460
461static void
462rust_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
713static void
714rust_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
726static void
727rust_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 {
921d8f54
MG
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);
c44af4eb
TT
743 return;
744 }
745
746 type = check_typedef (type);
747 switch (TYPE_CODE (type))
748 {
921d8f54
MG
749 case TYPE_CODE_VOID:
750 fputs_filtered ("()", stream);
751 break;
752
c44af4eb
TT
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 }
921d8f54
MG
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 }
c44af4eb
TT
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 {
b5a4b3c5 884 /* ADT enums. */
c44af4eb 885 int i, len = 0;
b5a4b3c5
MG
886 /* Skip the discriminant field. */
887 int skip_to = 1;
c44af4eb
TT
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);
c44af4eb 894 }
b5a4b3c5
MG
895 fputs_filtered ("{\n", stream);
896
9e8a8ea8
TT
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 }
c44af4eb
TT
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
b5a4b3c5 917 if (TYPE_NFIELDS (variant_type) > skip_to)
c44af4eb
TT
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);
b5a4b3c5 924 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
c44af4eb
TT
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
959static int
960rust_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
1001static struct type *
1002rust_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
1067struct type *
1068rust_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
1081enum 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
1104static void
1105rust_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
49f190bc
UW
1131 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1132 floatformats_ieee_single);
1133 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1134 floatformats_ieee_double);
c44af4eb
TT
1135
1136 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1137
1138 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1139 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1140 types[rust_primitive_usize]);
1141
1142 lai->primitive_type_vector = types;
1143 lai->bool_type_default = types[rust_primitive_bool];
1144 lai->string_char_type = types[rust_primitive_u8];
1145}
1146
1147\f
1148
1149/* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1150
1151static struct value *
1152rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1153{
1154 int i;
1155 int num_args = exp->elts[*pos + 1].longconst;
1156 const char *method;
1157 char *name;
1158 struct value *function, *result, *arg0;
1159 struct value **args;
1160 struct cleanup *cleanup;
1161 struct type *type, *fn_type;
1162 const struct block *block;
1163 struct block_symbol sym;
1164
1165 /* For an ordinary function call we can simply defer to the
1166 generic implementation. */
1167 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1168 return evaluate_subexp_standard (NULL, exp, pos, noside);
1169
1170 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1171 *pos += 4;
1172 method = &exp->elts[*pos + 1].string;
1173 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1174
1175 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1176 type in order to look up the method. */
1177 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1178
1179 if (noside == EVAL_SKIP)
1180 {
1181 for (i = 0; i < num_args; ++i)
1182 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1183 return arg0;
1184 }
1185
1186 args = XNEWVEC (struct value *, num_args + 1);
1187 cleanup = make_cleanup (xfree, args);
1188 args[0] = arg0;
1189
1190 /* We don't yet implement real Deref semantics. */
1191 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1192 args[0] = value_ind (args[0]);
1193
1194 type = value_type (args[0]);
1195 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1196 && TYPE_CODE (type) != TYPE_CODE_UNION
1197 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1198 || rust_tuple_type_p (type))
1199 error (_("Method calls only supported on struct or enum types"));
1200 if (TYPE_TAG_NAME (type) == NULL)
1201 error (_("Method call on nameless type"));
1202
1203 name = concat (TYPE_TAG_NAME (type), "::", method, (char *) NULL);
1204 make_cleanup (xfree, name);
1205
1206 block = get_selected_block (0);
1207 sym = lookup_symbol (name, block, VAR_DOMAIN, NULL);
1208 if (sym.symbol == NULL)
1209 error (_("Could not find function named '%s'"), name);
1210
1211 fn_type = SYMBOL_TYPE (sym.symbol);
1212 if (TYPE_NFIELDS (fn_type) == 0)
1213 error (_("Function '%s' takes no arguments"), name);
1214
1215 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1216 args[0] = value_addr (args[0]);
1217
1218 function = address_of_variable (sym.symbol, block);
1219
1220 for (i = 0; i < num_args; ++i)
1221 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1222
1223 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1224 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1225 else
1226 result = call_function_by_hand (function, num_args + 1, args);
1227 do_cleanups (cleanup);
1228 return result;
1229}
1230
01739a3b 1231/* A helper for rust_evaluate_subexp that handles OP_RANGE. */
c44af4eb
TT
1232
1233static struct value *
1234rust_range (struct expression *exp, int *pos, enum noside noside)
1235{
01739a3b 1236 enum range_type kind;
c44af4eb
TT
1237 struct value *low = NULL, *high = NULL;
1238 struct value *addrval, *result;
1239 CORE_ADDR addr;
1240 struct type *range_type;
1241 struct type *index_type;
1242 struct type *temp_type;
1243 const char *name;
1244
01739a3b 1245 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
c44af4eb
TT
1246 *pos += 3;
1247
1248 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1249 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1250 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1251 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1252
1253 if (noside == EVAL_SKIP)
1254 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1255
1256 if (low == NULL)
1257 {
1258 if (high == NULL)
1259 {
1260 index_type = NULL;
1261 name = "std::ops::RangeFull";
1262 }
1263 else
1264 {
1265 index_type = value_type (high);
1266 name = "std::ops::RangeTo";
1267 }
1268 }
1269 else
1270 {
1271 if (high == NULL)
1272 {
1273 index_type = value_type (low);
1274 name = "std::ops::RangeFrom";
1275 }
1276 else
1277 {
1278 if (!types_equal (value_type (low), value_type (high)))
1279 error (_("Range expression with different types"));
1280 index_type = value_type (low);
1281 name = "std::ops::Range";
1282 }
1283 }
1284
1285 /* If we don't have an index type, just allocate this on the
1286 arch. Here any type will do. */
1287 temp_type = (index_type == NULL
1288 ? language_bool_type (exp->language_defn, exp->gdbarch)
1289 : index_type);
1290 /* It would be nicer to cache the range type. */
1291 range_type = rust_composite_type (temp_type, name,
1292 low == NULL ? NULL : "start", index_type,
1293 high == NULL ? NULL : "end", index_type);
1294
1295 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1296 return value_zero (range_type, lval_memory);
1297
1298 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1299 addr = value_as_long (addrval);
1300 result = value_at_lazy (range_type, addr);
1301
1302 if (low != NULL)
1303 {
1304 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1305 "range");
1306
1307 value_assign (start, low);
1308 }
1309
1310 if (high != NULL)
1311 {
1312 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1313 "range");
1314
1315 value_assign (end, high);
1316 }
1317
1318 result = value_at_lazy (range_type, addr);
1319 return result;
1320}
1321
1322/* A helper function to compute the range and kind given a range
1323 value. TYPE is the type of the range value. RANGE is the range
1324 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1325 parameters might be filled in, or might not be, depending on the
1326 kind of range this is. KIND will always be set to the appropriate
1327 value describing the kind of range, and this can be used to
1328 determine whether LOW or HIGH are valid. */
1329
1330static void
1331rust_compute_range (struct type *type, struct value *range,
1332 LONGEST *low, LONGEST *high,
01739a3b 1333 enum range_type *kind)
c44af4eb
TT
1334{
1335 int i;
1336
1337 *low = 0;
1338 *high = 0;
1339 *kind = BOTH_BOUND_DEFAULT;
1340
1341 if (TYPE_NFIELDS (type) == 0)
1342 return;
1343
1344 i = 0;
1345 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1346 {
1347 *kind = HIGH_BOUND_DEFAULT;
1348 *low = value_as_long (value_field (range, 0));
1349 ++i;
1350 }
1351 if (TYPE_NFIELDS (type) > i
1352 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1353 {
1354 *kind = (*kind == BOTH_BOUND_DEFAULT
1355 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1356 *high = value_as_long (value_field (range, i));
1357 }
1358}
1359
1360/* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1361
1362static struct value *
1363rust_subscript (struct expression *exp, int *pos, enum noside noside,
1364 int for_addr)
1365{
1366 struct value *lhs, *rhs, *result;
1367 struct type *rhstype;
45f4ed92 1368 LONGEST low, high_bound;
c44af4eb 1369 /* Initialized to appease the compiler. */
01739a3b 1370 enum range_type kind = BOTH_BOUND_DEFAULT;
45f4ed92 1371 LONGEST high = 0;
c44af4eb
TT
1372 int want_slice = 0;
1373
1374 ++*pos;
1375 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1376 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1377
1378 if (noside == EVAL_SKIP)
1379 return lhs;
1380
1381 rhstype = check_typedef (value_type (rhs));
1382 if (rust_range_type_p (rhstype))
1383 {
1384 if (!for_addr)
1385 error (_("Can't take slice of array without '&'"));
1386 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1387 want_slice = 1;
1388 }
1389 else
1390 low = value_as_long (rhs);
1391
1392 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1393 {
1394 struct type *type = check_typedef (value_type (lhs));
1395
1396 result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
1397 }
1398 else
1399 {
1400 LONGEST low_bound;
1401 struct value *base;
1402 struct type *type = check_typedef (value_type (lhs));
1403
1404 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1405 {
1406 base = lhs;
1407 if (!get_array_bounds (type, &low_bound, &high_bound))
1408 error (_("Can't compute array bounds"));
1409 if (low_bound != 0)
1410 error (_("Found array with non-zero lower bound"));
1411 ++high_bound;
1412 }
1413 else if (rust_slice_type_p (type))
1414 {
1415 struct value *len;
1416
1417 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1418 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1419 low_bound = 0;
1420 high_bound = value_as_long (len);
1421 }
42d94011
MG
1422 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1423 {
1424 base = lhs;
1425 low_bound = 0;
1426 high_bound = LONGEST_MAX;
1427 }
c44af4eb
TT
1428 else
1429 error (_("Cannot subscript non-array type"));
1430
1431 if (want_slice
1432 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1433 low = low_bound;
1434 if (low < 0)
1435 error (_("Index less than zero"));
1436 if (low > high_bound)
1437 error (_("Index greater than length"));
1438
1439 result = value_subscript (base, low);
1440 }
1441
1442 if (for_addr)
1443 {
1444 if (want_slice)
1445 {
1446 struct type *usize, *slice;
1447 CORE_ADDR addr;
1448 struct value *addrval, *tem;
1449
1450 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1451 high = high_bound;
1452 if (high < 0)
1453 error (_("High index less than zero"));
1454 if (low > high)
1455 error (_("Low index greater than high index"));
1456 if (high > high_bound)
1457 error (_("High index greater than length"));
1458
1459 usize = language_lookup_primitive_type (exp->language_defn,
1460 exp->gdbarch,
1461 "usize");
1462 slice = rust_slice_type ("&[*gdb*]", value_type (result),
1463 usize);
1464
1465 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1466 addr = value_as_long (addrval);
1467 tem = value_at_lazy (slice, addr);
1468
1469 value_assign (value_field (tem, 0), value_addr (result));
1470 value_assign (value_field (tem, 1),
1471 value_from_longest (usize, high - low));
1472
1473 result = value_at_lazy (slice, addr);
1474 }
1475 else
1476 result = value_addr (result);
1477 }
1478
1479 return result;
1480}
1481
1482/* evaluate_exp implementation for Rust. */
1483
1484static struct value *
1485rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1486 int *pos, enum noside noside)
1487{
1488 struct value *result;
1489
1490 switch (exp->elts[*pos].opcode)
1491 {
1492 case UNOP_COMPLEMENT:
1493 {
1494 struct value *value;
1495
1496 ++*pos;
1497 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1498 if (noside == EVAL_SKIP)
1499 {
1500 /* Preserving the type is enough. */
1501 return value;
1502 }
1503 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1504 result = value_from_longest (value_type (value),
1505 value_logical_not (value));
1506 else
1507 result = value_complement (value);
1508 }
1509 break;
1510
1511 case BINOP_SUBSCRIPT:
1512 result = rust_subscript (exp, pos, noside, 0);
1513 break;
1514
1515 case OP_FUNCALL:
1516 result = rust_evaluate_funcall (exp, pos, noside);
1517 break;
1518
1519 case OP_AGGREGATE:
1520 {
1521 int pc = (*pos)++;
1522 struct type *type = exp->elts[pc + 1].type;
1523 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1524 int i;
1525 CORE_ADDR addr = 0;
1526 struct value *addrval = NULL;
1527
1528 *pos += 3;
1529
1530 if (noside == EVAL_NORMAL)
1531 {
1532 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1533 addr = value_as_long (addrval);
1534 result = value_at_lazy (type, addr);
1535 }
1536
1537 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1538 {
1539 struct value *init;
1540
1541 ++*pos;
1542 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1543 if (noside == EVAL_NORMAL)
1544 {
1545 /* This isn't quite right but will do for the time
1546 being, seeing that we can't implement the Copy
1547 trait anyway. */
1548 value_assign (result, init);
1549 }
1550
1551 --arglen;
1552 }
1553
1554 gdb_assert (arglen % 2 == 0);
1555 for (i = 0; i < arglen; i += 2)
1556 {
1557 int len;
1558 const char *fieldname;
1559 struct value *value, *field;
1560
1561 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1562 ++*pos;
1563 len = longest_to_int (exp->elts[*pos].longconst);
1564 ++*pos;
1565 fieldname = &exp->elts[*pos].string;
1566 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1567
1568 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1569 if (noside == EVAL_NORMAL)
1570 {
1571 field = value_struct_elt (&result, NULL, fieldname, NULL,
1572 "structure");
1573 value_assign (field, value);
1574 }
1575 }
1576
1577 if (noside == EVAL_SKIP)
1578 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1579 1);
1580 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1581 result = allocate_value (type);
1582 else
1583 result = value_at_lazy (type, addr);
1584 }
1585 break;
1586
1587 case OP_RUST_ARRAY:
1588 {
1589 int pc = (*pos)++;
1590 int copies;
1591 struct value *elt;
1592 struct value *ncopies;
1593
1594 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1595 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1596 copies = value_as_long (ncopies);
1597 if (copies < 0)
1598 error (_("Array with negative number of elements"));
1599
1600 if (noside == EVAL_NORMAL)
1601 {
1602 CORE_ADDR addr;
1603 int i;
1604 struct value **eltvec = XNEWVEC (struct value *, copies);
1605 struct cleanup *cleanup = make_cleanup (xfree, eltvec);
1606
1607 for (i = 0; i < copies; ++i)
1608 eltvec[i] = elt;
1609 result = value_array (0, copies - 1, eltvec);
1610
1611 do_cleanups (cleanup);
1612 }
1613 else
1614 {
1615 struct type *arraytype
1616 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1617 result = allocate_value (arraytype);
1618 }
1619 }
1620 break;
1621
1622 case STRUCTOP_ANONYMOUS:
1623 {
1624 /* Anonymous field access, i.e. foo.1. */
1625 struct value *lhs;
1626 int pc, field_number, nfields;
1627 struct type *type, *variant_type;
1628 struct disr_info disr;
1629
1630 pc = (*pos)++;
1631 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1632 (*pos) += 2;
1633 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1634
1635 type = value_type (lhs);
1636 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1637 {
1638 struct cleanup *cleanup;
1639
1640 disr = rust_get_disr_info (type, value_contents (lhs),
1641 value_embedded_offset (lhs),
1642 value_address (lhs), lhs);
1643
1644 cleanup = make_cleanup (xfree, disr.name);
1645
1646 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1647 {
1648 variant_type = NULL;
1649 nfields = 0;
1650 }
1651 else
1652 {
1653 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1654 nfields = TYPE_NFIELDS (variant_type);
1655 }
1656
1657 if (!disr.is_encoded)
1658 ++field_number;
1659
1660 if (field_number >= nfields || field_number < 0)
1661 error(_("Cannot access field %d of variant %s, \
1662there are only %d fields"),
1663 disr.is_encoded ? field_number : field_number - 1,
1664 disr.name,
1665 disr.is_encoded ? nfields : nfields - 1);
1666
1667 if (!(disr.is_encoded
1668 ? rust_tuple_struct_type_p (variant_type)
1669 : rust_tuple_variant_type_p (variant_type)))
1670 error(_("Variant %s is not a tuple variant"), disr.name);
1671
1672 result = value_primitive_field (lhs, 0, field_number,
1673 variant_type);
1674 do_cleanups (cleanup);
1675 }
1676 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1677 {
1678 /* Tuples and tuple structs */
1679 nfields = TYPE_NFIELDS(type);
1680
1681 if (field_number >= nfields || field_number < 0)
1682 error(_("Cannot access field %d of %s, there are only %d fields"),
1683 field_number, TYPE_TAG_NAME (type), nfields);
1684
1685 /* Tuples are tuple structs too. */
1686 if (!rust_tuple_struct_type_p (type))
1687 error(_("Attempting to access anonymous field %d of %s, which is \
1688not a tuple, tuple struct, or tuple-like variant"),
1689 field_number, TYPE_TAG_NAME (type));
1690
1691 result = value_primitive_field (lhs, 0, field_number, type);
1692 }
1693 else
1694 error(_("Anonymous field access is only allowed on tuples, \
1695tuple structs, and tuple-like enum variants"));
1696 }
1697 break;
1698
1699 case STRUCTOP_STRUCT:
1700 {
1701 struct value* lhs;
1702 struct type *type;
1703 int tem, pc;
1704
1705 pc = (*pos)++;
1706 tem = longest_to_int (exp->elts[pc + 1].longconst);
1707 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1708 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1709
1710 type = value_type (lhs);
1711
1712 if (TYPE_CODE (type) == TYPE_CODE_UNION)
1713 {
1714 int i, start;
1715 struct disr_info disr;
1716 struct cleanup* cleanup;
1717 struct type* variant_type;
1718 char* field_name;
1719
1720 field_name = &exp->elts[pc + 2].string;
1721
1722 disr = rust_get_disr_info (type, value_contents (lhs),
1723 value_embedded_offset (lhs),
1724 value_address (lhs), lhs);
1725
1726 cleanup = make_cleanup (xfree, disr.name);
1727
1728 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1729 error(_("Could not find field %s of struct variant %s"),
1730 field_name, disr.name);
1731
1732 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1733
1734 if (variant_type == NULL
1735 || rust_tuple_variant_type_p (variant_type))
1736 error(_("Attempting to access named field %s of tuple variant %s, \
1737which has only anonymous fields"),
1738 field_name, disr.name);
1739
1740 start = disr.is_encoded ? 0 : 1;
1741 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1742 {
1743 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1744 field_name) == 0) {
1745 result = value_primitive_field (lhs, 0, i, variant_type);
1746 break;
1747 }
1748 }
1749
1750 if (i == TYPE_NFIELDS (variant_type))
1751 /* We didn't find it. */
1752 error(_("Could not find field %s of struct variant %s"),
1753 field_name, disr.name);
1754
1755 do_cleanups (cleanup);
1756 }
1757 else
1758 {
1759 *pos = pc;
1760 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1761 }
1762 }
1763 break;
1764
01739a3b 1765 case OP_RANGE:
c44af4eb
TT
1766 result = rust_range (exp, pos, noside);
1767 break;
1768
1769 case UNOP_ADDR:
1770 /* We might have &array[range], in which case we need to make a
1771 slice. */
1772 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1773 {
1774 ++*pos;
1775 result = rust_subscript (exp, pos, noside, 1);
1776 break;
1777 }
1778 /* Fall through. */
1779 default:
1780 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1781 break;
1782 }
1783
1784 return result;
1785}
1786
1787/* operator_length implementation for Rust. */
1788
1789static void
1790rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1791 int *argsp)
1792{
1793 int oplen = 1;
1794 int args = 0;
1795
1796 switch (exp->elts[pc - 1].opcode)
1797 {
1798 case OP_AGGREGATE:
1799 /* We handle aggregate as a type and argument count. The first
1800 argument might be OP_OTHERS. After that the arguments
1801 alternate: first an OP_NAME, then an expression. */
1802 oplen = 4;
1803 args = longest_to_int (exp->elts[pc - 2].longconst);
1804 break;
1805
1806 case OP_OTHERS:
1807 oplen = 1;
1808 args = 1;
1809 break;
1810
1811 case STRUCTOP_ANONYMOUS:
1812 oplen = 3;
1813 args = 1;
1814 break;
1815
1816 case OP_RUST_ARRAY:
1817 oplen = 1;
1818 args = 2;
1819 break;
1820
1821 default:
1822 operator_length_standard (exp, pc, oplenp, argsp);
1823 return;
1824 }
1825
1826 *oplenp = oplen;
1827 *argsp = args;
1828}
1829
1830/* op_name implementation for Rust. */
1831
1832static char *
1833rust_op_name (enum exp_opcode opcode)
1834{
1835 switch (opcode)
1836 {
1837 case OP_AGGREGATE:
1838 return "OP_AGGREGATE";
1839 case OP_OTHERS:
1840 return "OP_OTHERS";
1841 default:
1842 return op_name_standard (opcode);
1843 }
1844}
1845
1846/* dump_subexp_body implementation for Rust. */
1847
1848static int
1849rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1850 int elt)
1851{
1852 switch (exp->elts[elt].opcode)
1853 {
1854 case OP_AGGREGATE:
1855 {
1856 int length = longest_to_int (exp->elts[elt + 2].longconst);
1857 int i;
1858
1859 fprintf_filtered (stream, "Type @");
1860 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1861 fprintf_filtered (stream, " (");
1862 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1863 fprintf_filtered (stream, "), length %d", length);
1864
1865 elt += 4;
1866 for (i = 0; i < length; ++i)
1867 elt = dump_subexp (exp, stream, elt);
1868 }
1869 break;
1870
1871 case OP_STRING:
1872 case OP_NAME:
1873 {
1874 LONGEST len = exp->elts[elt + 1].longconst;
1875
1876 fprintf_filtered (stream, "%s: %s",
1877 (exp->elts[elt].opcode == OP_STRING
1878 ? "string" : "name"),
1879 &exp->elts[elt + 2].string);
1880 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1881 }
1882 break;
1883
1884 case OP_OTHERS:
1885 elt = dump_subexp (exp, stream, elt + 1);
1886 break;
1887
1888 case STRUCTOP_ANONYMOUS:
1889 {
1890 int field_number;
1891
1892 field_number = longest_to_int (exp->elts[elt].longconst);
1893
1894 fprintf_filtered (stream, "Field number: %d", field_number);
1895 elt = dump_subexp (exp, stream, elt + 2);
1896 }
1897 break;
1898
1899 case OP_RUST_ARRAY:
1900 break;
1901
1902 default:
1903 elt = dump_subexp_body_standard (exp, stream, elt);
1904 break;
1905 }
1906
1907 return elt;
1908}
1909
1910/* print_subexp implementation for Rust. */
1911
1912static void
1913rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1914 enum precedence prec)
1915{
1916 switch (exp->elts[*pos].opcode)
1917 {
1918 case OP_AGGREGATE:
1919 {
1920 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1921 int i;
1922
1923 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1924 fputs_filtered (" { ", stream);
1925
1926 *pos += 4;
1927 for (i = 0; i < length; ++i)
1928 {
1929 rust_print_subexp (exp, pos, stream, prec);
1930 fputs_filtered (", ", stream);
1931 }
1932 fputs_filtered (" }", stream);
1933 }
1934 break;
1935
1936 case OP_NAME:
1937 {
1938 LONGEST len = exp->elts[*pos + 1].longconst;
1939
1940 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1941 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1942 }
1943 break;
1944
1945 case OP_OTHERS:
1946 {
1947 fputs_filtered ("<<others>> (", stream);
1948 ++*pos;
1949 rust_print_subexp (exp, pos, stream, prec);
1950 fputs_filtered (")", stream);
1951 }
1952 break;
1953
1954 case STRUCTOP_ANONYMOUS:
1955 {
1956 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1957
1958 (*pos) += 3;
1959 print_subexp (exp, pos, stream, PREC_SUFFIX);
1960 fprintf_filtered (stream, ".%d", tem);
1961 }
1962 return;
1963
1964 case OP_RUST_ARRAY:
1965 ++*pos;
1966 fprintf_filtered (stream, "[");
1967 rust_print_subexp (exp, pos, stream, prec);
1968 fprintf_filtered (stream, "; ");
1969 rust_print_subexp (exp, pos, stream, prec);
1970 fprintf_filtered (stream, "]");
1971 break;
1972
1973 default:
1974 print_subexp_standard (exp, pos, stream, prec);
1975 break;
1976 }
1977}
1978
1979/* operator_check implementation for Rust. */
1980
1981static int
1982rust_operator_check (struct expression *exp, int pos,
1983 int (*objfile_func) (struct objfile *objfile,
1984 void *data),
1985 void *data)
1986{
1987 switch (exp->elts[pos].opcode)
1988 {
1989 case OP_AGGREGATE:
1990 {
1991 struct type *type = exp->elts[pos + 1].type;
1992 struct objfile *objfile = TYPE_OBJFILE (type);
1993
1994 if (objfile != NULL && (*objfile_func) (objfile, data))
1995 return 1;
1996 }
1997 break;
1998
1999 case OP_OTHERS:
2000 case OP_NAME:
2001 case OP_RUST_ARRAY:
2002 break;
2003
2004 default:
2005 return operator_check_standard (exp, pos, objfile_func, data);
2006 }
2007
2008 return 0;
2009}
2010
2011\f
2012
2013/* Implementation of la_lookup_symbol_nonlocal for Rust. */
2014
2015static struct block_symbol
2016rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2017 const char *name,
2018 const struct block *block,
2019 const domain_enum domain)
2020{
2021 struct block_symbol result = {NULL, NULL};
2022
2023 if (symbol_lookup_debug)
2024 {
2025 fprintf_unfiltered (gdb_stdlog,
2026 "rust_lookup_symbol_non_local"
2027 " (%s, %s (scope %s), %s)\n",
2028 name, host_address_to_string (block),
2029 block_scope (block), domain_name (domain));
2030 }
2031
2032 /* Look up bare names in the block's scope. */
2033 if (name[cp_find_first_component (name)] == '\0')
2034 {
2035 const char *scope = block_scope (block);
2036
2037 if (scope[0] != '\0')
2038 {
2039 char *scopedname = concat (scope, "::", name, (char *) NULL);
2040 struct cleanup *cleanup = make_cleanup (xfree, scopedname);
2041
2042 result = lookup_symbol_in_static_block (scopedname, block,
2043 domain);
2044 if (result.symbol == NULL)
2045 result = lookup_global_symbol (scopedname, block, domain);
2046 do_cleanups (cleanup);
2047 }
2048 }
2049 return result;
2050}
2051
2052\f
2053
8b302db8
TT
2054/* la_sniff_from_mangled_name for Rust. */
2055
2056static int
2057rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2058{
2059 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2060 return *demangled != NULL;
2061}
2062
2063\f
2064
c44af4eb
TT
2065static const struct exp_descriptor exp_descriptor_rust =
2066{
2067 rust_print_subexp,
2068 rust_operator_length,
2069 rust_operator_check,
2070 rust_op_name,
2071 rust_dump_subexp_body,
2072 rust_evaluate_subexp
2073};
2074
56618e20
TT
2075static const char *rust_extensions[] =
2076{
2077 ".rs", NULL
2078};
2079
c44af4eb
TT
2080static const struct language_defn rust_language_defn =
2081{
2082 "rust",
2083 "Rust",
2084 language_rust,
2085 range_check_on,
2086 case_sensitive_on,
2087 array_row_major,
2088 macro_expansion_no,
56618e20 2089 rust_extensions,
c44af4eb
TT
2090 &exp_descriptor_rust,
2091 rust_parse,
2092 rustyyerror,
2093 null_post_parser,
2094 rust_printchar, /* Print a character constant */
2095 rust_printstr, /* Function to print string constant */
2096 rust_emitchar, /* Print a single char */
2097 rust_print_type, /* Print a type using appropriate syntax */
2098 rust_print_typedef, /* Print a typedef using appropriate syntax */
2099 rust_val_print, /* Print a value using appropriate syntax */
2100 c_value_print, /* Print a top-level value */
2101 default_read_var_value, /* la_read_var_value */
2102 NULL, /* Language specific skip_trampoline */
2103 NULL, /* name_of_this */
2104 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2105 basic_lookup_transparent_type,/* lookup_transparent_type */
2106 gdb_demangle, /* Language specific symbol demangler */
8b302db8 2107 rust_sniff_from_mangled_name,
c44af4eb
TT
2108 NULL, /* Language specific
2109 class_name_from_physname */
2110 c_op_print_tab, /* expression operators for printing */
2111 1, /* c-style arrays */
2112 0, /* String lower bound */
2113 default_word_break_characters,
2114 default_make_symbol_completion_list,
2115 rust_language_arch_info,
2116 default_print_array_index,
2117 default_pass_by_reference,
2118 c_get_string,
2119 NULL, /* la_get_symbol_name_cmp */
2120 iterate_over_symbols,
2121 &default_varobj_ops,
2122 NULL,
2123 NULL,
2124 LANG_MAGIC
2125};
2126
2127void
2128_initialize_rust_language (void)
2129{
2130 add_language (&rust_language_defn);
2131}
This page took 0.144978 seconds and 4 git commands to generate.