]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rust-lang.c
Implement some Rust operations
[thirdparty/binutils-gdb.git] / gdb / rust-lang.c
CommitLineData
c44af4eb
TT
1/* Rust language support routines for GDB, the GNU debugger.
2
3666a048 3 Copyright (C) 2016-2021 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"
af30c400 41#include "parser-defs.h"
6fab4359 42#include "rust-exp.h"
c44af4eb 43
c9317f21 44/* See rust-lang.h. */
c44af4eb 45
c9317f21
TT
46const char *
47rust_last_path_segment (const char *path)
c44af4eb
TT
48{
49 const char *result = strrchr (path, ':');
50
51 if (result == NULL)
c9317f21 52 return path;
c44af4eb
TT
53 return result + 1;
54}
55
03c85b11 56/* See rust-lang.h. */
c44af4eb 57
03c85b11 58std::string
c44af4eb
TT
59rust_crate_for_block (const struct block *block)
60{
61 const char *scope = block_scope (block);
62
63 if (scope[0] == '\0')
03c85b11 64 return std::string ();
c44af4eb 65
03c85b11 66 return std::string (scope, cp_find_first_component (scope));
c44af4eb
TT
67}
68
c9317f21
TT
69/* Return true if TYPE, which must be a struct type, represents a Rust
70 enum. */
c44af4eb 71
b96645f1 72static bool
9c6a1327 73rust_enum_p (struct type *type)
b96645f1 74{
9c6a1327
TT
75 /* is_dynamic_type will return true if any field has a dynamic
76 attribute -- but we only want to check the top level. */
77 return TYPE_HAS_VARIANT_PARTS (type);
b96645f1
MG
78}
79
9c6a1327
TT
80/* Return true if TYPE, which must be an already-resolved enum type,
81 has no variants. */
098b2108
TT
82
83static bool
84rust_empty_enum_p (const struct type *type)
85{
1f704f76 86 return type->num_fields () == 0;
098b2108
TT
87}
88
9c6a1327
TT
89/* Given an already-resolved enum type and contents, find which
90 variant is active. */
c44af4eb 91
9c6a1327
TT
92static int
93rust_enum_variant (struct type *type)
c44af4eb 94{
9c6a1327 95 /* The active variant is simply the first non-artificial field. */
1f704f76 96 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
97 if (!TYPE_FIELD_ARTIFICIAL (type, i))
98 return i;
c44af4eb 99
9c6a1327
TT
100 /* Perhaps we could get here by trying to print an Ada variant
101 record in Rust mode. Unlikely, but an error is safer than an
102 assert. */
103 error (_("Could not find active enum variant"));
c44af4eb
TT
104}
105
106/* See rust-lang.h. */
107
65c40c95 108bool
c44af4eb
TT
109rust_tuple_type_p (struct type *type)
110{
111 /* The current implementation is a bit of a hack, but there's
112 nothing else in the debuginfo to distinguish a tuple from a
113 struct. */
78134374 114 return (type->code () == TYPE_CODE_STRUCT
7d93a1e0
SM
115 && type->name () != NULL
116 && type->name ()[0] == '(');
c44af4eb
TT
117}
118
c44af4eb 119/* Return true if all non-static fields of a structlike type are in a
c9317f21 120 sequence like __0, __1, __2. */
c44af4eb 121
65c40c95 122static bool
c9317f21 123rust_underscore_fields (struct type *type)
c44af4eb
TT
124{
125 int i, field_number;
126
127 field_number = 0;
128
78134374 129 if (type->code () != TYPE_CODE_STRUCT)
65c40c95 130 return false;
1f704f76 131 for (i = 0; i < type->num_fields (); ++i)
c44af4eb 132 {
ceacbf6e 133 if (!field_is_static (&type->field (i)))
c44af4eb 134 {
c9317f21 135 char buf[20];
c44af4eb 136
c9317f21
TT
137 xsnprintf (buf, sizeof (buf), "__%d", field_number);
138 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
139 return false;
140 field_number++;
c44af4eb
TT
141 }
142 }
65c40c95 143 return true;
c44af4eb
TT
144}
145
146/* See rust-lang.h. */
147
65c40c95 148bool
c44af4eb
TT
149rust_tuple_struct_type_p (struct type *type)
150{
12df5c00
TT
151 /* This is just an approximation until DWARF can represent Rust more
152 precisely. We exclude zero-length structs because they may not
153 be tuple structs, and there's no way to tell. */
1f704f76 154 return type->num_fields () > 0 && rust_underscore_fields (type);
c44af4eb
TT
155}
156
157/* Return true if TYPE is a slice type, otherwise false. */
158
65c40c95 159static bool
c44af4eb
TT
160rust_slice_type_p (struct type *type)
161{
78134374 162 return (type->code () == TYPE_CODE_STRUCT
7d93a1e0
SM
163 && type->name () != NULL
164 && (strncmp (type->name (), "&[", 2) == 0
165 || strcmp (type->name (), "&str") == 0));
c44af4eb
TT
166}
167
168/* Return true if TYPE is a range type, otherwise false. */
169
65c40c95 170static bool
c44af4eb
TT
171rust_range_type_p (struct type *type)
172{
173 int i;
174
78134374 175 if (type->code () != TYPE_CODE_STRUCT
1f704f76 176 || type->num_fields () > 2
7d93a1e0
SM
177 || type->name () == NULL
178 || strstr (type->name (), "::Range") == NULL)
65c40c95 179 return false;
c44af4eb 180
1f704f76 181 if (type->num_fields () == 0)
65c40c95 182 return true;
c44af4eb
TT
183
184 i = 0;
185 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
186 {
1f704f76 187 if (type->num_fields () == 1)
65c40c95 188 return true;
c44af4eb
TT
189 i = 1;
190 }
1f704f76 191 else if (type->num_fields () == 2)
c44af4eb
TT
192 {
193 /* First field had to be "start". */
65c40c95 194 return false;
c44af4eb
TT
195 }
196
197 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
198}
199
6873858b
TT
200/* Return true if TYPE is an inclusive range type, otherwise false.
201 This is only valid for types which are already known to be range
202 types. */
203
204static bool
205rust_inclusive_range_type_p (struct type *type)
206{
7d93a1e0
SM
207 return (strstr (type->name (), "::RangeInclusive") != NULL
208 || strstr (type->name (), "::RangeToInclusive") != NULL);
6873858b
TT
209}
210
c44af4eb
TT
211/* Return true if TYPE seems to be the type "u8", otherwise false. */
212
65c40c95 213static bool
c44af4eb
TT
214rust_u8_type_p (struct type *type)
215{
78134374 216 return (type->code () == TYPE_CODE_INT
c6d940a9 217 && type->is_unsigned ()
c44af4eb
TT
218 && TYPE_LENGTH (type) == 1);
219}
220
221/* Return true if TYPE is a Rust character type. */
222
65c40c95 223static bool
c44af4eb
TT
224rust_chartype_p (struct type *type)
225{
78134374 226 return (type->code () == TYPE_CODE_CHAR
c44af4eb 227 && TYPE_LENGTH (type) == 4
c6d940a9 228 && type->is_unsigned ());
c44af4eb
TT
229}
230
71a3c369
TT
231/* If VALUE represents a trait object pointer, return the underlying
232 pointer with the correct (i.e., runtime) type. Otherwise, return
233 NULL. */
234
235static struct value *
236rust_get_trait_object_pointer (struct value *value)
237{
238 struct type *type = check_typedef (value_type (value));
239
1f704f76 240 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
71a3c369
TT
241 return NULL;
242
243 /* Try to be a bit resilient if the ABI changes. */
244 int vtable_field = 0;
245 for (int i = 0; i < 2; ++i)
246 {
247 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
248 vtable_field = i;
249 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
250 return NULL;
251 }
252
253 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
254 struct symbol *symbol = find_symbol_at_address (vtable);
cf724bc9 255 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
71a3c369
TT
256 return NULL;
257
258 struct rust_vtable_symbol *vtable_sym
259 = static_cast<struct rust_vtable_symbol *> (symbol);
260 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
261 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
262}
263
c44af4eb
TT
264\f
265
1c485265 266/* See language.h. */
c44af4eb 267
1c485265
AB
268void
269rust_language::printstr (struct ui_file *stream, struct type *type,
270 const gdb_byte *string, unsigned int length,
271 const char *user_encoding, int force_ellipses,
272 const struct value_print_options *options) const
c44af4eb
TT
273{
274 /* Rust always uses UTF-8, but let the caller override this if need
275 be. */
276 const char *encoding = user_encoding;
277 if (user_encoding == NULL || !*user_encoding)
278 {
279 /* In Rust strings, characters are "u8". */
280 if (rust_u8_type_p (type))
281 encoding = "UTF-8";
282 else
283 {
284 /* This is probably some C string, so let's let C deal with
285 it. */
286 c_printstr (stream, type, string, length, user_encoding,
287 force_ellipses, options);
288 return;
289 }
290 }
291
292 /* This is not ideal as it doesn't use our character printer. */
293 generic_printstr (stream, type, string, length, encoding, force_ellipses,
294 '"', 0, options);
295}
296
297\f
298
45320ffa
TT
299/* Helper function to print a string slice. */
300
301static void
302rust_val_print_str (struct ui_file *stream, struct value *val,
303 const struct value_print_options *options)
304{
305 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
306 "slice");
307 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
308
309 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
310 value_as_address (base), value_as_long (len), stream,
311 options);
312}
313
1c485265 314/* See rust-lang.h. */
b96645f1 315
1c485265
AB
316void
317rust_language::val_print_struct
318 (struct value *val, struct ui_file *stream, int recurse,
319 const struct value_print_options *options) const
b96645f1
MG
320{
321 int i;
322 int first_field;
5f56f7cb 323 struct type *type = check_typedef (value_type (val));
45320ffa 324
7d93a1e0 325 if (rust_slice_type_p (type) && strcmp (type->name (), "&str") == 0)
45320ffa 326 {
80062eb9
AB
327 /* If what we are printing here is actually a string within a
328 structure then VAL will be the original parent value, while TYPE
329 will be the type of the structure representing the string we want
330 to print.
331 However, RUST_VAL_PRINT_STR looks up the fields of the string
332 inside VAL, assuming that VAL is the string.
333 So, recreate VAL as a value representing just the string. */
5f56f7cb 334 val = value_at_lazy (type, value_address (val));
45320ffa
TT
335 rust_val_print_str (stream, val, options);
336 return;
337 }
338
65c40c95
TT
339 bool is_tuple = rust_tuple_type_p (type);
340 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
b96645f1
MG
341 struct value_print_options opts;
342
343 if (!is_tuple)
344 {
7d93a1e0 345 if (type->name () != NULL)
dda83cd7 346 fprintf_filtered (stream, "%s", type->name ());
b96645f1 347
1f704f76 348 if (type->num_fields () == 0)
dda83cd7 349 return;
b96645f1 350
7d93a1e0 351 if (type->name () != NULL)
dda83cd7 352 fputs_filtered (" ", stream);
b96645f1
MG
353 }
354
355 if (is_tuple || is_tuple_struct)
356 fputs_filtered ("(", stream);
357 else
358 fputs_filtered ("{", stream);
359
360 opts = *options;
361 opts.deref_ref = 0;
362
363 first_field = 1;
1f704f76 364 for (i = 0; i < type->num_fields (); ++i)
b96645f1 365 {
ceacbf6e 366 if (field_is_static (&type->field (i)))
dda83cd7 367 continue;
b96645f1
MG
368
369 if (!first_field)
dda83cd7 370 fputs_filtered (",", stream);
b96645f1
MG
371
372 if (options->prettyformat)
dda83cd7 373 {
b50f188d
TT
374 fputs_filtered ("\n", stream);
375 print_spaces_filtered (2 + 2 * recurse, stream);
dda83cd7 376 }
b96645f1 377 else if (!first_field)
dda83cd7 378 fputs_filtered (" ", stream);
b96645f1
MG
379
380 first_field = 0;
381
382 if (!is_tuple && !is_tuple_struct)
dda83cd7 383 {
3f0cbb04
TT
384 fputs_styled (TYPE_FIELD_NAME (type, i),
385 variable_name_style.style (), stream);
b50f188d 386 fputs_filtered (": ", stream);
dda83cd7 387 }
b96645f1 388
1c485265 389 value_print_inner (value_field (val, i), stream, recurse + 1, &opts);
b96645f1
MG
390 }
391
392 if (options->prettyformat)
393 {
394 fputs_filtered ("\n", stream);
395 print_spaces_filtered (2 * recurse, stream);
396 }
397
398 if (is_tuple || is_tuple_struct)
399 fputs_filtered (")", stream);
400 else
401 fputs_filtered ("}", stream);
402}
403
1c485265 404/* See rust-lang.h. */
c9317f21 405
1c485265
AB
406void
407rust_language::print_enum (struct value *val, struct ui_file *stream,
408 int recurse,
409 const struct value_print_options *options) const
c9317f21
TT
410{
411 struct value_print_options opts = *options;
5f56f7cb 412 struct type *type = check_typedef (value_type (val));
c9317f21
TT
413
414 opts.deref_ref = 0;
415
9c6a1327
TT
416 gdb_assert (rust_enum_p (type));
417 gdb::array_view<const gdb_byte> view (value_contents_for_printing (val),
418 TYPE_LENGTH (value_type (val)));
419 type = resolve_dynamic_type (type, view, value_address (val));
420
098b2108
TT
421 if (rust_empty_enum_p (type))
422 {
423 /* Print the enum type name here to be more clear. */
7f6aba03 424 fprintf_filtered (stream, _("%s {%p[<No data fields>%p]}"),
7d93a1e0 425 type->name (),
7f6aba03 426 metadata_style.style ().ptr (), nullptr);
098b2108
TT
427 return;
428 }
429
9c6a1327
TT
430 int variant_fieldno = rust_enum_variant (type);
431 val = value_field (val, variant_fieldno);
940da03e 432 struct type *variant_type = type->field (variant_fieldno).type ();
c9317f21 433
1f704f76 434 int nfields = variant_type->num_fields ();
c9317f21
TT
435
436 bool is_tuple = rust_tuple_struct_type_p (variant_type);
437
7d93a1e0 438 fprintf_filtered (stream, "%s", variant_type->name ());
c9317f21
TT
439 if (nfields == 0)
440 {
441 /* In case of a nullary variant like 'None', just output
442 the name. */
443 return;
444 }
445
446 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
447 if (is_tuple)
448 fprintf_filtered (stream, "(");
449 else
450 {
451 /* struct variant. */
452 fprintf_filtered (stream, "{");
453 }
454
455 bool first_field = true;
1f704f76 456 for (int j = 0; j < variant_type->num_fields (); j++)
c9317f21
TT
457 {
458 if (!first_field)
459 fputs_filtered (", ", stream);
460 first_field = false;
461
462 if (!is_tuple)
3f0cbb04
TT
463 fprintf_filtered (stream, "%ps: ",
464 styled_string (variable_name_style.style (),
465 TYPE_FIELD_NAME (variant_type, j)));
c9317f21 466
1c485265 467 value_print_inner (value_field (val, j), stream, recurse + 1, &opts);
c9317f21
TT
468 }
469
470 if (is_tuple)
471 fputs_filtered (")", stream);
472 else
473 fputs_filtered ("}", stream);
474}
475
c44af4eb
TT
476static const struct generic_val_print_decorations rust_decorations =
477{
478 /* Complex isn't used in Rust, but we provide C-ish values just in
479 case. */
480 "",
481 " + ",
482 " * I",
483 "true",
484 "false",
921d8f54 485 "()",
c44af4eb
TT
486 "[",
487 "]"
488};
489
1c485265
AB
490/* See language.h. */
491
492void
493rust_language::value_print_inner
494 (struct value *val, struct ui_file *stream, int recurse,
495 const struct value_print_options *options) const
5f56f7cb
TT
496{
497 struct value_print_options opts = *options;
498 opts.deref_ref = 1;
499
500 if (opts.prettyformat == Val_prettyformat_default)
501 opts.prettyformat = (opts.prettyformat_structs
502 ? Val_prettyformat : Val_no_prettyformat);
503
504 struct type *type = check_typedef (value_type (val));
78134374 505 switch (type->code ())
c44af4eb
TT
506 {
507 case TYPE_CODE_PTR:
508 {
509 LONGEST low_bound, high_bound;
510
78134374 511 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
c44af4eb
TT
512 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
513 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
5f56f7cb
TT
514 &high_bound))
515 {
516 /* We have a pointer to a byte string, so just print
517 that. */
518 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
519 CORE_ADDR addr = value_as_address (val);
8ee511af 520 struct gdbarch *arch = type->arch ();
c44af4eb 521
5f56f7cb
TT
522 if (opts.addressprint)
523 {
524 fputs_filtered (paddress (arch, addr), stream);
525 fputs_filtered (" ", stream);
526 }
c44af4eb 527
5f56f7cb
TT
528 fputs_filtered ("b", stream);
529 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
530 high_bound - low_bound + 1, stream,
531 &opts);
532 break;
533 }
c44af4eb 534 }
5f56f7cb 535 goto generic_print;
c44af4eb 536
c44af4eb
TT
537 case TYPE_CODE_INT:
538 /* Recognize the unit type. */
c6d940a9 539 if (type->is_unsigned () && TYPE_LENGTH (type) == 0
7d93a1e0 540 && type->name () != NULL && strcmp (type->name (), "()") == 0)
c44af4eb
TT
541 {
542 fputs_filtered ("()", stream);
543 break;
544 }
545 goto generic_print;
546
547 case TYPE_CODE_STRING:
548 {
c44af4eb
TT
549 LONGEST low_bound, high_bound;
550
551 if (!get_array_bounds (type, &low_bound, &high_bound))
552 error (_("Could not determine the array bounds"));
553
554 /* If we see a plain TYPE_CODE_STRING, then we're printing a
555 byte string, hence the choice of "ASCII" as the
556 encoding. */
557 fputs_filtered ("b", stream);
1c485265
AB
558 printstr (stream, TYPE_TARGET_TYPE (type),
559 value_contents_for_printing (val),
560 high_bound - low_bound + 1, "ASCII", 0, &opts);
c44af4eb
TT
561 }
562 break;
563
564 case TYPE_CODE_ARRAY:
565 {
566 LONGEST low_bound, high_bound;
567
568 if (get_array_bounds (type, &low_bound, &high_bound)
569 && high_bound - low_bound + 1 == 0)
570 fputs_filtered ("[]", stream);
571 else
572 goto generic_print;
573 }
574 break;
575
576 case TYPE_CODE_UNION:
c9317f21
TT
577 /* Untagged unions are printed as if they are structs. Since
578 the field bit positions overlap in the debuginfo, the code
579 for printing a union is same as that for a struct, the only
580 difference is that the input type will have overlapping
581 fields. */
5f56f7cb 582 val_print_struct (val, stream, recurse, &opts);
c44af4eb
TT
583 break;
584
585 case TYPE_CODE_STRUCT:
c9317f21 586 if (rust_enum_p (type))
1c485265 587 print_enum (val, stream, recurse, &opts);
c9317f21 588 else
5f56f7cb 589 val_print_struct (val, stream, recurse, &opts);
b96645f1 590 break;
c44af4eb 591
b96645f1
MG
592 default:
593 generic_print:
594 /* Nothing special yet. */
5f56f7cb 595 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
b96645f1
MG
596 }
597}
c44af4eb 598
b96645f1 599\f
c44af4eb 600
b96645f1 601static void
c9317f21
TT
602rust_internal_print_type (struct type *type, const char *varstring,
603 struct ui_file *stream, int show, int level,
604 const struct type_print_options *flags,
a33ccfc7 605 bool for_rust_enum, print_offset_data *podata);
b96645f1
MG
606
607/* Print a struct or union typedef. */
608static void
609rust_print_struct_def (struct type *type, const char *varstring,
b50f188d 610 struct ui_file *stream, int show, int level,
c9317f21 611 const struct type_print_options *flags,
a33ccfc7 612 bool for_rust_enum, print_offset_data *podata)
b96645f1 613{
b50f188d
TT
614 /* Print a tuple type simply. */
615 if (rust_tuple_type_p (type))
616 {
7d93a1e0 617 fputs_filtered (type->name (), stream);
b50f188d
TT
618 return;
619 }
c44af4eb 620
b50f188d
TT
621 /* If we see a base class, delegate to C. */
622 if (TYPE_N_BASECLASSES (type) > 0)
623 c_print_type (type, varstring, stream, show, level, flags);
c44af4eb 624
a33ccfc7
TT
625 if (flags->print_offsets)
626 {
627 /* Temporarily bump the level so that the output lines up
628 correctly. */
629 level += 2;
630 }
631
c9317f21
TT
632 /* Compute properties of TYPE here because, in the enum case, the
633 rest of the code ends up looking only at the variant part. */
7d93a1e0 634 const char *tagname = type->name ();
c9317f21
TT
635 bool is_tuple_struct = rust_tuple_struct_type_p (type);
636 bool is_tuple = rust_tuple_type_p (type);
637 bool is_enum = rust_enum_p (type);
b96645f1 638
c9317f21
TT
639 if (for_rust_enum)
640 {
641 /* Already printing an outer enum, so nothing to print here. */
642 }
643 else
644 {
645 /* This code path is also used by unions and enums. */
646 if (is_enum)
647 {
648 fputs_filtered ("enum ", stream);
24e99c6c 649 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
8c2e4e06
SM
650 if (prop != nullptr && prop->kind () == PROP_TYPE)
651 type = prop->original_type ();
c9317f21 652 }
78134374 653 else if (type->code () == TYPE_CODE_STRUCT)
c9317f21
TT
654 fputs_filtered ("struct ", stream);
655 else
656 fputs_filtered ("union ", stream);
b96645f1 657
c9317f21
TT
658 if (tagname != NULL)
659 fputs_filtered (tagname, stream);
660 }
b96645f1 661
1f704f76 662 if (type->num_fields () == 0 && !is_tuple)
b50f188d 663 return;
a33ccfc7 664 if (for_rust_enum && !flags->print_offsets)
c9317f21
TT
665 fputs_filtered (is_tuple_struct ? "(" : "{", stream);
666 else
667 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
c44af4eb 668
a33ccfc7
TT
669 /* When printing offsets, we rearrange the fields into storage
670 order. This lets us show holes more clearly. We work using
671 field indices here because it simplifies calls to
672 print_offset_data::update below. */
673 std::vector<int> fields;
1f704f76 674 for (int i = 0; i < type->num_fields (); ++i)
b50f188d 675 {
ceacbf6e 676 if (field_is_static (&type->field (i)))
b50f188d 677 continue;
9c6a1327 678 if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
a33ccfc7
TT
679 continue;
680 fields.push_back (i);
681 }
682 if (flags->print_offsets)
683 std::sort (fields.begin (), fields.end (),
684 [&] (int a, int b)
685 {
686 return (TYPE_FIELD_BITPOS (type, a)
687 < TYPE_FIELD_BITPOS (type, b));
688 });
689
690 for (int i : fields)
691 {
692 QUIT;
693
ceacbf6e 694 gdb_assert (!field_is_static (&type->field (i)));
9c6a1327 695 gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
a33ccfc7
TT
696
697 if (flags->print_offsets)
698 podata->update (type, i, stream);
b50f188d
TT
699
700 /* We'd like to print "pub" here as needed, but rustc
701 doesn't emit the debuginfo, and our types don't have
702 cplus_struct_type attached. */
703
704 /* For a tuple struct we print the type but nothing
705 else. */
a33ccfc7 706 if (!for_rust_enum || flags->print_offsets)
c9317f21
TT
707 print_spaces_filtered (level + 2, stream);
708 if (is_enum)
3f0cbb04
TT
709 fputs_styled (TYPE_FIELD_NAME (type, i), variable_name_style.style (),
710 stream);
c9317f21 711 else if (!is_tuple_struct)
3f0cbb04
TT
712 fprintf_filtered (stream, "%ps: ",
713 styled_string (variable_name_style.style (),
714 TYPE_FIELD_NAME (type, i)));
b50f188d 715
940da03e 716 rust_internal_print_type (type->field (i).type (), NULL,
53d7df28 717 stream, (is_enum ? show : show - 1),
a33ccfc7
TT
718 level + 2, flags, is_enum, podata);
719 if (!for_rust_enum || flags->print_offsets)
c9317f21 720 fputs_filtered (",\n", stream);
a33ccfc7
TT
721 /* Note that this check of "I" is ok because we only sorted the
722 fields by offset when print_offsets was set, so we won't take
723 this branch in that case. */
1f704f76 724 else if (i + 1 < type->num_fields ())
c9317f21 725 fputs_filtered (", ", stream);
b50f188d 726 }
c44af4eb 727
a33ccfc7
TT
728 if (flags->print_offsets)
729 {
730 /* Undo the temporary level increase we did above. */
731 level -= 2;
732 podata->finish (type, level, stream);
733 print_spaces_filtered (print_offset_data::indentation, stream);
734 if (level == 0)
735 print_spaces_filtered (2, stream);
736 }
737 if (!for_rust_enum || flags->print_offsets)
c9317f21
TT
738 print_spaces_filtered (level, stream);
739 fputs_filtered (is_tuple_struct ? ")" : "}", stream);
c44af4eb
TT
740}
741
c44af4eb
TT
742/* la_print_type implementation for Rust. */
743
744static void
c9317f21
TT
745rust_internal_print_type (struct type *type, const char *varstring,
746 struct ui_file *stream, int show, int level,
747 const struct type_print_options *flags,
a33ccfc7 748 bool for_rust_enum, print_offset_data *podata)
c44af4eb 749{
c44af4eb
TT
750 QUIT;
751 if (show <= 0
7d93a1e0 752 && type->name () != NULL)
c44af4eb 753 {
921d8f54 754 /* Rust calls the unit type "void" in its debuginfo,
dda83cd7 755 but we don't want to print it as that. */
78134374 756 if (type->code () == TYPE_CODE_VOID)
dda83cd7 757 fputs_filtered ("()", stream);
921d8f54 758 else
dda83cd7 759 fputs_filtered (type->name (), stream);
c44af4eb
TT
760 return;
761 }
762
763 type = check_typedef (type);
78134374 764 switch (type->code ())
c44af4eb 765 {
921d8f54 766 case TYPE_CODE_VOID:
c9317f21
TT
767 /* If we have an enum, we've already printed the type's
768 unqualified name, and there is nothing else to print
769 here. */
770 if (!for_rust_enum)
771 fputs_filtered ("()", stream);
921d8f54
MG
772 break;
773
c44af4eb
TT
774 case TYPE_CODE_FUNC:
775 /* Delegate varargs to the C printer. */
a409645d 776 if (type->has_varargs ())
c44af4eb
TT
777 goto c_printer;
778
779 fputs_filtered ("fn ", stream);
780 if (varstring != NULL)
781 fputs_filtered (varstring, stream);
782 fputs_filtered ("(", stream);
1f704f76 783 for (int i = 0; i < type->num_fields (); ++i)
c44af4eb
TT
784 {
785 QUIT;
786 if (i > 0)
787 fputs_filtered (", ", stream);
940da03e 788 rust_internal_print_type (type->field (i).type (), "", stream,
a33ccfc7 789 -1, 0, flags, false, podata);
c44af4eb 790 }
921d8f54
MG
791 fputs_filtered (")", stream);
792 /* If it returns unit, we can omit the return type. */
78134374 793 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
dda83cd7
SM
794 {
795 fputs_filtered (" -> ", stream);
796 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
a33ccfc7 797 -1, 0, flags, false, podata);
dda83cd7 798 }
c44af4eb
TT
799 break;
800
801 case TYPE_CODE_ARRAY:
802 {
803 LONGEST low_bound, high_bound;
804
805 fputs_filtered ("[", stream);
c9317f21 806 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
a33ccfc7
TT
807 stream, show - 1, level, flags, false,
808 podata);
c44af4eb 809
cf88be68
SM
810 if (type->bounds ()->high.kind () == PROP_LOCEXPR
811 || type->bounds ()->high.kind () == PROP_LOCLIST)
e6cf65f2 812 fprintf_filtered (stream, "; variable length");
c44af4eb 813 else if (get_array_bounds (type, &low_bound, &high_bound))
e6cf65f2 814 fprintf_filtered (stream, "; %s",
c44af4eb
TT
815 plongest (high_bound - low_bound + 1));
816 fputs_filtered ("]", stream);
817 }
818 break;
819
c9317f21 820 case TYPE_CODE_UNION:
c44af4eb 821 case TYPE_CODE_STRUCT:
c9317f21 822 rust_print_struct_def (type, varstring, stream, show, level, flags,
a33ccfc7 823 for_rust_enum, podata);
c44af4eb
TT
824 break;
825
826 case TYPE_CODE_ENUM:
827 {
b926417a 828 int len = 0;
c44af4eb
TT
829
830 fputs_filtered ("enum ", stream);
7d93a1e0 831 if (type->name () != NULL)
c44af4eb 832 {
7d93a1e0 833 fputs_filtered (type->name (), stream);
c44af4eb 834 fputs_filtered (" ", stream);
7d93a1e0 835 len = strlen (type->name ());
c44af4eb
TT
836 }
837 fputs_filtered ("{\n", stream);
838
1f704f76 839 for (int i = 0; i < type->num_fields (); ++i)
c44af4eb
TT
840 {
841 const char *name = TYPE_FIELD_NAME (type, i);
842
843 QUIT;
844
845 if (len > 0
7d93a1e0 846 && strncmp (name, type->name (), len) == 0
c44af4eb
TT
847 && name[len] == ':'
848 && name[len + 1] == ':')
849 name += len + 2;
32f47895
TT
850 fprintf_filtered (stream, "%*s%ps,\n",
851 level + 2, "",
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 *
d148f803
TT
1045rust_range (struct type *expect_type, struct expression *exp,
1046 enum noside noside, enum range_flag kind,
1047 struct value *low, struct value *high)
c44af4eb 1048{
c44af4eb
TT
1049 struct value *addrval, *result;
1050 CORE_ADDR addr;
1051 struct type *range_type;
1052 struct type *index_type;
1053 struct type *temp_type;
1054 const char *name;
1055
2f1b18db 1056 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
c44af4eb
TT
1057
1058 if (noside == EVAL_SKIP)
1059 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1060
1061 if (low == NULL)
1062 {
1063 if (high == NULL)
1064 {
1065 index_type = NULL;
1066 name = "std::ops::RangeFull";
1067 }
1068 else
1069 {
1070 index_type = value_type (high);
6873858b
TT
1071 name = (inclusive
1072 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
c44af4eb
TT
1073 }
1074 }
1075 else
1076 {
1077 if (high == NULL)
1078 {
1079 index_type = value_type (low);
1080 name = "std::ops::RangeFrom";
1081 }
1082 else
1083 {
1084 if (!types_equal (value_type (low), value_type (high)))
1085 error (_("Range expression with different types"));
1086 index_type = value_type (low);
6873858b 1087 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
c44af4eb
TT
1088 }
1089 }
1090
1091 /* If we don't have an index type, just allocate this on the
1092 arch. Here any type will do. */
1093 temp_type = (index_type == NULL
1094 ? language_bool_type (exp->language_defn, exp->gdbarch)
1095 : index_type);
1096 /* It would be nicer to cache the range type. */
1097 range_type = rust_composite_type (temp_type, name,
1098 low == NULL ? NULL : "start", index_type,
1099 high == NULL ? NULL : "end", index_type);
1100
1101 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1102 return value_zero (range_type, lval_memory);
1103
1104 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1105 addr = value_as_long (addrval);
1106 result = value_at_lazy (range_type, addr);
1107
1108 if (low != NULL)
1109 {
1110 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1111 "range");
1112
1113 value_assign (start, low);
1114 }
1115
1116 if (high != NULL)
1117 {
1118 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1119 "range");
1120
1121 value_assign (end, high);
1122 }
1123
1124 result = value_at_lazy (range_type, addr);
1125 return result;
1126}
1127
1128/* A helper function to compute the range and kind given a range
1129 value. TYPE is the type of the range value. RANGE is the range
1130 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1131 parameters might be filled in, or might not be, depending on the
1132 kind of range this is. KIND will always be set to the appropriate
1133 value describing the kind of range, and this can be used to
1134 determine whether LOW or HIGH are valid. */
1135
1136static void
1137rust_compute_range (struct type *type, struct value *range,
1138 LONGEST *low, LONGEST *high,
f2d8e4c5 1139 range_flags *kind)
c44af4eb
TT
1140{
1141 int i;
1142
1143 *low = 0;
1144 *high = 0;
2f1b18db 1145 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
c44af4eb 1146
1f704f76 1147 if (type->num_fields () == 0)
c44af4eb
TT
1148 return;
1149
1150 i = 0;
1151 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1152 {
2f1b18db 1153 *kind = RANGE_HIGH_BOUND_DEFAULT;
c44af4eb
TT
1154 *low = value_as_long (value_field (range, 0));
1155 ++i;
1156 }
1f704f76 1157 if (type->num_fields () > i
c44af4eb
TT
1158 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1159 {
2f1b18db
AB
1160 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1161 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
c44af4eb 1162 *high = value_as_long (value_field (range, i));
6873858b
TT
1163
1164 if (rust_inclusive_range_type_p (type))
1165 ++*high;
c44af4eb
TT
1166 }
1167}
1168
1169/* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1170
1171static struct value *
984af2cb
TT
1172rust_subscript (struct type *expect_type, struct expression *exp,
1173 enum noside noside, bool for_addr,
1174 struct value *lhs, struct value *rhs)
c44af4eb 1175{
984af2cb 1176 struct value *result;
c44af4eb 1177 struct type *rhstype;
45f4ed92 1178 LONGEST low, high_bound;
c44af4eb 1179 /* Initialized to appease the compiler. */
f2d8e4c5 1180 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
45f4ed92 1181 LONGEST high = 0;
c44af4eb
TT
1182 int want_slice = 0;
1183
c44af4eb
TT
1184 if (noside == EVAL_SKIP)
1185 return lhs;
1186
1187 rhstype = check_typedef (value_type (rhs));
1188 if (rust_range_type_p (rhstype))
1189 {
1190 if (!for_addr)
1191 error (_("Can't take slice of array without '&'"));
1192 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1193 want_slice = 1;
1194 }
1195 else
1196 low = value_as_long (rhs);
1197
b3e3859b 1198 struct type *type = check_typedef (value_type (lhs));
c44af4eb
TT
1199 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1200 {
b3e3859b 1201 struct type *base_type = nullptr;
78134374 1202 if (type->code () == TYPE_CODE_ARRAY)
b3e3859b
TT
1203 base_type = TYPE_TARGET_TYPE (type);
1204 else if (rust_slice_type_p (type))
1205 {
1f704f76 1206 for (int i = 0; i < type->num_fields (); ++i)
b3e3859b
TT
1207 {
1208 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1209 {
940da03e 1210 base_type = TYPE_TARGET_TYPE (type->field (i).type ());
b3e3859b
TT
1211 break;
1212 }
1213 }
1214 if (base_type == nullptr)
1215 error (_("Could not find 'data_ptr' in slice type"));
1216 }
78134374 1217 else if (type->code () == TYPE_CODE_PTR)
b3e3859b
TT
1218 base_type = TYPE_TARGET_TYPE (type);
1219 else
1220 error (_("Cannot subscript non-array type"));
1221
1222 struct type *new_type;
1223 if (want_slice)
1224 {
1225 if (rust_slice_type_p (type))
1226 new_type = type;
1227 else
1228 {
1229 struct type *usize
1230 = language_lookup_primitive_type (exp->language_defn,
1231 exp->gdbarch,
1232 "usize");
1233 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1234 }
1235 }
1236 else
1237 new_type = base_type;
c44af4eb 1238
b3e3859b 1239 return value_zero (new_type, VALUE_LVAL (lhs));
c44af4eb
TT
1240 }
1241 else
1242 {
1243 LONGEST low_bound;
1244 struct value *base;
c44af4eb 1245
78134374 1246 if (type->code () == TYPE_CODE_ARRAY)
c44af4eb
TT
1247 {
1248 base = lhs;
1249 if (!get_array_bounds (type, &low_bound, &high_bound))
1250 error (_("Can't compute array bounds"));
1251 if (low_bound != 0)
1252 error (_("Found array with non-zero lower bound"));
1253 ++high_bound;
1254 }
1255 else if (rust_slice_type_p (type))
1256 {
1257 struct value *len;
1258
1259 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1260 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1261 low_bound = 0;
1262 high_bound = value_as_long (len);
1263 }
78134374 1264 else if (type->code () == TYPE_CODE_PTR)
42d94011
MG
1265 {
1266 base = lhs;
1267 low_bound = 0;
1268 high_bound = LONGEST_MAX;
1269 }
c44af4eb
TT
1270 else
1271 error (_("Cannot subscript non-array type"));
1272
2f1b18db 1273 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
c44af4eb
TT
1274 low = low_bound;
1275 if (low < 0)
1276 error (_("Index less than zero"));
1277 if (low > high_bound)
1278 error (_("Index greater than length"));
1279
1280 result = value_subscript (base, low);
1281 }
1282
1283 if (for_addr)
1284 {
1285 if (want_slice)
1286 {
1287 struct type *usize, *slice;
1288 CORE_ADDR addr;
1289 struct value *addrval, *tem;
1290
2f1b18db 1291 if (kind & RANGE_HIGH_BOUND_DEFAULT)
c44af4eb
TT
1292 high = high_bound;
1293 if (high < 0)
1294 error (_("High index less than zero"));
1295 if (low > high)
1296 error (_("Low index greater than high index"));
1297 if (high > high_bound)
1298 error (_("High index greater than length"));
1299
1300 usize = language_lookup_primitive_type (exp->language_defn,
1301 exp->gdbarch,
1302 "usize");
45320ffa
TT
1303 const char *new_name = ((type != nullptr
1304 && rust_slice_type_p (type))
7d93a1e0 1305 ? type->name () : "&[*gdb*]");
45320ffa
TT
1306
1307 slice = rust_slice_type (new_name, value_type (result), usize);
c44af4eb
TT
1308
1309 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1310 addr = value_as_long (addrval);
1311 tem = value_at_lazy (slice, addr);
1312
1313 value_assign (value_field (tem, 0), value_addr (result));
1314 value_assign (value_field (tem, 1),
1315 value_from_longest (usize, high - low));
1316
1317 result = value_at_lazy (slice, addr);
1318 }
1319 else
1320 result = value_addr (result);
1321 }
1322
1323 return result;
1324}
1325
d123f9e4
TT
1326/* A helper function for UNOP_IND. */
1327
1328static struct value *
1329eval_op_rust_ind (struct type *expect_type, struct expression *exp,
1330 enum noside noside,
1331 struct value *value)
1332{
1333 gdb_assert (noside == EVAL_NORMAL);
1334 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1335 if (trait_ptr != NULL)
1336 value = trait_ptr;
1337
1338 return value_ind (value);
1339}
1340
6fa9831f
TT
1341/* A helper function for UNOP_COMPLEMENT. */
1342
6fab4359 1343struct value *
6fa9831f
TT
1344eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1345 enum noside noside,
6fab4359 1346 enum exp_opcode opcode,
6fa9831f
TT
1347 struct value *value)
1348{
1349 if (noside == EVAL_SKIP)
1350 {
1351 /* Preserving the type is enough. */
1352 return value;
1353 }
1354 if (value_type (value)->code () == TYPE_CODE_BOOL)
1355 return value_from_longest (value_type (value), value_logical_not (value));
1356 return value_complement (value);
1357}
1358
05104233
TT
1359/* A helper function for OP_ARRAY. */
1360
6fab4359 1361struct value *
05104233
TT
1362eval_op_rust_array (struct type *expect_type, struct expression *exp,
1363 enum noside noside,
6fab4359 1364 enum exp_opcode opcode,
05104233
TT
1365 struct value *elt, struct value *ncopies)
1366{
1367 int copies = value_as_long (ncopies);
1368 if (copies < 0)
1369 error (_("Array with negative number of elements"));
1370
1371 if (noside == EVAL_NORMAL)
1372 {
1373 int i;
1374 std::vector<struct value *> eltvec (copies);
1375
1376 for (i = 0; i < copies; ++i)
1377 eltvec[i] = elt;
1378 return value_array (0, copies - 1, eltvec.data ());
1379 }
1380 else
1381 {
1382 struct type *arraytype
1383 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1384 return allocate_value (arraytype);
1385 }
1386}
1387
575cae23
TT
1388/* A helper function for STRUCTOP_ANONYMOUS. */
1389
1390static struct value *
1391eval_op_rust_struct_anon (struct type *expect_type, struct expression *exp,
1392 enum noside noside,
1393 int field_number, struct value *lhs)
1394{
1395 struct type *type = value_type (lhs);
1396
1397 if (type->code () == TYPE_CODE_STRUCT)
1398 {
1399 struct type *outer_type = NULL;
1400
1401 if (rust_enum_p (type))
1402 {
1403 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1404 TYPE_LENGTH (type));
1405 type = resolve_dynamic_type (type, view, value_address (lhs));
1406
1407 if (rust_empty_enum_p (type))
1408 error (_("Cannot access field %d of empty enum %s"),
1409 field_number, type->name ());
1410
1411 int fieldno = rust_enum_variant (type);
1412 lhs = value_primitive_field (lhs, 0, fieldno, type);
1413 outer_type = type;
1414 type = value_type (lhs);
1415 }
1416
1417 /* Tuples and tuple structs */
1418 int nfields = type->num_fields ();
1419
1420 if (field_number >= nfields || field_number < 0)
1421 {
1422 if (outer_type != NULL)
1423 error(_("Cannot access field %d of variant %s::%s, "
1424 "there are only %d fields"),
1425 field_number, outer_type->name (),
1426 rust_last_path_segment (type->name ()),
1427 nfields);
1428 else
1429 error(_("Cannot access field %d of %s, "
1430 "there are only %d fields"),
1431 field_number, type->name (), nfields);
1432 }
1433
1434 /* Tuples are tuple structs too. */
1435 if (!rust_tuple_struct_type_p (type))
1436 {
1437 if (outer_type != NULL)
1438 error(_("Variant %s::%s is not a tuple variant"),
1439 outer_type->name (),
1440 rust_last_path_segment (type->name ()));
1441 else
1442 error(_("Attempting to access anonymous field %d "
1443 "of %s, which is not a tuple, tuple struct, or "
1444 "tuple-like variant"),
1445 field_number, type->name ());
1446 }
1447
1448 return value_primitive_field (lhs, 0, field_number, type);
1449 }
1450 else
1451 error(_("Anonymous field access is only allowed on tuples, \
1452tuple structs, and tuple-like enum variants"));
1453}
1454
1fa41fc7
TT
1455/* A helper function for STRUCTOP_STRUCT. */
1456
1457static struct value *
1458eval_op_rust_structop (struct type *expect_type, struct expression *exp,
1459 enum noside noside,
1460 struct value *lhs, const char *field_name)
1461{
1462 struct value *result;
1463 struct type *type = value_type (lhs);
1464 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1465 {
1466 gdb::array_view<const gdb_byte> view (value_contents (lhs),
1467 TYPE_LENGTH (type));
1468 type = resolve_dynamic_type (type, view, value_address (lhs));
1469
1470 if (rust_empty_enum_p (type))
1471 error (_("Cannot access field %s of empty enum %s"),
1472 field_name, type->name ());
1473
1474 int fieldno = rust_enum_variant (type);
1475 lhs = value_primitive_field (lhs, 0, fieldno, type);
1476
1477 struct type *outer_type = type;
1478 type = value_type (lhs);
1479 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1480 error (_("Attempting to access named field %s of tuple "
1481 "variant %s::%s, which has only anonymous fields"),
1482 field_name, outer_type->name (),
1483 rust_last_path_segment (type->name ()));
1484
1485 try
1486 {
1487 result = value_struct_elt (&lhs, NULL, field_name,
1488 NULL, "structure");
1489 }
1490 catch (const gdb_exception_error &except)
1491 {
1492 error (_("Could not find field %s of struct variant %s::%s"),
1493 field_name, outer_type->name (),
1494 rust_last_path_segment (type->name ()));
1495 }
1496 }
1497 else
1498 result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
1499 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1500 result = value_zero (value_type (result), VALUE_LVAL (result));
1501 return result;
1502}
1503
c44af4eb
TT
1504/* evaluate_exp implementation for Rust. */
1505
1506static struct value *
1507rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1508 int *pos, enum noside noside)
1509{
1510 struct value *result;
6fab4359 1511 enum exp_opcode op = exp->elts[*pos].opcode;
c44af4eb 1512
6fab4359 1513 switch (op)
c44af4eb 1514 {
71a3c369
TT
1515 case UNOP_IND:
1516 {
1517 if (noside != EVAL_NORMAL)
1518 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1519 else
1520 {
1521 ++*pos;
1522 struct value *value = evaluate_subexp (expect_type, exp, pos,
1523 noside);
d123f9e4 1524 result = eval_op_rust_ind (expect_type, exp, noside, value);
71a3c369
TT
1525 }
1526 }
1527 break;
1528
c44af4eb
TT
1529 case UNOP_COMPLEMENT:
1530 {
1531 struct value *value;
1532
1533 ++*pos;
fe1fe7ea 1534 value = evaluate_subexp (nullptr, exp, pos, noside);
6fab4359 1535 result = eval_op_rust_complement (expect_type, exp, noside, op, value);
c44af4eb
TT
1536 }
1537 break;
1538
1539 case BINOP_SUBSCRIPT:
984af2cb
TT
1540 {
1541 ++*pos;
1542 struct value *lhs = evaluate_subexp (nullptr, exp, pos, noside);
1543 struct value *rhs = evaluate_subexp (nullptr, exp, pos, noside);
1544 result = rust_subscript (expect_type, exp, noside, false, lhs, rhs);
1545 }
c44af4eb
TT
1546 break;
1547
1548 case OP_FUNCALL:
1549 result = rust_evaluate_funcall (exp, pos, noside);
1550 break;
1551
1552 case OP_AGGREGATE:
1553 {
1554 int pc = (*pos)++;
1555 struct type *type = exp->elts[pc + 1].type;
1556 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1557 int i;
1558 CORE_ADDR addr = 0;
1559 struct value *addrval = NULL;
1560
1561 *pos += 3;
1562
1563 if (noside == EVAL_NORMAL)
1564 {
1565 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1566 addr = value_as_long (addrval);
1567 result = value_at_lazy (type, addr);
1568 }
1569
1570 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1571 {
1572 struct value *init;
1573
1574 ++*pos;
1575 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1576 if (noside == EVAL_NORMAL)
1577 {
1578 /* This isn't quite right but will do for the time
1579 being, seeing that we can't implement the Copy
1580 trait anyway. */
1581 value_assign (result, init);
1582 }
1583
1584 --arglen;
1585 }
1586
1587 gdb_assert (arglen % 2 == 0);
1588 for (i = 0; i < arglen; i += 2)
1589 {
1590 int len;
1591 const char *fieldname;
1592 struct value *value, *field;
1593
1594 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1595 ++*pos;
1596 len = longest_to_int (exp->elts[*pos].longconst);
1597 ++*pos;
1598 fieldname = &exp->elts[*pos].string;
1599 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1600
1601 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1602 if (noside == EVAL_NORMAL)
1603 {
1604 field = value_struct_elt (&result, NULL, fieldname, NULL,
1605 "structure");
1606 value_assign (field, value);
1607 }
1608 }
1609
1610 if (noside == EVAL_SKIP)
1611 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1612 1);
1613 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1614 result = allocate_value (type);
1615 else
1616 result = value_at_lazy (type, addr);
1617 }
1618 break;
1619
1620 case OP_RUST_ARRAY:
1621 {
8d49165d 1622 (*pos)++;
c44af4eb
TT
1623 struct value *elt;
1624 struct value *ncopies;
1625
1626 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1627 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
6fab4359 1628 return eval_op_rust_array (expect_type, exp, noside, op, elt, ncopies);
c44af4eb
TT
1629 }
1630 break;
1631
1632 case STRUCTOP_ANONYMOUS:
1633 {
dda83cd7
SM
1634 /* Anonymous field access, i.e. foo.1. */
1635 struct value *lhs;
575cae23 1636 int pc, field_number;
dda83cd7
SM
1637
1638 pc = (*pos)++;
1639 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1640 (*pos) += 2;
fe1fe7ea 1641 lhs = evaluate_subexp (nullptr, exp, pos, noside);
c44af4eb 1642
575cae23
TT
1643 return eval_op_rust_struct_anon (expect_type, exp, noside,
1644 field_number, lhs);
c44af4eb
TT
1645 }
1646 break;
1647
1648 case STRUCTOP_STRUCT:
1649 {
dda83cd7 1650 struct value *lhs;
dda83cd7 1651 int tem, pc;
c44af4eb 1652
dda83cd7
SM
1653 pc = (*pos)++;
1654 tem = longest_to_int (exp->elts[pc + 1].longconst);
1655 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
fe1fe7ea 1656 lhs = evaluate_subexp (nullptr, exp, pos, noside);
c44af4eb 1657
6830f270 1658 const char *field_name = &exp->elts[pc + 2].string;
1fa41fc7
TT
1659 return eval_op_rust_structop (expect_type, exp, noside, lhs,
1660 field_name);
c44af4eb
TT
1661 }
1662 break;
1663
01739a3b 1664 case OP_RANGE:
d148f803
TT
1665 {
1666 struct value *low = NULL, *high = NULL;
1667 auto kind
1668 = (enum range_flag) longest_to_int (exp->elts[*pos + 1].longconst);
1669 *pos += 3;
1670
1671 if (!(kind & RANGE_LOW_BOUND_DEFAULT))
1672 low = evaluate_subexp (nullptr, exp, pos, noside);
1673 if (!(kind & RANGE_HIGH_BOUND_DEFAULT))
1674 high = evaluate_subexp (nullptr, exp, pos, noside);
1675
1676 result = rust_range (expect_type, exp, noside, kind, low, high);
1677 }
c44af4eb
TT
1678 break;
1679
1680 case UNOP_ADDR:
1681 /* We might have &array[range], in which case we need to make a
1682 slice. */
1683 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1684 {
1685 ++*pos;
984af2cb
TT
1686 ++*pos;
1687 struct value *lhs = evaluate_subexp (nullptr, exp, pos, noside);
1688 struct value *rhs = evaluate_subexp (nullptr, exp, pos, noside);
1689
1690 result = rust_subscript (expect_type, exp, noside, true, lhs, rhs);
c44af4eb
TT
1691 break;
1692 }
1693 /* Fall through. */
1694 default:
1695 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1696 break;
1697 }
1698
1699 return result;
1700}
1701
1702/* operator_length implementation for Rust. */
1703
1704static void
1705rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1706 int *argsp)
1707{
1708 int oplen = 1;
1709 int args = 0;
1710
1711 switch (exp->elts[pc - 1].opcode)
1712 {
1713 case OP_AGGREGATE:
1714 /* We handle aggregate as a type and argument count. The first
1715 argument might be OP_OTHERS. After that the arguments
1716 alternate: first an OP_NAME, then an expression. */
1717 oplen = 4;
1718 args = longest_to_int (exp->elts[pc - 2].longconst);
1719 break;
1720
1721 case OP_OTHERS:
1722 oplen = 1;
1723 args = 1;
1724 break;
1725
1726 case STRUCTOP_ANONYMOUS:
1727 oplen = 3;
1728 args = 1;
1729 break;
1730
1731 case OP_RUST_ARRAY:
1732 oplen = 1;
1733 args = 2;
1734 break;
1735
1736 default:
1737 operator_length_standard (exp, pc, oplenp, argsp);
1738 return;
1739 }
1740
1741 *oplenp = oplen;
1742 *argsp = args;
1743}
1744
c44af4eb
TT
1745/* dump_subexp_body implementation for Rust. */
1746
1747static int
1748rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
1749 int elt)
1750{
1751 switch (exp->elts[elt].opcode)
1752 {
1753 case OP_AGGREGATE:
1754 {
1755 int length = longest_to_int (exp->elts[elt + 2].longconst);
1756 int i;
1757
1758 fprintf_filtered (stream, "Type @");
1759 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1760 fprintf_filtered (stream, " (");
1761 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1762 fprintf_filtered (stream, "), length %d", length);
1763
1764 elt += 4;
1765 for (i = 0; i < length; ++i)
1766 elt = dump_subexp (exp, stream, elt);
1767 }
1768 break;
1769
1770 case OP_STRING:
1771 case OP_NAME:
1772 {
1773 LONGEST len = exp->elts[elt + 1].longconst;
1774
1775 fprintf_filtered (stream, "%s: %s",
1776 (exp->elts[elt].opcode == OP_STRING
1777 ? "string" : "name"),
1778 &exp->elts[elt + 2].string);
1779 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1780 }
1781 break;
1782
1783 case OP_OTHERS:
1784 elt = dump_subexp (exp, stream, elt + 1);
1785 break;
1786
1787 case STRUCTOP_ANONYMOUS:
1788 {
1789 int field_number;
1790
68f2f2e3 1791 field_number = longest_to_int (exp->elts[elt + 1].longconst);
c44af4eb
TT
1792
1793 fprintf_filtered (stream, "Field number: %d", field_number);
68f2f2e3 1794 elt = dump_subexp (exp, stream, elt + 3);
c44af4eb
TT
1795 }
1796 break;
1797
1798 case OP_RUST_ARRAY:
68f2f2e3 1799 ++elt;
c44af4eb
TT
1800 break;
1801
1802 default:
1803 elt = dump_subexp_body_standard (exp, stream, elt);
1804 break;
1805 }
1806
1807 return elt;
1808}
1809
1810/* print_subexp implementation for Rust. */
1811
1812static void
1813rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
1814 enum precedence prec)
1815{
1816 switch (exp->elts[*pos].opcode)
1817 {
1818 case OP_AGGREGATE:
1819 {
1820 int length = longest_to_int (exp->elts[*pos + 2].longconst);
1821 int i;
1822
1823 type_print (exp->elts[*pos + 1].type, "", stream, 0);
1824 fputs_filtered (" { ", stream);
1825
1826 *pos += 4;
1827 for (i = 0; i < length; ++i)
1828 {
1829 rust_print_subexp (exp, pos, stream, prec);
1830 fputs_filtered (", ", stream);
1831 }
1832 fputs_filtered (" }", stream);
1833 }
1834 break;
1835
1836 case OP_NAME:
1837 {
1838 LONGEST len = exp->elts[*pos + 1].longconst;
1839
1840 fputs_filtered (&exp->elts[*pos + 2].string, stream);
1841 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
1842 }
1843 break;
1844
1845 case OP_OTHERS:
1846 {
1847 fputs_filtered ("<<others>> (", stream);
1848 ++*pos;
1849 rust_print_subexp (exp, pos, stream, prec);
1850 fputs_filtered (")", stream);
1851 }
1852 break;
1853
1854 case STRUCTOP_ANONYMOUS:
1855 {
1856 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
1857
1858 (*pos) += 3;
1859 print_subexp (exp, pos, stream, PREC_SUFFIX);
1860 fprintf_filtered (stream, ".%d", tem);
1861 }
256afbc2 1862 break;
c44af4eb
TT
1863
1864 case OP_RUST_ARRAY:
1865 ++*pos;
1866 fprintf_filtered (stream, "[");
1867 rust_print_subexp (exp, pos, stream, prec);
1868 fprintf_filtered (stream, "; ");
1869 rust_print_subexp (exp, pos, stream, prec);
1870 fprintf_filtered (stream, "]");
1871 break;
1872
1873 default:
1874 print_subexp_standard (exp, pos, stream, prec);
1875 break;
1876 }
1877}
1878
1879/* operator_check implementation for Rust. */
1880
1881static int
1882rust_operator_check (struct expression *exp, int pos,
1883 int (*objfile_func) (struct objfile *objfile,
1884 void *data),
1885 void *data)
1886{
1887 switch (exp->elts[pos].opcode)
1888 {
1889 case OP_AGGREGATE:
1890 {
1891 struct type *type = exp->elts[pos + 1].type;
6ac37371 1892 struct objfile *objfile = type->objfile_owner ();
c44af4eb
TT
1893
1894 if (objfile != NULL && (*objfile_func) (objfile, data))
1895 return 1;
1896 }
1897 break;
1898
1899 case OP_OTHERS:
1900 case OP_NAME:
1901 case OP_RUST_ARRAY:
1902 break;
1903
1904 default:
1905 return operator_check_standard (exp, pos, objfile_func, data);
1906 }
1907
1908 return 0;
1909}
1910
1911\f
1912
1c485265 1913const struct exp_descriptor rust_language::exp_descriptor_tab =
c44af4eb
TT
1914{
1915 rust_print_subexp,
1916 rust_operator_length,
1917 rust_operator_check,
c44af4eb
TT
1918 rust_dump_subexp_body,
1919 rust_evaluate_subexp
1920};
1921
1c485265 1922/* See language.h. */
0874fd07 1923
1c485265
AB
1924void
1925rust_language::language_arch_info (struct gdbarch *gdbarch,
1926 struct language_arch_info *lai) const
0874fd07 1927{
1c485265 1928 const struct builtin_type *builtin = builtin_type (gdbarch);
87afa652 1929
1c485265
AB
1930 /* Helper function to allow shorter lines below. */
1931 auto add = [&] (struct type * t) -> struct type *
87afa652 1932 {
1c485265
AB
1933 lai->add_primitive_type (t);
1934 return t;
1935 };
1936
1937 struct type *bool_type
1938 = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
1939 add (arch_character_type (gdbarch, 32, 1, "char"));
1940 add (arch_integer_type (gdbarch, 8, 0, "i8"));
1941 struct type *u8_type
1942 = add (arch_integer_type (gdbarch, 8, 1, "u8"));
1943 add (arch_integer_type (gdbarch, 16, 0, "i16"));
1944 add (arch_integer_type (gdbarch, 16, 1, "u16"));
1945 add (arch_integer_type (gdbarch, 32, 0, "i32"));
1946 add (arch_integer_type (gdbarch, 32, 1, "u32"));
1947 add (arch_integer_type (gdbarch, 64, 0, "i64"));
1948 add (arch_integer_type (gdbarch, 64, 1, "u64"));
1949
1950 unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1951 add (arch_integer_type (gdbarch, length, 0, "isize"));
1952 struct type *usize_type
1953 = add (arch_integer_type (gdbarch, length, 1, "usize"));
1954
1955 add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
1956 add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
1957 add (arch_integer_type (gdbarch, 0, 1, "()"));
1958
1959 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1960 add (rust_slice_type ("&str", tem, usize_type));
1961
1962 lai->set_bool_type (bool_type);
1963 lai->set_string_char_type (u8_type);
1964}
39e7ecca 1965
1c485265 1966/* See language.h. */
efdf6a73 1967
1c485265
AB
1968void
1969rust_language::print_type (struct type *type, const char *varstring,
1970 struct ui_file *stream, int show, int level,
1971 const struct type_print_options *flags) const
1972{
1973 print_offset_data podata;
1974 rust_internal_print_type (type, varstring, stream, show, level,
1975 flags, false, &podata);
1976}
efdf6a73 1977
1c485265 1978/* See language.h. */
5aba6ebe 1979
1c485265
AB
1980void
1981rust_language::emitchar (int ch, struct type *chtype,
1982 struct ui_file *stream, int quoter) const
1983{
1984 if (!rust_chartype_p (chtype))
1985 generic_emit_char (ch, chtype, stream, quoter,
8ee511af 1986 target_charset (chtype->arch ()));
1c485265
AB
1987 else if (ch == '\\' || ch == quoter)
1988 fprintf_filtered (stream, "\\%c", ch);
1989 else if (ch == '\n')
1990 fputs_filtered ("\\n", stream);
1991 else if (ch == '\r')
1992 fputs_filtered ("\\r", stream);
1993 else if (ch == '\t')
1994 fputs_filtered ("\\t", stream);
1995 else if (ch == '\0')
1996 fputs_filtered ("\\0", stream);
1997 else if (ch >= 32 && ch <= 127 && isprint (ch))
1998 fputc_filtered (ch, stream);
1999 else if (ch <= 255)
2000 fprintf_filtered (stream, "\\x%02x", ch);
2001 else
2002 fprintf_filtered (stream, "\\u{%06x}", ch);
2003}
5aba6ebe 2004
1c485265 2005/* See language.h. */
b7c6e27d 2006
1c485265
AB
2007bool
2008rust_language::is_string_type_p (struct type *type) const
2009{
2010 LONGEST low_bound, high_bound;
b7c6e27d 2011
1c485265
AB
2012 type = check_typedef (type);
2013 return ((type->code () == TYPE_CODE_STRING)
2014 || (type->code () == TYPE_CODE_PTR
2015 && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
2016 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
2017 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
2018 &high_bound)))
2019 || (type->code () == TYPE_CODE_STRUCT
2020 && !rust_enum_p (type)
2021 && rust_slice_type_p (type)
2022 && strcmp (type->name (), "&str") == 0));
2023}
0874fd07
AB
2024
2025/* Single instance of the Rust language class. */
2026
2027static rust_language rust_language_defn;