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