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