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