746f565947be8e74dcbc1529d38e127129c15f1c
[binutils-gdb.git] / gdb / rust-lang.c
1 /* Rust language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 2016-2022 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 "typeprint.h"
35 #include "valprint.h"
36 #include "varobj.h"
37 #include <algorithm>
38 #include <string>
39 #include <vector>
40 #include "cli/cli-style.h"
41 #include "parser-defs.h"
42 #include "rust-exp.h"
43
44 /* See rust-lang.h. */
45
46 const char *
47 rust_last_path_segment (const char *path)
48 {
49 const char *result = strrchr (path, ':');
50
51 if (result == NULL)
52 return path;
53 return result + 1;
54 }
55
56 /* See rust-lang.h. */
57
58 std::string
59 rust_crate_for_block (const struct block *block)
60 {
61 const char *scope = block_scope (block);
62
63 if (scope[0] == '\0')
64 return std::string ();
65
66 return std::string (scope, cp_find_first_component (scope));
67 }
68
69 /* Return true if TYPE, which must be a struct type, represents a Rust
70 enum. */
71
72 static bool
73 rust_enum_p (struct type *type)
74 {
75 /* is_dynamic_type will return true if any field has a dynamic
76 attribute -- but we only want to check the top level. */
77 return TYPE_HAS_VARIANT_PARTS (type);
78 }
79
80 /* Return true if TYPE, which must be an already-resolved enum type,
81 has no variants. */
82
83 static bool
84 rust_empty_enum_p (const struct type *type)
85 {
86 return type->num_fields () == 0;
87 }
88
89 /* Given an already-resolved enum type and contents, find which
90 variant is active. */
91
92 static int
93 rust_enum_variant (struct type *type)
94 {
95 /* The active variant is simply the first non-artificial field. */
96 for (int i = 0; i < type->num_fields (); ++i)
97 if (!TYPE_FIELD_ARTIFICIAL (type, i))
98 return i;
99
100 /* Perhaps we could get here by trying to print an Ada variant
101 record in Rust mode. Unlikely, but an error is safer than an
102 assert. */
103 error (_("Could not find active enum variant"));
104 }
105
106 /* See rust-lang.h. */
107
108 bool
109 rust_tuple_type_p (struct type *type)
110 {
111 /* The current implementation is a bit of a hack, but there's
112 nothing else in the debuginfo to distinguish a tuple from a
113 struct. */
114 return (type->code () == TYPE_CODE_STRUCT
115 && type->name () != NULL
116 && type->name ()[0] == '(');
117 }
118
119 /* Return true if all non-static fields of a structlike type are in a
120 sequence like __0, __1, __2. */
121
122 static bool
123 rust_underscore_fields (struct type *type)
124 {
125 int i, field_number;
126
127 field_number = 0;
128
129 if (type->code () != TYPE_CODE_STRUCT)
130 return false;
131 for (i = 0; i < type->num_fields (); ++i)
132 {
133 if (!field_is_static (&type->field (i)))
134 {
135 char buf[20];
136
137 xsnprintf (buf, sizeof (buf), "__%d", field_number);
138 if (strcmp (buf, type->field (i).name ()) != 0)
139 return false;
140 field_number++;
141 }
142 }
143 return true;
144 }
145
146 /* See rust-lang.h. */
147
148 bool
149 rust_tuple_struct_type_p (struct type *type)
150 {
151 /* This is just an approximation until DWARF can represent Rust more
152 precisely. We exclude zero-length structs because they may not
153 be tuple structs, and there's no way to tell. */
154 return type->num_fields () > 0 && rust_underscore_fields (type);
155 }
156
157 /* Return true if TYPE is a slice type, otherwise false. */
158
159 static bool
160 rust_slice_type_p (struct type *type)
161 {
162 if (type->code () == TYPE_CODE_STRUCT
163 && type->name () != NULL
164 && type->num_fields () == 2)
165 {
166 /* The order of fields doesn't matter. While it would be nice
167 to check for artificiality here, the Rust compiler doesn't
168 emit this information. */
169 const char *n1 = type->field (0).name ();
170 const char *n2 = type->field (1).name ();
171 return ((streq (n1, "data_ptr") && streq (n2, "length"))
172 || (streq (n2, "data_ptr") && streq (n1, "length")));
173 }
174 return false;
175 }
176
177 /* Return true if TYPE is a range type, otherwise false. */
178
179 static bool
180 rust_range_type_p (struct type *type)
181 {
182 int i;
183
184 if (type->code () != TYPE_CODE_STRUCT
185 || type->num_fields () > 2
186 || type->name () == NULL
187 || strstr (type->name (), "::Range") == NULL)
188 return false;
189
190 if (type->num_fields () == 0)
191 return true;
192
193 i = 0;
194 if (strcmp (type->field (0).name (), "start") == 0)
195 {
196 if (type->num_fields () == 1)
197 return true;
198 i = 1;
199 }
200 else if (type->num_fields () == 2)
201 {
202 /* First field had to be "start". */
203 return false;
204 }
205
206 return strcmp (type->field (i).name (), "end") == 0;
207 }
208
209 /* Return true if TYPE is an inclusive range type, otherwise false.
210 This is only valid for types which are already known to be range
211 types. */
212
213 static bool
214 rust_inclusive_range_type_p (struct type *type)
215 {
216 return (strstr (type->name (), "::RangeInclusive") != NULL
217 || strstr (type->name (), "::RangeToInclusive") != NULL);
218 }
219
220 /* Return true if TYPE seems to be the type "u8", otherwise false. */
221
222 static bool
223 rust_u8_type_p (struct type *type)
224 {
225 return (type->code () == TYPE_CODE_INT
226 && type->is_unsigned ()
227 && TYPE_LENGTH (type) == 1);
228 }
229
230 /* Return true if TYPE is a Rust character type. */
231
232 static bool
233 rust_chartype_p (struct type *type)
234 {
235 return (type->code () == TYPE_CODE_CHAR
236 && TYPE_LENGTH (type) == 4
237 && type->is_unsigned ());
238 }
239
240 /* If VALUE represents a trait object pointer, return the underlying
241 pointer with the correct (i.e., runtime) type. Otherwise, return
242 NULL. */
243
244 static struct value *
245 rust_get_trait_object_pointer (struct value *value)
246 {
247 struct type *type = check_typedef (value_type (value));
248
249 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
250 return NULL;
251
252 /* Try to be a bit resilient if the ABI changes. */
253 int vtable_field = 0;
254 for (int i = 0; i < 2; ++i)
255 {
256 if (strcmp (type->field (i).name (), "vtable") == 0)
257 vtable_field = i;
258 else if (strcmp (type->field (i).name (), "pointer") != 0)
259 return NULL;
260 }
261
262 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
263 struct symbol *symbol = find_symbol_at_address (vtable);
264 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
265 return NULL;
266
267 struct rust_vtable_symbol *vtable_sym
268 = static_cast<struct rust_vtable_symbol *> (symbol);
269 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
270 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
271 }
272
273 \f
274
275 /* See language.h. */
276
277 void
278 rust_language::printstr (struct ui_file *stream, struct type *type,
279 const gdb_byte *string, unsigned int length,
280 const char *user_encoding, int force_ellipses,
281 const struct value_print_options *options) const
282 {
283 /* Rust always uses UTF-8, but let the caller override this if need
284 be. */
285 const char *encoding = user_encoding;
286 if (user_encoding == NULL || !*user_encoding)
287 {
288 /* In Rust strings, characters are "u8". */
289 if (rust_u8_type_p (type))
290 encoding = "UTF-8";
291 else
292 {
293 /* This is probably some C string, so let's let C deal with
294 it. */
295 c_printstr (stream, type, string, length, user_encoding,
296 force_ellipses, options);
297 return;
298 }
299 }
300
301 /* This is not ideal as it doesn't use our character printer. */
302 generic_printstr (stream, type, string, length, encoding, force_ellipses,
303 '"', 0, options);
304 }
305
306 \f
307
308 static const struct generic_val_print_decorations rust_decorations =
309 {
310 /* Complex isn't used in Rust, but we provide C-ish values just in
311 case. */
312 "",
313 " + ",
314 " * I",
315 "true",
316 "false",
317 "()",
318 "[",
319 "]"
320 };
321
322 /* Helper function to print a slice. */
323
324 static void
325 rust_val_print_slice (struct value *val, struct ui_file *stream, int recurse,
326 const struct value_print_options *options)
327 {
328 struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
329 "slice");
330 struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
331
332 struct type *type = check_typedef (value_type (val));
333 if (strcmp (type->name (), "&str") == 0)
334 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
335 value_as_address (base), value_as_long (len), stream,
336 options);
337 else
338 {
339 LONGEST llen = value_as_long (len);
340
341 type_print (value_type (val), "", stream, -1);
342 gdb_printf (stream, " ");
343
344 if (llen == 0)
345 gdb_printf (stream, "[]");
346 else
347 {
348 struct type *elt_type = TYPE_TARGET_TYPE (value_type (base));
349 struct type *array_type = lookup_array_range_type (elt_type, 0,
350 llen - 1);
351 struct value *array = allocate_value_lazy (array_type);
352 VALUE_LVAL (array) = lval_memory;
353 set_value_address (array, value_as_address (base));
354 value_fetch_lazy (array);
355 generic_value_print (array, stream, recurse, options,
356 &rust_decorations);
357 }
358 }
359 }
360
361 /* See rust-lang.h. */
362
363 void
364 rust_language::val_print_struct
365 (struct value *val, struct ui_file *stream, int recurse,
366 const struct value_print_options *options) const
367 {
368 int i;
369 int first_field;
370 struct type *type = check_typedef (value_type (val));
371
372 if (rust_slice_type_p (type))
373 {
374 rust_val_print_slice (val, stream, recurse, options);
375 return;
376 }
377
378 bool is_tuple = rust_tuple_type_p (type);
379 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
380 struct value_print_options opts;
381
382 if (!is_tuple)
383 {
384 if (type->name () != NULL)
385 gdb_printf (stream, "%s", type->name ());
386
387 if (type->num_fields () == 0)
388 return;
389
390 if (type->name () != NULL)
391 gdb_puts (" ", stream);
392 }
393
394 if (is_tuple || is_tuple_struct)
395 gdb_puts ("(", stream);
396 else
397 gdb_puts ("{", stream);
398
399 opts = *options;
400 opts.deref_ref = 0;
401
402 first_field = 1;
403 for (i = 0; i < type->num_fields (); ++i)
404 {
405 if (field_is_static (&type->field (i)))
406 continue;
407
408 if (!first_field)
409 gdb_puts (",", stream);
410
411 if (options->prettyformat)
412 {
413 gdb_puts ("\n", stream);
414 print_spaces (2 + 2 * recurse, stream);
415 }
416 else if (!first_field)
417 gdb_puts (" ", stream);
418
419 first_field = 0;
420
421 if (!is_tuple && !is_tuple_struct)
422 {
423 fputs_styled (type->field (i).name (),
424 variable_name_style.style (), stream);
425 gdb_puts (": ", stream);
426 }
427
428 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
429 this);
430 }
431
432 if (options->prettyformat)
433 {
434 gdb_puts ("\n", stream);
435 print_spaces (2 * recurse, stream);
436 }
437
438 if (is_tuple || is_tuple_struct)
439 gdb_puts (")", stream);
440 else
441 gdb_puts ("}", stream);
442 }
443
444 /* See rust-lang.h. */
445
446 void
447 rust_language::print_enum (struct value *val, struct ui_file *stream,
448 int recurse,
449 const struct value_print_options *options) const
450 {
451 struct value_print_options opts = *options;
452 struct type *type = check_typedef (value_type (val));
453
454 opts.deref_ref = 0;
455
456 gdb_assert (rust_enum_p (type));
457 gdb::array_view<const gdb_byte> view
458 (value_contents_for_printing (val).data (),
459 TYPE_LENGTH (value_type (val)));
460 type = resolve_dynamic_type (type, view, value_address (val));
461
462 if (rust_empty_enum_p (type))
463 {
464 /* Print the enum type name here to be more clear. */
465 gdb_printf (stream, _("%s {%p[<No data fields>%p]}"),
466 type->name (),
467 metadata_style.style ().ptr (), nullptr);
468 return;
469 }
470
471 int variant_fieldno = rust_enum_variant (type);
472 val = value_field (val, variant_fieldno);
473 struct type *variant_type = type->field (variant_fieldno).type ();
474
475 int nfields = variant_type->num_fields ();
476
477 bool is_tuple = rust_tuple_struct_type_p (variant_type);
478
479 gdb_printf (stream, "%s", variant_type->name ());
480 if (nfields == 0)
481 {
482 /* In case of a nullary variant like 'None', just output
483 the name. */
484 return;
485 }
486
487 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
488 if (is_tuple)
489 gdb_printf (stream, "(");
490 else
491 {
492 /* struct variant. */
493 gdb_printf (stream, "{");
494 }
495
496 bool first_field = true;
497 for (int j = 0; j < variant_type->num_fields (); j++)
498 {
499 if (!first_field)
500 gdb_puts (", ", stream);
501 first_field = false;
502
503 if (!is_tuple)
504 gdb_printf (stream, "%ps: ",
505 styled_string (variable_name_style.style (),
506 variant_type->field (j).name ()));
507
508 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
509 this);
510 }
511
512 if (is_tuple)
513 gdb_puts (")", stream);
514 else
515 gdb_puts ("}", stream);
516 }
517
518 /* See language.h. */
519
520 void
521 rust_language::value_print_inner
522 (struct value *val, struct ui_file *stream, int recurse,
523 const struct value_print_options *options) const
524 {
525 struct value_print_options opts = *options;
526 opts.deref_ref = 1;
527
528 if (opts.prettyformat == Val_prettyformat_default)
529 opts.prettyformat = (opts.prettyformat_structs
530 ? Val_prettyformat : Val_no_prettyformat);
531
532 struct type *type = check_typedef (value_type (val));
533 switch (type->code ())
534 {
535 case TYPE_CODE_PTR:
536 {
537 LONGEST low_bound, high_bound;
538
539 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
540 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
541 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
542 &high_bound))
543 {
544 /* We have a pointer to a byte string, so just print
545 that. */
546 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
547 CORE_ADDR addr = value_as_address (val);
548 struct gdbarch *arch = type->arch ();
549
550 if (opts.addressprint)
551 {
552 gdb_puts (paddress (arch, addr), stream);
553 gdb_puts (" ", stream);
554 }
555
556 gdb_puts ("b", stream);
557 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
558 high_bound - low_bound + 1, stream,
559 &opts);
560 break;
561 }
562 }
563 goto generic_print;
564
565 case TYPE_CODE_INT:
566 /* Recognize the unit type. */
567 if (type->is_unsigned () && TYPE_LENGTH (type) == 0
568 && type->name () != NULL && strcmp (type->name (), "()") == 0)
569 {
570 gdb_puts ("()", stream);
571 break;
572 }
573 goto generic_print;
574
575 case TYPE_CODE_STRING:
576 {
577 LONGEST low_bound, high_bound;
578
579 if (!get_array_bounds (type, &low_bound, &high_bound))
580 error (_("Could not determine the array bounds"));
581
582 /* If we see a plain TYPE_CODE_STRING, then we're printing a
583 byte string, hence the choice of "ASCII" as the
584 encoding. */
585 gdb_puts ("b", stream);
586 printstr (stream, TYPE_TARGET_TYPE (type),
587 value_contents_for_printing (val).data (),
588 high_bound - low_bound + 1, "ASCII", 0, &opts);
589 }
590 break;
591
592 case TYPE_CODE_ARRAY:
593 {
594 LONGEST low_bound, high_bound;
595
596 if (get_array_bounds (type, &low_bound, &high_bound)
597 && high_bound - low_bound + 1 == 0)
598 gdb_puts ("[]", stream);
599 else
600 goto generic_print;
601 }
602 break;
603
604 case TYPE_CODE_UNION:
605 /* Untagged unions are printed as if they are structs. Since
606 the field bit positions overlap in the debuginfo, the code
607 for printing a union is same as that for a struct, the only
608 difference is that the input type will have overlapping
609 fields. */
610 val_print_struct (val, stream, recurse, &opts);
611 break;
612
613 case TYPE_CODE_STRUCT:
614 if (rust_enum_p (type))
615 print_enum (val, stream, recurse, &opts);
616 else
617 val_print_struct (val, stream, recurse, &opts);
618 break;
619
620 default:
621 generic_print:
622 /* Nothing special yet. */
623 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
624 }
625 }
626
627 /* See language.h. */
628
629 void
630 rust_language::value_print
631 (struct value *val, struct ui_file *stream,
632 const struct value_print_options *options) const
633 {
634 value_print_options opts = *options;
635 opts.deref_ref = true;
636
637 struct type *type = check_typedef (value_type (val));
638 if (type->is_pointer_or_reference ())
639 {
640 gdb_printf (stream, "(");
641 type_print (value_type (val), "", stream, -1);
642 gdb_printf (stream, ") ");
643 }
644
645 return common_val_print (val, stream, 0, &opts, this);
646 }
647
648 \f
649
650 static void
651 rust_internal_print_type (struct type *type, const char *varstring,
652 struct ui_file *stream, int show, int level,
653 const struct type_print_options *flags,
654 bool for_rust_enum, print_offset_data *podata);
655
656 /* Print a struct or union typedef. */
657 static void
658 rust_print_struct_def (struct type *type, const char *varstring,
659 struct ui_file *stream, int show, int level,
660 const struct type_print_options *flags,
661 bool for_rust_enum, print_offset_data *podata)
662 {
663 /* Print a tuple type simply. */
664 if (rust_tuple_type_p (type))
665 {
666 gdb_puts (type->name (), stream);
667 return;
668 }
669
670 /* If we see a base class, delegate to C. */
671 if (TYPE_N_BASECLASSES (type) > 0)
672 c_print_type (type, varstring, stream, show, level, language_rust, flags);
673
674 if (flags->print_offsets)
675 {
676 /* Temporarily bump the level so that the output lines up
677 correctly. */
678 level += 2;
679 }
680
681 /* Compute properties of TYPE here because, in the enum case, the
682 rest of the code ends up looking only at the variant part. */
683 const char *tagname = type->name ();
684 bool is_tuple_struct = rust_tuple_struct_type_p (type);
685 bool is_tuple = rust_tuple_type_p (type);
686 bool is_enum = rust_enum_p (type);
687
688 if (for_rust_enum)
689 {
690 /* Already printing an outer enum, so nothing to print here. */
691 }
692 else
693 {
694 /* This code path is also used by unions and enums. */
695 if (is_enum)
696 {
697 gdb_puts ("enum ", stream);
698 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
699 if (prop != nullptr && prop->kind () == PROP_TYPE)
700 type = prop->original_type ();
701 }
702 else if (type->code () == TYPE_CODE_STRUCT)
703 gdb_puts ("struct ", stream);
704 else
705 gdb_puts ("union ", stream);
706
707 if (tagname != NULL)
708 gdb_puts (tagname, stream);
709 }
710
711 if (type->num_fields () == 0 && !is_tuple)
712 return;
713 if (for_rust_enum && !flags->print_offsets)
714 gdb_puts (is_tuple_struct ? "(" : "{", stream);
715 else
716 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
717
718 /* When printing offsets, we rearrange the fields into storage
719 order. This lets us show holes more clearly. We work using
720 field indices here because it simplifies calls to
721 print_offset_data::update below. */
722 std::vector<int> fields;
723 for (int i = 0; i < type->num_fields (); ++i)
724 {
725 if (field_is_static (&type->field (i)))
726 continue;
727 if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
728 continue;
729 fields.push_back (i);
730 }
731 if (flags->print_offsets)
732 std::sort (fields.begin (), fields.end (),
733 [&] (int a, int b)
734 {
735 return (type->field (a).loc_bitpos ()
736 < type->field (b).loc_bitpos ());
737 });
738
739 for (int i : fields)
740 {
741 QUIT;
742
743 gdb_assert (!field_is_static (&type->field (i)));
744 gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
745
746 if (flags->print_offsets)
747 podata->update (type, i, stream);
748
749 /* We'd like to print "pub" here as needed, but rustc
750 doesn't emit the debuginfo, and our types don't have
751 cplus_struct_type attached. */
752
753 /* For a tuple struct we print the type but nothing
754 else. */
755 if (!for_rust_enum || flags->print_offsets)
756 print_spaces (level + 2, stream);
757 if (is_enum)
758 fputs_styled (type->field (i).name (), variable_name_style.style (),
759 stream);
760 else if (!is_tuple_struct)
761 gdb_printf (stream, "%ps: ",
762 styled_string (variable_name_style.style (),
763 type->field (i).name ()));
764
765 rust_internal_print_type (type->field (i).type (), NULL,
766 stream, (is_enum ? show : show - 1),
767 level + 2, flags, is_enum, podata);
768 if (!for_rust_enum || flags->print_offsets)
769 gdb_puts (",\n", stream);
770 /* Note that this check of "I" is ok because we only sorted the
771 fields by offset when print_offsets was set, so we won't take
772 this branch in that case. */
773 else if (i + 1 < type->num_fields ())
774 gdb_puts (", ", stream);
775 }
776
777 if (flags->print_offsets)
778 {
779 /* Undo the temporary level increase we did above. */
780 level -= 2;
781 podata->finish (type, level, stream);
782 print_spaces (print_offset_data::indentation, stream);
783 if (level == 0)
784 print_spaces (2, stream);
785 }
786 if (!for_rust_enum || flags->print_offsets)
787 print_spaces (level, stream);
788 gdb_puts (is_tuple_struct ? ")" : "}", stream);
789 }
790
791 /* la_print_type implementation for Rust. */
792
793 static void
794 rust_internal_print_type (struct type *type, const char *varstring,
795 struct ui_file *stream, int show, int level,
796 const struct type_print_options *flags,
797 bool for_rust_enum, print_offset_data *podata)
798 {
799 QUIT;
800 if (show <= 0
801 && type->name () != NULL)
802 {
803 /* Rust calls the unit type "void" in its debuginfo,
804 but we don't want to print it as that. */
805 if (type->code () == TYPE_CODE_VOID)
806 gdb_puts ("()", stream);
807 else
808 gdb_puts (type->name (), stream);
809 return;
810 }
811
812 type = check_typedef (type);
813 switch (type->code ())
814 {
815 case TYPE_CODE_VOID:
816 /* If we have an enum, we've already printed the type's
817 unqualified name, and there is nothing else to print
818 here. */
819 if (!for_rust_enum)
820 gdb_puts ("()", stream);
821 break;
822
823 case TYPE_CODE_FUNC:
824 /* Delegate varargs to the C printer. */
825 if (type->has_varargs ())
826 goto c_printer;
827
828 gdb_puts ("fn ", stream);
829 if (varstring != NULL)
830 gdb_puts (varstring, stream);
831 gdb_puts ("(", stream);
832 for (int i = 0; i < type->num_fields (); ++i)
833 {
834 QUIT;
835 if (i > 0)
836 gdb_puts (", ", stream);
837 rust_internal_print_type (type->field (i).type (), "", stream,
838 -1, 0, flags, false, podata);
839 }
840 gdb_puts (")", stream);
841 /* If it returns unit, we can omit the return type. */
842 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
843 {
844 gdb_puts (" -> ", stream);
845 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
846 -1, 0, flags, false, podata);
847 }
848 break;
849
850 case TYPE_CODE_ARRAY:
851 {
852 LONGEST low_bound, high_bound;
853
854 gdb_puts ("[", stream);
855 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
856 stream, show - 1, level, flags, false,
857 podata);
858
859 if (type->bounds ()->high.kind () == PROP_LOCEXPR
860 || type->bounds ()->high.kind () == PROP_LOCLIST)
861 gdb_printf (stream, "; variable length");
862 else if (get_array_bounds (type, &low_bound, &high_bound))
863 gdb_printf (stream, "; %s",
864 plongest (high_bound - low_bound + 1));
865 gdb_puts ("]", stream);
866 }
867 break;
868
869 case TYPE_CODE_UNION:
870 case TYPE_CODE_STRUCT:
871 rust_print_struct_def (type, varstring, stream, show, level, flags,
872 for_rust_enum, podata);
873 break;
874
875 case TYPE_CODE_ENUM:
876 {
877 int len = 0;
878
879 gdb_puts ("enum ", stream);
880 if (type->name () != NULL)
881 {
882 gdb_puts (type->name (), stream);
883 gdb_puts (" ", stream);
884 len = strlen (type->name ());
885 }
886 gdb_puts ("{\n", stream);
887
888 for (int i = 0; i < type->num_fields (); ++i)
889 {
890 const char *name = type->field (i).name ();
891
892 QUIT;
893
894 if (len > 0
895 && strncmp (name, type->name (), len) == 0
896 && name[len] == ':'
897 && name[len + 1] == ':')
898 name += len + 2;
899 gdb_printf (stream, "%*s%ps,\n",
900 level + 2, "",
901 styled_string (variable_name_style.style (),
902 name));
903 }
904
905 gdb_puts ("}", stream);
906 }
907 break;
908
909 case TYPE_CODE_PTR:
910 {
911 if (type->name () != nullptr)
912 gdb_puts (type->name (), stream);
913 else
914 {
915 /* We currently can't distinguish between pointers and
916 references. */
917 gdb_puts ("*mut ", stream);
918 type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
919 }
920 }
921 break;
922
923 default:
924 c_printer:
925 c_print_type (type, varstring, stream, show, level, language_rust,
926 flags);
927 }
928 }
929
930 \f
931
932 /* Like arch_composite_type, but uses TYPE to decide how to allocate
933 -- either on an obstack or on a gdbarch. */
934
935 static struct type *
936 rust_composite_type (struct type *original,
937 const char *name,
938 const char *field1, struct type *type1,
939 const char *field2, struct type *type2)
940 {
941 struct type *result = alloc_type_copy (original);
942 int i, nfields, bitpos;
943
944 nfields = 0;
945 if (field1 != NULL)
946 ++nfields;
947 if (field2 != NULL)
948 ++nfields;
949
950 result->set_code (TYPE_CODE_STRUCT);
951 result->set_name (name);
952
953 result->set_num_fields (nfields);
954 result->set_fields
955 ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
956
957 i = 0;
958 bitpos = 0;
959 if (field1 != NULL)
960 {
961 struct field *field = &result->field (i);
962
963 field->set_loc_bitpos (bitpos);
964 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
965
966 field->set_name (field1);
967 field->set_type (type1);
968 ++i;
969 }
970 if (field2 != NULL)
971 {
972 struct field *field = &result->field (i);
973 unsigned align = type_align (type2);
974
975 if (align != 0)
976 {
977 int delta;
978
979 align *= TARGET_CHAR_BIT;
980 delta = bitpos % align;
981 if (delta != 0)
982 bitpos += align - delta;
983 }
984 field->set_loc_bitpos (bitpos);
985
986 field->set_name (field2);
987 field->set_type (type2);
988 ++i;
989 }
990
991 if (i > 0)
992 TYPE_LENGTH (result)
993 = (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT +
994 TYPE_LENGTH (result->field (i - 1).type ()));
995 return result;
996 }
997
998 /* See rust-lang.h. */
999
1000 struct type *
1001 rust_slice_type (const char *name, struct type *elt_type,
1002 struct type *usize_type)
1003 {
1004 struct type *type;
1005
1006 elt_type = lookup_pointer_type (elt_type);
1007 type = rust_composite_type (elt_type, name,
1008 "data_ptr", elt_type,
1009 "length", usize_type);
1010
1011 return type;
1012 }
1013
1014 \f
1015
1016 /* A helper for rust_evaluate_subexp that handles OP_RANGE. */
1017
1018 struct value *
1019 rust_range (struct type *expect_type, struct expression *exp,
1020 enum noside noside, enum range_flag kind,
1021 struct value *low, struct value *high)
1022 {
1023 struct value *addrval, *result;
1024 CORE_ADDR addr;
1025 struct type *range_type;
1026 struct type *index_type;
1027 struct type *temp_type;
1028 const char *name;
1029
1030 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
1031
1032 if (low == NULL)
1033 {
1034 if (high == NULL)
1035 {
1036 index_type = NULL;
1037 name = "std::ops::RangeFull";
1038 }
1039 else
1040 {
1041 index_type = value_type (high);
1042 name = (inclusive
1043 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
1044 }
1045 }
1046 else
1047 {
1048 if (high == NULL)
1049 {
1050 index_type = value_type (low);
1051 name = "std::ops::RangeFrom";
1052 }
1053 else
1054 {
1055 if (!types_equal (value_type (low), value_type (high)))
1056 error (_("Range expression with different types"));
1057 index_type = value_type (low);
1058 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
1059 }
1060 }
1061
1062 /* If we don't have an index type, just allocate this on the
1063 arch. Here any type will do. */
1064 temp_type = (index_type == NULL
1065 ? language_bool_type (exp->language_defn, exp->gdbarch)
1066 : index_type);
1067 /* It would be nicer to cache the range type. */
1068 range_type = rust_composite_type (temp_type, name,
1069 low == NULL ? NULL : "start", index_type,
1070 high == NULL ? NULL : "end", index_type);
1071
1072 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1073 return value_zero (range_type, lval_memory);
1074
1075 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1076 addr = value_as_long (addrval);
1077 result = value_at_lazy (range_type, addr);
1078
1079 if (low != NULL)
1080 {
1081 struct value *start = value_struct_elt (&result, {}, "start", NULL,
1082 "range");
1083
1084 value_assign (start, low);
1085 }
1086
1087 if (high != NULL)
1088 {
1089 struct value *end = value_struct_elt (&result, {}, "end", NULL,
1090 "range");
1091
1092 value_assign (end, high);
1093 }
1094
1095 result = value_at_lazy (range_type, addr);
1096 return result;
1097 }
1098
1099 /* A helper function to compute the range and kind given a range
1100 value. TYPE is the type of the range value. RANGE is the range
1101 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1102 parameters might be filled in, or might not be, depending on the
1103 kind of range this is. KIND will always be set to the appropriate
1104 value describing the kind of range, and this can be used to
1105 determine whether LOW or HIGH are valid. */
1106
1107 static void
1108 rust_compute_range (struct type *type, struct value *range,
1109 LONGEST *low, LONGEST *high,
1110 range_flags *kind)
1111 {
1112 int i;
1113
1114 *low = 0;
1115 *high = 0;
1116 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1117
1118 if (type->num_fields () == 0)
1119 return;
1120
1121 i = 0;
1122 if (strcmp (type->field (0).name (), "start") == 0)
1123 {
1124 *kind = RANGE_HIGH_BOUND_DEFAULT;
1125 *low = value_as_long (value_field (range, 0));
1126 ++i;
1127 }
1128 if (type->num_fields () > i
1129 && strcmp (type->field (i).name (), "end") == 0)
1130 {
1131 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1132 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
1133 *high = value_as_long (value_field (range, i));
1134
1135 if (rust_inclusive_range_type_p (type))
1136 ++*high;
1137 }
1138 }
1139
1140 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1141
1142 struct value *
1143 rust_subscript (struct type *expect_type, struct expression *exp,
1144 enum noside noside, bool for_addr,
1145 struct value *lhs, struct value *rhs)
1146 {
1147 struct value *result;
1148 struct type *rhstype;
1149 LONGEST low, high_bound;
1150 /* Initialized to appease the compiler. */
1151 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
1152 LONGEST high = 0;
1153 int want_slice = 0;
1154
1155 rhstype = check_typedef (value_type (rhs));
1156 if (rust_range_type_p (rhstype))
1157 {
1158 if (!for_addr)
1159 error (_("Can't take slice of array without '&'"));
1160 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1161 want_slice = 1;
1162 }
1163 else
1164 low = value_as_long (rhs);
1165
1166 struct type *type = check_typedef (value_type (lhs));
1167 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1168 {
1169 struct type *base_type = nullptr;
1170 if (type->code () == TYPE_CODE_ARRAY)
1171 base_type = TYPE_TARGET_TYPE (type);
1172 else if (rust_slice_type_p (type))
1173 {
1174 for (int i = 0; i < type->num_fields (); ++i)
1175 {
1176 if (strcmp (type->field (i).name (), "data_ptr") == 0)
1177 {
1178 base_type = TYPE_TARGET_TYPE (type->field (i).type ());
1179 break;
1180 }
1181 }
1182 if (base_type == nullptr)
1183 error (_("Could not find 'data_ptr' in slice type"));
1184 }
1185 else if (type->code () == TYPE_CODE_PTR)
1186 base_type = TYPE_TARGET_TYPE (type);
1187 else
1188 error (_("Cannot subscript non-array type"));
1189
1190 struct type *new_type;
1191 if (want_slice)
1192 {
1193 if (rust_slice_type_p (type))
1194 new_type = type;
1195 else
1196 {
1197 struct type *usize
1198 = language_lookup_primitive_type (exp->language_defn,
1199 exp->gdbarch,
1200 "usize");
1201 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1202 }
1203 }
1204 else
1205 new_type = base_type;
1206
1207 return value_zero (new_type, VALUE_LVAL (lhs));
1208 }
1209 else
1210 {
1211 LONGEST low_bound;
1212 struct value *base;
1213
1214 if (type->code () == TYPE_CODE_ARRAY)
1215 {
1216 base = lhs;
1217 if (!get_array_bounds (type, &low_bound, &high_bound))
1218 error (_("Can't compute array bounds"));
1219 if (low_bound != 0)
1220 error (_("Found array with non-zero lower bound"));
1221 ++high_bound;
1222 }
1223 else if (rust_slice_type_p (type))
1224 {
1225 struct value *len;
1226
1227 base = value_struct_elt (&lhs, {}, "data_ptr", NULL, "slice");
1228 len = value_struct_elt (&lhs, {}, "length", NULL, "slice");
1229 low_bound = 0;
1230 high_bound = value_as_long (len);
1231 }
1232 else if (type->code () == TYPE_CODE_PTR)
1233 {
1234 base = lhs;
1235 low_bound = 0;
1236 high_bound = LONGEST_MAX;
1237 }
1238 else
1239 error (_("Cannot subscript non-array type"));
1240
1241 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
1242 low = low_bound;
1243 if (low < 0)
1244 error (_("Index less than zero"));
1245 if (low > high_bound)
1246 error (_("Index greater than length"));
1247
1248 result = value_subscript (base, low);
1249 }
1250
1251 if (for_addr)
1252 {
1253 if (want_slice)
1254 {
1255 struct type *usize, *slice;
1256 CORE_ADDR addr;
1257 struct value *addrval, *tem;
1258
1259 if (kind & RANGE_HIGH_BOUND_DEFAULT)
1260 high = high_bound;
1261 if (high < 0)
1262 error (_("High index less than zero"));
1263 if (low > high)
1264 error (_("Low index greater than high index"));
1265 if (high > high_bound)
1266 error (_("High index greater than length"));
1267
1268 usize = language_lookup_primitive_type (exp->language_defn,
1269 exp->gdbarch,
1270 "usize");
1271 const char *new_name = ((type != nullptr
1272 && rust_slice_type_p (type))
1273 ? type->name () : "&[*gdb*]");
1274
1275 slice = rust_slice_type (new_name, value_type (result), usize);
1276
1277 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1278 addr = value_as_long (addrval);
1279 tem = value_at_lazy (slice, addr);
1280
1281 value_assign (value_field (tem, 0), value_addr (result));
1282 value_assign (value_field (tem, 1),
1283 value_from_longest (usize, high - low));
1284
1285 result = value_at_lazy (slice, addr);
1286 }
1287 else
1288 result = value_addr (result);
1289 }
1290
1291 return result;
1292 }
1293
1294 namespace expr
1295 {
1296
1297 struct value *
1298 rust_unop_ind_operation::evaluate (struct type *expect_type,
1299 struct expression *exp,
1300 enum noside noside)
1301 {
1302 if (noside != EVAL_NORMAL)
1303 return unop_ind_operation::evaluate (expect_type, exp, noside);
1304
1305 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1306 noside);
1307 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1308 if (trait_ptr != NULL)
1309 value = trait_ptr;
1310
1311 return value_ind (value);
1312 }
1313
1314 } /* namespace expr */
1315
1316 /* A helper function for UNOP_COMPLEMENT. */
1317
1318 struct value *
1319 eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1320 enum noside noside,
1321 enum exp_opcode opcode,
1322 struct value *value)
1323 {
1324 if (value_type (value)->code () == TYPE_CODE_BOOL)
1325 return value_from_longest (value_type (value), value_logical_not (value));
1326 return value_complement (value);
1327 }
1328
1329 /* A helper function for OP_ARRAY. */
1330
1331 struct value *
1332 eval_op_rust_array (struct type *expect_type, struct expression *exp,
1333 enum noside noside,
1334 enum exp_opcode opcode,
1335 struct value *elt, struct value *ncopies)
1336 {
1337 int copies = value_as_long (ncopies);
1338 if (copies < 0)
1339 error (_("Array with negative number of elements"));
1340
1341 if (noside == EVAL_NORMAL)
1342 {
1343 int i;
1344 std::vector<struct value *> eltvec (copies);
1345
1346 for (i = 0; i < copies; ++i)
1347 eltvec[i] = elt;
1348 return value_array (0, copies - 1, eltvec.data ());
1349 }
1350 else
1351 {
1352 struct type *arraytype
1353 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1354 return allocate_value (arraytype);
1355 }
1356 }
1357
1358 namespace expr
1359 {
1360
1361 struct value *
1362 rust_struct_anon::evaluate (struct type *expect_type,
1363 struct expression *exp,
1364 enum noside noside)
1365 {
1366 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1367 int field_number = std::get<0> (m_storage);
1368
1369 struct type *type = value_type (lhs);
1370
1371 if (type->code () == TYPE_CODE_STRUCT)
1372 {
1373 struct type *outer_type = NULL;
1374
1375 if (rust_enum_p (type))
1376 {
1377 type = resolve_dynamic_type (type, value_contents (lhs),
1378 value_address (lhs));
1379
1380 if (rust_empty_enum_p (type))
1381 error (_("Cannot access field %d of empty enum %s"),
1382 field_number, type->name ());
1383
1384 int fieldno = rust_enum_variant (type);
1385 lhs = value_primitive_field (lhs, 0, fieldno, type);
1386 outer_type = type;
1387 type = value_type (lhs);
1388 }
1389
1390 /* Tuples and tuple structs */
1391 int nfields = type->num_fields ();
1392
1393 if (field_number >= nfields || field_number < 0)
1394 {
1395 if (outer_type != NULL)
1396 error(_("Cannot access field %d of variant %s::%s, "
1397 "there are only %d fields"),
1398 field_number, outer_type->name (),
1399 rust_last_path_segment (type->name ()),
1400 nfields);
1401 else
1402 error(_("Cannot access field %d of %s, "
1403 "there are only %d fields"),
1404 field_number, type->name (), nfields);
1405 }
1406
1407 /* Tuples are tuple structs too. */
1408 if (!rust_tuple_struct_type_p (type))
1409 {
1410 if (outer_type != NULL)
1411 error(_("Variant %s::%s is not a tuple variant"),
1412 outer_type->name (),
1413 rust_last_path_segment (type->name ()));
1414 else
1415 error(_("Attempting to access anonymous field %d "
1416 "of %s, which is not a tuple, tuple struct, or "
1417 "tuple-like variant"),
1418 field_number, type->name ());
1419 }
1420
1421 return value_primitive_field (lhs, 0, field_number, type);
1422 }
1423 else
1424 error(_("Anonymous field access is only allowed on tuples, \
1425 tuple structs, and tuple-like enum variants"));
1426 }
1427
1428 struct value *
1429 rust_structop::evaluate (struct type *expect_type,
1430 struct expression *exp,
1431 enum noside noside)
1432 {
1433 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1434 const char *field_name = std::get<1> (m_storage).c_str ();
1435
1436 struct value *result;
1437 struct type *type = value_type (lhs);
1438 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1439 {
1440 type = resolve_dynamic_type (type, value_contents (lhs),
1441 value_address (lhs));
1442
1443 if (rust_empty_enum_p (type))
1444 error (_("Cannot access field %s of empty enum %s"),
1445 field_name, type->name ());
1446
1447 int fieldno = rust_enum_variant (type);
1448 lhs = value_primitive_field (lhs, 0, fieldno, type);
1449
1450 struct type *outer_type = type;
1451 type = value_type (lhs);
1452 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1453 error (_("Attempting to access named field %s of tuple "
1454 "variant %s::%s, which has only anonymous fields"),
1455 field_name, outer_type->name (),
1456 rust_last_path_segment (type->name ()));
1457
1458 try
1459 {
1460 result = value_struct_elt (&lhs, {}, field_name,
1461 NULL, "structure");
1462 }
1463 catch (const gdb_exception_error &except)
1464 {
1465 error (_("Could not find field %s of struct variant %s::%s"),
1466 field_name, outer_type->name (),
1467 rust_last_path_segment (type->name ()));
1468 }
1469 }
1470 else
1471 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1472 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1473 result = value_zero (value_type (result), VALUE_LVAL (result));
1474 return result;
1475 }
1476
1477 value *
1478 rust_aggregate_operation::evaluate (struct type *expect_type,
1479 struct expression *exp,
1480 enum noside noside)
1481 {
1482 struct type *type = std::get<0> (m_storage);
1483 CORE_ADDR addr = 0;
1484 struct value *addrval = NULL;
1485 value *result;
1486
1487 if (noside == EVAL_NORMAL)
1488 {
1489 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1490 addr = value_as_long (addrval);
1491 result = value_at_lazy (type, addr);
1492 }
1493
1494 if (std::get<1> (m_storage) != nullptr)
1495 {
1496 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1497 noside);
1498
1499 if (noside == EVAL_NORMAL)
1500 {
1501 /* This isn't quite right but will do for the time
1502 being, seeing that we can't implement the Copy
1503 trait anyway. */
1504 value_assign (result, init);
1505 }
1506 }
1507
1508 for (const auto &item : std::get<2> (m_storage))
1509 {
1510 value *val = item.second->evaluate (nullptr, exp, noside);
1511 if (noside == EVAL_NORMAL)
1512 {
1513 const char *fieldname = item.first.c_str ();
1514 value *field = value_struct_elt (&result, {}, fieldname,
1515 nullptr, "structure");
1516 value_assign (field, val);
1517 }
1518 }
1519
1520 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1521 result = allocate_value (type);
1522 else
1523 result = value_at_lazy (type, addr);
1524
1525 return result;
1526 }
1527
1528 value *
1529 rust_structop::evaluate_funcall (struct type *expect_type,
1530 struct expression *exp,
1531 enum noside noside,
1532 const std::vector<operation_up> &ops)
1533 {
1534 std::vector<struct value *> args (ops.size () + 1);
1535
1536 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1537 type in order to look up the method. */
1538 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1539 /* We don't yet implement real Deref semantics. */
1540 while (value_type (args[0])->code () == TYPE_CODE_PTR)
1541 args[0] = value_ind (args[0]);
1542
1543 struct type *type = value_type (args[0]);
1544 if ((type->code () != TYPE_CODE_STRUCT
1545 && type->code () != TYPE_CODE_UNION
1546 && type->code () != TYPE_CODE_ENUM)
1547 || rust_tuple_type_p (type))
1548 error (_("Method calls only supported on struct or enum types"));
1549 if (type->name () == NULL)
1550 error (_("Method call on nameless type"));
1551
1552 std::string name = (std::string (type->name ()) + "::"
1553 + std::get<1> (m_storage));
1554
1555 const struct block *block = get_selected_block (0);
1556 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1557 VAR_DOMAIN, NULL);
1558 if (sym.symbol == NULL)
1559 error (_("Could not find function named '%s'"), name.c_str ());
1560
1561 struct type *fn_type = sym.symbol->type ();
1562 if (fn_type->num_fields () == 0)
1563 error (_("Function '%s' takes no arguments"), name.c_str ());
1564
1565 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1566 args[0] = value_addr (args[0]);
1567
1568 value *function = address_of_variable (sym.symbol, block);
1569
1570 for (int i = 0; i < ops.size (); ++i)
1571 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1572
1573 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1574 return value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1575 return call_function_by_hand (function, NULL, args);
1576 }
1577
1578 }
1579
1580 \f
1581
1582 /* See language.h. */
1583
1584 void
1585 rust_language::language_arch_info (struct gdbarch *gdbarch,
1586 struct language_arch_info *lai) const
1587 {
1588 const struct builtin_type *builtin = builtin_type (gdbarch);
1589
1590 /* Helper function to allow shorter lines below. */
1591 auto add = [&] (struct type * t) -> struct type *
1592 {
1593 lai->add_primitive_type (t);
1594 return t;
1595 };
1596
1597 struct type *bool_type
1598 = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
1599 add (arch_character_type (gdbarch, 32, 1, "char"));
1600 add (arch_integer_type (gdbarch, 8, 0, "i8"));
1601 struct type *u8_type
1602 = add (arch_integer_type (gdbarch, 8, 1, "u8"));
1603 add (arch_integer_type (gdbarch, 16, 0, "i16"));
1604 add (arch_integer_type (gdbarch, 16, 1, "u16"));
1605 add (arch_integer_type (gdbarch, 32, 0, "i32"));
1606 add (arch_integer_type (gdbarch, 32, 1, "u32"));
1607 add (arch_integer_type (gdbarch, 64, 0, "i64"));
1608 add (arch_integer_type (gdbarch, 64, 1, "u64"));
1609
1610 unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1611 add (arch_integer_type (gdbarch, length, 0, "isize"));
1612 struct type *usize_type
1613 = add (arch_integer_type (gdbarch, length, 1, "usize"));
1614
1615 add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
1616 add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
1617 add (arch_integer_type (gdbarch, 0, 1, "()"));
1618
1619 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1620 add (rust_slice_type ("&str", tem, usize_type));
1621
1622 lai->set_bool_type (bool_type);
1623 lai->set_string_char_type (u8_type);
1624 }
1625
1626 /* See language.h. */
1627
1628 void
1629 rust_language::print_type (struct type *type, const char *varstring,
1630 struct ui_file *stream, int show, int level,
1631 const struct type_print_options *flags) const
1632 {
1633 print_offset_data podata (flags);
1634 rust_internal_print_type (type, varstring, stream, show, level,
1635 flags, false, &podata);
1636 }
1637
1638 /* See language.h. */
1639
1640 void
1641 rust_language::emitchar (int ch, struct type *chtype,
1642 struct ui_file *stream, int quoter) const
1643 {
1644 if (!rust_chartype_p (chtype))
1645 generic_emit_char (ch, chtype, stream, quoter,
1646 target_charset (chtype->arch ()));
1647 else if (ch == '\\' || ch == quoter)
1648 gdb_printf (stream, "\\%c", ch);
1649 else if (ch == '\n')
1650 gdb_puts ("\\n", stream);
1651 else if (ch == '\r')
1652 gdb_puts ("\\r", stream);
1653 else if (ch == '\t')
1654 gdb_puts ("\\t", stream);
1655 else if (ch == '\0')
1656 gdb_puts ("\\0", stream);
1657 else if (ch >= 32 && ch <= 127 && isprint (ch))
1658 gdb_putc (ch, stream);
1659 else if (ch <= 255)
1660 gdb_printf (stream, "\\x%02x", ch);
1661 else
1662 gdb_printf (stream, "\\u{%06x}", ch);
1663 }
1664
1665 /* See language.h. */
1666
1667 bool
1668 rust_language::is_string_type_p (struct type *type) const
1669 {
1670 LONGEST low_bound, high_bound;
1671
1672 type = check_typedef (type);
1673 return ((type->code () == TYPE_CODE_STRING)
1674 || (type->code () == TYPE_CODE_PTR
1675 && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
1676 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
1677 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
1678 &high_bound)))
1679 || (type->code () == TYPE_CODE_STRUCT
1680 && !rust_enum_p (type)
1681 && rust_slice_type_p (type)
1682 && strcmp (type->name (), "&str") == 0));
1683 }
1684
1685 /* Single instance of the Rust language class. */
1686
1687 static rust_language rust_language_defn;