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