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