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