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