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