problem looking up some symbols when they have a linkage name
[deliverable/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 bool is_univariant = false;
635
636 int enum_discriminant_index = -1;
637
638 if (for_rust_enum)
639 {
640 /* Already printing an outer enum, so nothing to print here. */
641 }
642 else
643 {
644 /* This code path is also used by unions and enums. */
645 if (is_enum)
646 {
647 fputs_filtered ("enum ", stream);
648 type = TYPE_FIELD_TYPE (type, 0);
649
650 struct dynamic_prop *discriminant_prop
651 = get_dyn_prop (DYN_PROP_DISCRIMINATED, type);
652 struct discriminant_info *info
653 = (struct discriminant_info *) discriminant_prop->data.baton;
654 enum_discriminant_index = info->discriminant_index;
655 }
656 else if (TYPE_CODE (type) == TYPE_CODE_UNION && TYPE_NFIELDS (type) == 1)
657 {
658 /* Probably a univariant enum. */
659 fputs_filtered ("enum ", stream);
660 is_univariant = true;
661 }
662 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
663 fputs_filtered ("struct ", stream);
664 else
665 fputs_filtered ("union ", stream);
666
667 if (tagname != NULL)
668 fputs_filtered (tagname, stream);
669 }
670
671 if (TYPE_NFIELDS (type) == 0 && !is_tuple)
672 return;
673 if (for_rust_enum)
674 fputs_filtered (is_tuple_struct ? "(" : "{", stream);
675 else
676 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
677
678 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
679 {
680 QUIT;
681 if (field_is_static (&TYPE_FIELD (type, i)))
682 continue;
683
684 /* We'd like to print "pub" here as needed, but rustc
685 doesn't emit the debuginfo, and our types don't have
686 cplus_struct_type attached. */
687
688 /* For a tuple struct we print the type but nothing
689 else. */
690 if (!for_rust_enum)
691 print_spaces_filtered (level + 2, stream);
692 if (is_enum)
693 {
694 if (i == enum_discriminant_index)
695 continue;
696 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
697 }
698 else if (is_univariant)
699 {
700 const char *name
701 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
702 fputs_filtered (name, stream);
703 }
704 else if (!is_tuple_struct)
705 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
706
707 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL,
708 stream,
709 ((is_enum || is_univariant) ? show : show - 1),
710 level + 2, flags, is_enum || is_univariant);
711 if (!for_rust_enum)
712 fputs_filtered (",\n", stream);
713 else if (i + 1 < TYPE_NFIELDS (type))
714 fputs_filtered (", ", stream);
715 }
716
717 if (!for_rust_enum)
718 print_spaces_filtered (level, stream);
719 fputs_filtered (is_tuple_struct ? ")" : "}", stream);
720 }
721
722 /* la_print_typedef implementation for Rust. */
723
724 static void
725 rust_print_typedef (struct type *type,
726 struct symbol *new_symbol,
727 struct ui_file *stream)
728 {
729 type = check_typedef (type);
730 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
731 type_print (type, "", stream, 0);
732 fprintf_filtered (stream, ";\n");
733 }
734
735 /* la_print_type implementation for Rust. */
736
737 static void
738 rust_internal_print_type (struct type *type, const char *varstring,
739 struct ui_file *stream, int show, int level,
740 const struct type_print_options *flags,
741 bool for_rust_enum)
742 {
743 int i;
744
745 QUIT;
746 if (show <= 0
747 && TYPE_NAME (type) != NULL)
748 {
749 /* Rust calls the unit type "void" in its debuginfo,
750 but we don't want to print it as that. */
751 if (TYPE_CODE (type) == TYPE_CODE_VOID)
752 fputs_filtered ("()", stream);
753 else
754 fputs_filtered (TYPE_NAME (type), stream);
755 return;
756 }
757
758 type = check_typedef (type);
759 switch (TYPE_CODE (type))
760 {
761 case TYPE_CODE_VOID:
762 /* If we have an enum, we've already printed the type's
763 unqualified name, and there is nothing else to print
764 here. */
765 if (!for_rust_enum)
766 fputs_filtered ("()", stream);
767 break;
768
769 case TYPE_CODE_FUNC:
770 /* Delegate varargs to the C printer. */
771 if (TYPE_VARARGS (type))
772 goto c_printer;
773
774 fputs_filtered ("fn ", stream);
775 if (varstring != NULL)
776 fputs_filtered (varstring, stream);
777 fputs_filtered ("(", stream);
778 for (i = 0; i < TYPE_NFIELDS (type); ++i)
779 {
780 QUIT;
781 if (i > 0)
782 fputs_filtered (", ", stream);
783 rust_internal_print_type (TYPE_FIELD_TYPE (type, i), "", stream,
784 -1, 0, flags, false);
785 }
786 fputs_filtered (")", stream);
787 /* If it returns unit, we can omit the return type. */
788 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
789 {
790 fputs_filtered (" -> ", stream);
791 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
792 -1, 0, flags, false);
793 }
794 break;
795
796 case TYPE_CODE_ARRAY:
797 {
798 LONGEST low_bound, high_bound;
799
800 fputs_filtered ("[", stream);
801 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
802 stream, show - 1, level, flags, false);
803
804 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
805 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
806 fprintf_filtered (stream, "; variable length");
807 else if (get_array_bounds (type, &low_bound, &high_bound))
808 fprintf_filtered (stream, "; %s",
809 plongest (high_bound - low_bound + 1));
810 fputs_filtered ("]", stream);
811 }
812 break;
813
814 case TYPE_CODE_UNION:
815 case TYPE_CODE_STRUCT:
816 rust_print_struct_def (type, varstring, stream, show, level, flags,
817 for_rust_enum);
818 break;
819
820 case TYPE_CODE_ENUM:
821 {
822 int i, len = 0;
823
824 fputs_filtered ("enum ", stream);
825 if (TYPE_TAG_NAME (type) != NULL)
826 {
827 fputs_filtered (TYPE_TAG_NAME (type), stream);
828 fputs_filtered (" ", stream);
829 len = strlen (TYPE_TAG_NAME (type));
830 }
831 fputs_filtered ("{\n", stream);
832
833 for (i = 0; i < TYPE_NFIELDS (type); ++i)
834 {
835 const char *name = TYPE_FIELD_NAME (type, i);
836
837 QUIT;
838
839 if (len > 0
840 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
841 && name[len] == ':'
842 && name[len + 1] == ':')
843 name += len + 2;
844 fprintfi_filtered (level + 2, stream, "%s,\n", name);
845 }
846
847 fputs_filtered ("}", stream);
848 }
849 break;
850
851 default:
852 c_printer:
853 c_print_type (type, varstring, stream, show, level, flags);
854 }
855 }
856
857 static void
858 rust_print_type (struct type *type, const char *varstring,
859 struct ui_file *stream, int show, int level,
860 const struct type_print_options *flags)
861 {
862 rust_internal_print_type (type, varstring, stream, show, level,
863 flags, false);
864 }
865
866 \f
867
868 /* Compute the alignment of the type T. */
869
870 static int
871 rust_type_alignment (struct type *t)
872 {
873 t = check_typedef (t);
874 switch (TYPE_CODE (t))
875 {
876 default:
877 error (_("Could not compute alignment of type"));
878
879 case TYPE_CODE_PTR:
880 case TYPE_CODE_ENUM:
881 case TYPE_CODE_INT:
882 case TYPE_CODE_FLT:
883 case TYPE_CODE_REF:
884 case TYPE_CODE_CHAR:
885 case TYPE_CODE_BOOL:
886 return TYPE_LENGTH (t);
887
888 case TYPE_CODE_ARRAY:
889 case TYPE_CODE_COMPLEX:
890 return rust_type_alignment (TYPE_TARGET_TYPE (t));
891
892 case TYPE_CODE_STRUCT:
893 case TYPE_CODE_UNION:
894 {
895 int i;
896 int align = 1;
897
898 for (i = 0; i < TYPE_NFIELDS (t); ++i)
899 {
900 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
901 if (a > align)
902 align = a;
903 }
904 return align;
905 }
906 }
907 }
908
909 /* Like arch_composite_type, but uses TYPE to decide how to allocate
910 -- either on an obstack or on a gdbarch. */
911
912 static struct type *
913 rust_composite_type (struct type *original,
914 const char *name,
915 const char *field1, struct type *type1,
916 const char *field2, struct type *type2)
917 {
918 struct type *result = alloc_type_copy (original);
919 int i, nfields, bitpos;
920
921 nfields = 0;
922 if (field1 != NULL)
923 ++nfields;
924 if (field2 != NULL)
925 ++nfields;
926
927 TYPE_CODE (result) = TYPE_CODE_STRUCT;
928 TYPE_NAME (result) = name;
929 TYPE_TAG_NAME (result) = name;
930
931 TYPE_NFIELDS (result) = nfields;
932 TYPE_FIELDS (result)
933 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
934
935 i = 0;
936 bitpos = 0;
937 if (field1 != NULL)
938 {
939 struct field *field = &TYPE_FIELD (result, i);
940
941 SET_FIELD_BITPOS (*field, bitpos);
942 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
943
944 FIELD_NAME (*field) = field1;
945 FIELD_TYPE (*field) = type1;
946 ++i;
947 }
948 if (field2 != NULL)
949 {
950 struct field *field = &TYPE_FIELD (result, i);
951 int align = rust_type_alignment (type2);
952
953 if (align != 0)
954 {
955 int delta;
956
957 align *= TARGET_CHAR_BIT;
958 delta = bitpos % align;
959 if (delta != 0)
960 bitpos += align - delta;
961 }
962 SET_FIELD_BITPOS (*field, bitpos);
963
964 FIELD_NAME (*field) = field2;
965 FIELD_TYPE (*field) = type2;
966 ++i;
967 }
968
969 if (i > 0)
970 TYPE_LENGTH (result)
971 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
972 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
973 return result;
974 }
975
976 /* See rust-lang.h. */
977
978 struct type *
979 rust_slice_type (const char *name, struct type *elt_type,
980 struct type *usize_type)
981 {
982 struct type *type;
983
984 elt_type = lookup_pointer_type (elt_type);
985 type = rust_composite_type (elt_type, name,
986 "data_ptr", elt_type,
987 "length", usize_type);
988
989 return type;
990 }
991
992 enum rust_primitive_types
993 {
994 rust_primitive_bool,
995 rust_primitive_char,
996 rust_primitive_i8,
997 rust_primitive_u8,
998 rust_primitive_i16,
999 rust_primitive_u16,
1000 rust_primitive_i32,
1001 rust_primitive_u32,
1002 rust_primitive_i64,
1003 rust_primitive_u64,
1004 rust_primitive_isize,
1005 rust_primitive_usize,
1006 rust_primitive_f32,
1007 rust_primitive_f64,
1008 rust_primitive_unit,
1009 rust_primitive_str,
1010 nr_rust_primitive_types
1011 };
1012
1013 /* la_language_arch_info implementation for Rust. */
1014
1015 static void
1016 rust_language_arch_info (struct gdbarch *gdbarch,
1017 struct language_arch_info *lai)
1018 {
1019 const struct builtin_type *builtin = builtin_type (gdbarch);
1020 struct type *tem;
1021 struct type **types;
1022 unsigned int length;
1023
1024 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1025 struct type *);
1026
1027 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1028 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1029 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1030 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1031 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1032 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1033 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1034 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1035 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1036 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1037
1038 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1039 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1040 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1041
1042 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1043 floatformats_ieee_single);
1044 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1045 floatformats_ieee_double);
1046
1047 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1048
1049 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1050 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1051 types[rust_primitive_usize]);
1052
1053 lai->primitive_type_vector = types;
1054 lai->bool_type_default = types[rust_primitive_bool];
1055 lai->string_char_type = types[rust_primitive_u8];
1056 }
1057
1058 \f
1059
1060 /* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1061
1062 static struct value *
1063 rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1064 {
1065 int i;
1066 int num_args = exp->elts[*pos + 1].longconst;
1067 const char *method;
1068 struct value *function, *result, *arg0;
1069 struct type *type, *fn_type;
1070 const struct block *block;
1071 struct block_symbol sym;
1072
1073 /* For an ordinary function call we can simply defer to the
1074 generic implementation. */
1075 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1076 return evaluate_subexp_standard (NULL, exp, pos, noside);
1077
1078 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1079 *pos += 4;
1080 method = &exp->elts[*pos + 1].string;
1081 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1082
1083 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1084 type in order to look up the method. */
1085 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1086
1087 if (noside == EVAL_SKIP)
1088 {
1089 for (i = 0; i < num_args; ++i)
1090 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1091 return arg0;
1092 }
1093
1094 std::vector<struct value *> args (num_args + 1);
1095 args[0] = arg0;
1096
1097 /* We don't yet implement real Deref semantics. */
1098 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1099 args[0] = value_ind (args[0]);
1100
1101 type = value_type (args[0]);
1102 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1103 && TYPE_CODE (type) != TYPE_CODE_UNION
1104 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1105 || rust_tuple_type_p (type))
1106 error (_("Method calls only supported on struct or enum types"));
1107 if (TYPE_TAG_NAME (type) == NULL)
1108 error (_("Method call on nameless type"));
1109
1110 std::string name = std::string (TYPE_TAG_NAME (type)) + "::" + method;
1111
1112 block = get_selected_block (0);
1113 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
1114 if (sym.symbol == NULL)
1115 error (_("Could not find function named '%s'"), name.c_str ());
1116
1117 fn_type = SYMBOL_TYPE (sym.symbol);
1118 if (TYPE_NFIELDS (fn_type) == 0)
1119 error (_("Function '%s' takes no arguments"), name.c_str ());
1120
1121 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1122 args[0] = value_addr (args[0]);
1123
1124 function = address_of_variable (sym.symbol, block);
1125
1126 for (i = 0; i < num_args; ++i)
1127 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1128
1129 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1130 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1131 else
1132 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
1133 return result;
1134 }
1135
1136 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1137
1138 static struct value *
1139 rust_range (struct expression *exp, int *pos, enum noside noside)
1140 {
1141 enum range_type kind;
1142 struct value *low = NULL, *high = NULL;
1143 struct value *addrval, *result;
1144 CORE_ADDR addr;
1145 struct type *range_type;
1146 struct type *index_type;
1147 struct type *temp_type;
1148 const char *name;
1149
1150 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
1151 *pos += 3;
1152
1153 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1154 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1155 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1156 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1157
1158 if (noside == EVAL_SKIP)
1159 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1160
1161 if (low == NULL)
1162 {
1163 if (high == NULL)
1164 {
1165 index_type = NULL;
1166 name = "std::ops::RangeFull";
1167 }
1168 else
1169 {
1170 index_type = value_type (high);
1171 name = "std::ops::RangeTo";
1172 }
1173 }
1174 else
1175 {
1176 if (high == NULL)
1177 {
1178 index_type = value_type (low);
1179 name = "std::ops::RangeFrom";
1180 }
1181 else
1182 {
1183 if (!types_equal (value_type (low), value_type (high)))
1184 error (_("Range expression with different types"));
1185 index_type = value_type (low);
1186 name = "std::ops::Range";
1187 }
1188 }
1189
1190 /* If we don't have an index type, just allocate this on the
1191 arch. Here any type will do. */
1192 temp_type = (index_type == NULL
1193 ? language_bool_type (exp->language_defn, exp->gdbarch)
1194 : index_type);
1195 /* It would be nicer to cache the range type. */
1196 range_type = rust_composite_type (temp_type, name,
1197 low == NULL ? NULL : "start", index_type,
1198 high == NULL ? NULL : "end", index_type);
1199
1200 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1201 return value_zero (range_type, lval_memory);
1202
1203 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1204 addr = value_as_long (addrval);
1205 result = value_at_lazy (range_type, addr);
1206
1207 if (low != NULL)
1208 {
1209 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1210 "range");
1211
1212 value_assign (start, low);
1213 }
1214
1215 if (high != NULL)
1216 {
1217 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1218 "range");
1219
1220 value_assign (end, high);
1221 }
1222
1223 result = value_at_lazy (range_type, addr);
1224 return result;
1225 }
1226
1227 /* A helper function to compute the range and kind given a range
1228 value. TYPE is the type of the range value. RANGE is the range
1229 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1230 parameters might be filled in, or might not be, depending on the
1231 kind of range this is. KIND will always be set to the appropriate
1232 value describing the kind of range, and this can be used to
1233 determine whether LOW or HIGH are valid. */
1234
1235 static void
1236 rust_compute_range (struct type *type, struct value *range,
1237 LONGEST *low, LONGEST *high,
1238 enum range_type *kind)
1239 {
1240 int i;
1241
1242 *low = 0;
1243 *high = 0;
1244 *kind = BOTH_BOUND_DEFAULT;
1245
1246 if (TYPE_NFIELDS (type) == 0)
1247 return;
1248
1249 i = 0;
1250 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1251 {
1252 *kind = HIGH_BOUND_DEFAULT;
1253 *low = value_as_long (value_field (range, 0));
1254 ++i;
1255 }
1256 if (TYPE_NFIELDS (type) > i
1257 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1258 {
1259 *kind = (*kind == BOTH_BOUND_DEFAULT
1260 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1261 *high = value_as_long (value_field (range, i));
1262 }
1263 }
1264
1265 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1266
1267 static struct value *
1268 rust_subscript (struct expression *exp, int *pos, enum noside noside,
1269 int for_addr)
1270 {
1271 struct value *lhs, *rhs, *result;
1272 struct type *rhstype;
1273 LONGEST low, high_bound;
1274 /* Initialized to appease the compiler. */
1275 enum range_type kind = BOTH_BOUND_DEFAULT;
1276 LONGEST high = 0;
1277 int want_slice = 0;
1278
1279 ++*pos;
1280 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1281 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1282
1283 if (noside == EVAL_SKIP)
1284 return lhs;
1285
1286 rhstype = check_typedef (value_type (rhs));
1287 if (rust_range_type_p (rhstype))
1288 {
1289 if (!for_addr)
1290 error (_("Can't take slice of array without '&'"));
1291 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1292 want_slice = 1;
1293 }
1294 else
1295 low = value_as_long (rhs);
1296
1297 struct type *type = check_typedef (value_type (lhs));
1298 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1299 {
1300 struct type *base_type = nullptr;
1301 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1302 base_type = TYPE_TARGET_TYPE (type);
1303 else if (rust_slice_type_p (type))
1304 {
1305 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1306 {
1307 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1308 {
1309 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1310 break;
1311 }
1312 }
1313 if (base_type == nullptr)
1314 error (_("Could not find 'data_ptr' in slice type"));
1315 }
1316 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1317 base_type = TYPE_TARGET_TYPE (type);
1318 else
1319 error (_("Cannot subscript non-array type"));
1320
1321 struct type *new_type;
1322 if (want_slice)
1323 {
1324 if (rust_slice_type_p (type))
1325 new_type = type;
1326 else
1327 {
1328 struct type *usize
1329 = language_lookup_primitive_type (exp->language_defn,
1330 exp->gdbarch,
1331 "usize");
1332 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1333 }
1334 }
1335 else
1336 new_type = base_type;
1337
1338 return value_zero (new_type, VALUE_LVAL (lhs));
1339 }
1340 else
1341 {
1342 LONGEST low_bound;
1343 struct value *base;
1344
1345 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1346 {
1347 base = lhs;
1348 if (!get_array_bounds (type, &low_bound, &high_bound))
1349 error (_("Can't compute array bounds"));
1350 if (low_bound != 0)
1351 error (_("Found array with non-zero lower bound"));
1352 ++high_bound;
1353 }
1354 else if (rust_slice_type_p (type))
1355 {
1356 struct value *len;
1357
1358 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1359 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1360 low_bound = 0;
1361 high_bound = value_as_long (len);
1362 }
1363 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1364 {
1365 base = lhs;
1366 low_bound = 0;
1367 high_bound = LONGEST_MAX;
1368 }
1369 else
1370 error (_("Cannot subscript non-array type"));
1371
1372 if (want_slice
1373 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1374 low = low_bound;
1375 if (low < 0)
1376 error (_("Index less than zero"));
1377 if (low > high_bound)
1378 error (_("Index greater than length"));
1379
1380 result = value_subscript (base, low);
1381 }
1382
1383 if (for_addr)
1384 {
1385 if (want_slice)
1386 {
1387 struct type *usize, *slice;
1388 CORE_ADDR addr;
1389 struct value *addrval, *tem;
1390
1391 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1392 high = high_bound;
1393 if (high < 0)
1394 error (_("High index less than zero"));
1395 if (low > high)
1396 error (_("Low index greater than high index"));
1397 if (high > high_bound)
1398 error (_("High index greater than length"));
1399
1400 usize = language_lookup_primitive_type (exp->language_defn,
1401 exp->gdbarch,
1402 "usize");
1403 const char *new_name = ((type != nullptr
1404 && rust_slice_type_p (type))
1405 ? TYPE_NAME (type) : "&[*gdb*]");
1406
1407 slice = rust_slice_type (new_name, value_type (result), usize);
1408
1409 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1410 addr = value_as_long (addrval);
1411 tem = value_at_lazy (slice, addr);
1412
1413 value_assign (value_field (tem, 0), value_addr (result));
1414 value_assign (value_field (tem, 1),
1415 value_from_longest (usize, high - low));
1416
1417 result = value_at_lazy (slice, addr);
1418 }
1419 else
1420 result = value_addr (result);
1421 }
1422
1423 return result;
1424 }
1425
1426 /* evaluate_exp implementation for Rust. */
1427
1428 static struct value *
1429 rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1430 int *pos, enum noside noside)
1431 {
1432 struct value *result;
1433
1434 switch (exp->elts[*pos].opcode)
1435 {
1436 case UNOP_IND:
1437 {
1438 if (noside != EVAL_NORMAL)
1439 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1440 else
1441 {
1442 ++*pos;
1443 struct value *value = evaluate_subexp (expect_type, exp, pos,
1444 noside);
1445
1446 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1447 if (trait_ptr != NULL)
1448 value = trait_ptr;
1449
1450 result = value_ind (value);
1451 }
1452 }
1453 break;
1454
1455 case UNOP_COMPLEMENT:
1456 {
1457 struct value *value;
1458
1459 ++*pos;
1460 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1461 if (noside == EVAL_SKIP)
1462 {
1463 /* Preserving the type is enough. */
1464 return value;
1465 }
1466 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1467 result = value_from_longest (value_type (value),
1468 value_logical_not (value));
1469 else
1470 result = value_complement (value);
1471 }
1472 break;
1473
1474 case BINOP_SUBSCRIPT:
1475 result = rust_subscript (exp, pos, noside, 0);
1476 break;
1477
1478 case OP_FUNCALL:
1479 result = rust_evaluate_funcall (exp, pos, noside);
1480 break;
1481
1482 case OP_AGGREGATE:
1483 {
1484 int pc = (*pos)++;
1485 struct type *type = exp->elts[pc + 1].type;
1486 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1487 int i;
1488 CORE_ADDR addr = 0;
1489 struct value *addrval = NULL;
1490
1491 *pos += 3;
1492
1493 if (noside == EVAL_NORMAL)
1494 {
1495 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1496 addr = value_as_long (addrval);
1497 result = value_at_lazy (type, addr);
1498 }
1499
1500 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1501 {
1502 struct value *init;
1503
1504 ++*pos;
1505 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1506 if (noside == EVAL_NORMAL)
1507 {
1508 /* This isn't quite right but will do for the time
1509 being, seeing that we can't implement the Copy
1510 trait anyway. */
1511 value_assign (result, init);
1512 }
1513
1514 --arglen;
1515 }
1516
1517 gdb_assert (arglen % 2 == 0);
1518 for (i = 0; i < arglen; i += 2)
1519 {
1520 int len;
1521 const char *fieldname;
1522 struct value *value, *field;
1523
1524 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1525 ++*pos;
1526 len = longest_to_int (exp->elts[*pos].longconst);
1527 ++*pos;
1528 fieldname = &exp->elts[*pos].string;
1529 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1530
1531 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1532 if (noside == EVAL_NORMAL)
1533 {
1534 field = value_struct_elt (&result, NULL, fieldname, NULL,
1535 "structure");
1536 value_assign (field, value);
1537 }
1538 }
1539
1540 if (noside == EVAL_SKIP)
1541 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1542 1);
1543 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1544 result = allocate_value (type);
1545 else
1546 result = value_at_lazy (type, addr);
1547 }
1548 break;
1549
1550 case OP_RUST_ARRAY:
1551 {
1552 int pc = (*pos)++;
1553 int copies;
1554 struct value *elt;
1555 struct value *ncopies;
1556
1557 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1558 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1559 copies = value_as_long (ncopies);
1560 if (copies < 0)
1561 error (_("Array with negative number of elements"));
1562
1563 if (noside == EVAL_NORMAL)
1564 {
1565 int i;
1566 std::vector<struct value *> eltvec (copies);
1567
1568 for (i = 0; i < copies; ++i)
1569 eltvec[i] = elt;
1570 result = value_array (0, copies - 1, eltvec.data ());
1571 }
1572 else
1573 {
1574 struct type *arraytype
1575 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1576 result = allocate_value (arraytype);
1577 }
1578 }
1579 break;
1580
1581 case STRUCTOP_ANONYMOUS:
1582 {
1583 /* Anonymous field access, i.e. foo.1. */
1584 struct value *lhs;
1585 int pc, field_number, nfields;
1586 struct type *type, *variant_type;
1587
1588 pc = (*pos)++;
1589 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1590 (*pos) += 2;
1591 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1592
1593 type = value_type (lhs);
1594
1595 /* Treat a univariant union as if it were an enum. */
1596 if (TYPE_CODE (type) == TYPE_CODE_UNION && TYPE_NFIELDS (type) == 1)
1597 {
1598 lhs = value_primitive_field (lhs, 0, 0, type);
1599 type = value_type (lhs);
1600 }
1601
1602 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1603 {
1604 struct type *outer_type = NULL;
1605
1606 if (rust_enum_p (type))
1607 {
1608 const gdb_byte *valaddr = value_contents (lhs);
1609 struct field *variant_field = rust_enum_variant (type, valaddr);
1610
1611 struct value *union_value = value_primitive_field (lhs, 0, 0,
1612 type);
1613
1614 int fieldno = (variant_field
1615 - &TYPE_FIELD (value_type (union_value), 0));
1616 lhs = value_primitive_field (union_value, 0, fieldno,
1617 value_type (union_value));
1618 outer_type = type;
1619 type = value_type (lhs);
1620 }
1621
1622 /* Tuples and tuple structs */
1623 nfields = TYPE_NFIELDS (type);
1624
1625 if (field_number >= nfields || field_number < 0)
1626 {
1627 if (outer_type != NULL)
1628 error(_("Cannot access field %d of variant %s::%s, "
1629 "there are only %d fields"),
1630 field_number, TYPE_TAG_NAME (outer_type),
1631 rust_last_path_segment (TYPE_TAG_NAME (type)),
1632 nfields);
1633 else
1634 error(_("Cannot access field %d of %s, "
1635 "there are only %d fields"),
1636 field_number, TYPE_TAG_NAME (type), nfields);
1637 }
1638
1639 /* Tuples are tuple structs too. */
1640 if (!rust_tuple_struct_type_p (type))
1641 {
1642 if (outer_type != NULL)
1643 error(_("Variant %s::%s is not a tuple variant"),
1644 TYPE_TAG_NAME (outer_type),
1645 rust_last_path_segment (TYPE_TAG_NAME (type)));
1646 else
1647 error(_("Attempting to access anonymous field %d "
1648 "of %s, which is not a tuple, tuple struct, or "
1649 "tuple-like variant"),
1650 field_number, TYPE_TAG_NAME (type));
1651 }
1652
1653 result = value_primitive_field (lhs, 0, field_number, type);
1654 }
1655 else
1656 error(_("Anonymous field access is only allowed on tuples, \
1657 tuple structs, and tuple-like enum variants"));
1658 }
1659 break;
1660
1661 case STRUCTOP_STRUCT:
1662 {
1663 struct value *lhs;
1664 struct type *type;
1665 int tem, pc;
1666
1667 pc = (*pos)++;
1668 tem = longest_to_int (exp->elts[pc + 1].longconst);
1669 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1670 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1671
1672 const char *field_name = &exp->elts[pc + 2].string;
1673 type = value_type (lhs);
1674 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type))
1675 {
1676 const gdb_byte *valaddr = value_contents (lhs);
1677 struct field *variant_field = rust_enum_variant (type, valaddr);
1678
1679 struct value *union_value = value_primitive_field (lhs, 0, 0,
1680 type);
1681
1682 int fieldno = (variant_field
1683 - &TYPE_FIELD (value_type (union_value), 0));
1684 lhs = value_primitive_field (union_value, 0, fieldno,
1685 value_type (union_value));
1686
1687 struct type *outer_type = type;
1688 type = value_type (lhs);
1689 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1690 error (_("Attempting to access named field foo of tuple "
1691 "variant %s::%s, which has only anonymous fields"),
1692 TYPE_TAG_NAME (outer_type),
1693 rust_last_path_segment (TYPE_NAME (type)));
1694
1695 TRY
1696 {
1697 result = value_struct_elt (&lhs, NULL, field_name,
1698 NULL, "structure");
1699 }
1700 CATCH (except, RETURN_MASK_ERROR)
1701 {
1702 error (_("Could not find field %s of struct variant %s::%s"),
1703 field_name, TYPE_TAG_NAME (outer_type),
1704 rust_last_path_segment (TYPE_NAME (type)));
1705 }
1706 END_CATCH
1707 }
1708 else
1709 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1710 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1711 result = value_zero (value_type (result), VALUE_LVAL (result));
1712 }
1713 break;
1714
1715 case OP_RANGE:
1716 result = rust_range (exp, pos, noside);
1717 break;
1718
1719 case UNOP_ADDR:
1720 /* We might have &array[range], in which case we need to make a
1721 slice. */
1722 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1723 {
1724 ++*pos;
1725 result = rust_subscript (exp, pos, noside, 1);
1726 break;
1727 }
1728 /* Fall through. */
1729 default:
1730 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1731 break;
1732 }
1733
1734 return result;
1735 }
1736
1737 /* operator_length implementation for Rust. */
1738
1739 static void
1740 rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1741 int *argsp)
1742 {
1743 int oplen = 1;
1744 int args = 0;
1745
1746 switch (exp->elts[pc - 1].opcode)
1747 {
1748 case OP_AGGREGATE:
1749 /* We handle aggregate as a type and argument count. The first
1750 argument might be OP_OTHERS. After that the arguments
1751 alternate: first an OP_NAME, then an expression. */
1752 oplen = 4;
1753 args = longest_to_int (exp->elts[pc - 2].longconst);
1754 break;
1755
1756 case OP_OTHERS:
1757 oplen = 1;
1758 args = 1;
1759 break;
1760
1761 case STRUCTOP_ANONYMOUS:
1762 oplen = 3;
1763 args = 1;
1764 break;
1765
1766 case OP_RUST_ARRAY:
1767 oplen = 1;
1768 args = 2;
1769 break;
1770
1771 default:
1772 operator_length_standard (exp, pc, oplenp, argsp);
1773 return;
1774 }
1775
1776 *oplenp = oplen;
1777 *argsp = args;
1778 }
1779
1780 /* op_name implementation for Rust. */
1781
1782 static const char *
1783 rust_op_name (enum exp_opcode opcode)
1784 {
1785 switch (opcode)
1786 {
1787 case OP_AGGREGATE:
1788 return "OP_AGGREGATE";
1789 case OP_OTHERS:
1790 return "OP_OTHERS";
1791 default:
1792 return op_name_standard (opcode);
1793 }
1794 }
1795
1796 /* dump_subexp_body implementation for Rust. */
1797
1798 static int
1799 rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1800 int elt)
1801 {
1802 switch (exp->elts[elt].opcode)
1803 {
1804 case OP_AGGREGATE:
1805 {
1806 int length = longest_to_int (exp->elts[elt + 2].longconst);
1807 int i;
1808
1809 fprintf_filtered (stream, "Type @");
1810 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1811 fprintf_filtered (stream, " (");
1812 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1813 fprintf_filtered (stream, "), length %d", length);
1814
1815 elt += 4;
1816 for (i = 0; i < length; ++i)
1817 elt = dump_subexp (exp, stream, elt);
1818 }
1819 break;
1820
1821 case OP_STRING:
1822 case OP_NAME:
1823 {
1824 LONGEST len = exp->elts[elt + 1].longconst;
1825
1826 fprintf_filtered (stream, "%s: %s",
1827 (exp->elts[elt].opcode == OP_STRING
1828 ? "string" : "name"),
1829 &exp->elts[elt + 2].string);
1830 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1831 }
1832 break;
1833
1834 case OP_OTHERS:
1835 elt = dump_subexp (exp, stream, elt + 1);
1836 break;
1837
1838 case STRUCTOP_ANONYMOUS:
1839 {
1840 int field_number;
1841
1842 field_number = longest_to_int (exp->elts[elt + 1].longconst);
1843
1844 fprintf_filtered (stream, "Field number: %d", field_number);
1845 elt = dump_subexp (exp, stream, elt + 3);
1846 }
1847 break;
1848
1849 case OP_RUST_ARRAY:
1850 ++elt;
1851 break;
1852
1853 default:
1854 elt = dump_subexp_body_standard (exp, stream, elt);
1855 break;
1856 }
1857
1858 return elt;
1859 }
1860
1861 /* print_subexp implementation for Rust. */
1862
1863 static void
1864 rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1865 enum precedence prec)
1866 {
1867 switch (exp->elts[*pos].opcode)
1868 {
1869 case OP_AGGREGATE:
1870 {
1871 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1872 int i;
1873
1874 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1875 fputs_filtered (" { ", stream);
1876
1877 *pos += 4;
1878 for (i = 0; i < length; ++i)
1879 {
1880 rust_print_subexp (exp, pos, stream, prec);
1881 fputs_filtered (", ", stream);
1882 }
1883 fputs_filtered (" }", stream);
1884 }
1885 break;
1886
1887 case OP_NAME:
1888 {
1889 LONGEST len = exp->elts[*pos + 1].longconst;
1890
1891 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1892 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1893 }
1894 break;
1895
1896 case OP_OTHERS:
1897 {
1898 fputs_filtered ("<<others>> (", stream);
1899 ++*pos;
1900 rust_print_subexp (exp, pos, stream, prec);
1901 fputs_filtered (")", stream);
1902 }
1903 break;
1904
1905 case STRUCTOP_ANONYMOUS:
1906 {
1907 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1908
1909 (*pos) += 3;
1910 print_subexp (exp, pos, stream, PREC_SUFFIX);
1911 fprintf_filtered (stream, ".%d", tem);
1912 }
1913 break;
1914
1915 case OP_RUST_ARRAY:
1916 ++*pos;
1917 fprintf_filtered (stream, "[");
1918 rust_print_subexp (exp, pos, stream, prec);
1919 fprintf_filtered (stream, "; ");
1920 rust_print_subexp (exp, pos, stream, prec);
1921 fprintf_filtered (stream, "]");
1922 break;
1923
1924 default:
1925 print_subexp_standard (exp, pos, stream, prec);
1926 break;
1927 }
1928 }
1929
1930 /* operator_check implementation for Rust. */
1931
1932 static int
1933 rust_operator_check (struct expression *exp, int pos,
1934 int (*objfile_func) (struct objfile *objfile,
1935 void *data),
1936 void *data)
1937 {
1938 switch (exp->elts[pos].opcode)
1939 {
1940 case OP_AGGREGATE:
1941 {
1942 struct type *type = exp->elts[pos + 1].type;
1943 struct objfile *objfile = TYPE_OBJFILE (type);
1944
1945 if (objfile != NULL && (*objfile_func) (objfile, data))
1946 return 1;
1947 }
1948 break;
1949
1950 case OP_OTHERS:
1951 case OP_NAME:
1952 case OP_RUST_ARRAY:
1953 break;
1954
1955 default:
1956 return operator_check_standard (exp, pos, objfile_func, data);
1957 }
1958
1959 return 0;
1960 }
1961
1962 \f
1963
1964 /* Implementation of la_lookup_symbol_nonlocal for Rust. */
1965
1966 static struct block_symbol
1967 rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
1968 const char *name,
1969 const struct block *block,
1970 const domain_enum domain)
1971 {
1972 struct block_symbol result = {NULL, NULL};
1973
1974 if (symbol_lookup_debug)
1975 {
1976 fprintf_unfiltered (gdb_stdlog,
1977 "rust_lookup_symbol_non_local"
1978 " (%s, %s (scope %s), %s)\n",
1979 name, host_address_to_string (block),
1980 block_scope (block), domain_name (domain));
1981 }
1982
1983 /* Look up bare names in the block's scope. */
1984 std::string scopedname;
1985 if (name[cp_find_first_component (name)] == '\0')
1986 {
1987 const char *scope = block_scope (block);
1988
1989 if (scope[0] != '\0')
1990 {
1991 scopedname = std::string (scope) + "::" + name;
1992 name = scopedname.c_str ();
1993 }
1994 else
1995 name = NULL;
1996 }
1997
1998 if (name != NULL)
1999 {
2000 result = lookup_symbol_in_static_block (name, block, domain);
2001 if (result.symbol == NULL)
2002 result = lookup_global_symbol (name, block, domain);
2003 }
2004 return result;
2005 }
2006
2007 \f
2008
2009 /* la_sniff_from_mangled_name for Rust. */
2010
2011 static int
2012 rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2013 {
2014 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2015 return *demangled != NULL;
2016 }
2017
2018 \f
2019
2020 /* la_watch_location_expression for Rust. */
2021
2022 static gdb::unique_xmalloc_ptr<char>
2023 rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2024 {
2025 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2026 std::string name = type_to_string (type);
2027 return gdb::unique_xmalloc_ptr<char>
2028 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2029 name.c_str ()));
2030 }
2031
2032 \f
2033
2034 static const struct exp_descriptor exp_descriptor_rust =
2035 {
2036 rust_print_subexp,
2037 rust_operator_length,
2038 rust_operator_check,
2039 rust_op_name,
2040 rust_dump_subexp_body,
2041 rust_evaluate_subexp
2042 };
2043
2044 static const char *rust_extensions[] =
2045 {
2046 ".rs", NULL
2047 };
2048
2049 extern const struct language_defn rust_language_defn =
2050 {
2051 "rust",
2052 "Rust",
2053 language_rust,
2054 range_check_on,
2055 case_sensitive_on,
2056 array_row_major,
2057 macro_expansion_no,
2058 rust_extensions,
2059 &exp_descriptor_rust,
2060 rust_parse,
2061 rustyyerror,
2062 null_post_parser,
2063 rust_printchar, /* Print a character constant */
2064 rust_printstr, /* Function to print string constant */
2065 rust_emitchar, /* Print a single char */
2066 rust_print_type, /* Print a type using appropriate syntax */
2067 rust_print_typedef, /* Print a typedef using appropriate syntax */
2068 rust_val_print, /* Print a value using appropriate syntax */
2069 c_value_print, /* Print a top-level value */
2070 default_read_var_value, /* la_read_var_value */
2071 NULL, /* Language specific skip_trampoline */
2072 NULL, /* name_of_this */
2073 false, /* la_store_sym_names_in_linkage_form_p */
2074 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2075 basic_lookup_transparent_type,/* lookup_transparent_type */
2076 gdb_demangle, /* Language specific symbol demangler */
2077 rust_sniff_from_mangled_name,
2078 NULL, /* Language specific
2079 class_name_from_physname */
2080 c_op_print_tab, /* expression operators for printing */
2081 1, /* c-style arrays */
2082 0, /* String lower bound */
2083 default_word_break_characters,
2084 default_collect_symbol_completion_matches,
2085 rust_language_arch_info,
2086 default_print_array_index,
2087 default_pass_by_reference,
2088 c_get_string,
2089 rust_watch_location_expression,
2090 NULL, /* la_get_symbol_name_matcher */
2091 iterate_over_symbols,
2092 default_search_name_hash,
2093 &default_varobj_ops,
2094 NULL,
2095 NULL,
2096 LANG_MAGIC
2097 };
This page took 0.107624 seconds and 5 git commands to generate.