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