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