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