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