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