]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rust-lang.c
Unify gdb puts functions
[thirdparty/binutils-gdb.git] / gdb / rust-lang.c
CommitLineData
c44af4eb
TT
1/* Rust language support routines for GDB, the GNU debugger.
2
4a94e368 3 Copyright (C) 2016-2022 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 137 xsnprintf (buf, sizeof (buf), "__%d", field_number);
33d16dd9 138 if (strcmp (buf, type->field (i).name ()) != 0)
c9317f21
TT
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;
33d16dd9 185 if (strcmp (type->field (0).name (), "start") == 0)
c44af4eb 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
33d16dd9 197 return strcmp (type->field (i).name (), "end") == 0;
c44af4eb
TT
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 {
33d16dd9 247 if (strcmp (type->field (i).name (), "vtable") == 0)
71a3c369 248 vtable_field = i;
33d16dd9 249 else if (strcmp (type->field (i).name (), "pointer") != 0)
71a3c369
TT
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{
158cc4fe 305 struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL,
45320ffa 306 "slice");
158cc4fe 307 struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice");
45320ffa
TT
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)
0426ad51 352 gdb_puts (" ", stream);
b96645f1
MG
353 }
354
355 if (is_tuple || is_tuple_struct)
0426ad51 356 gdb_puts ("(", stream);
b96645f1 357 else
0426ad51 358 gdb_puts ("{", stream);
b96645f1
MG
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)
0426ad51 370 gdb_puts (",", stream);
b96645f1
MG
371
372 if (options->prettyformat)
dda83cd7 373 {
0426ad51 374 gdb_puts ("\n", stream);
b50f188d 375 print_spaces_filtered (2 + 2 * recurse, stream);
dda83cd7 376 }
b96645f1 377 else if (!first_field)
0426ad51 378 gdb_puts (" ", stream);
b96645f1
MG
379
380 first_field = 0;
381
382 if (!is_tuple && !is_tuple_struct)
dda83cd7 383 {
33d16dd9 384 fputs_styled (type->field (i).name (),
3f0cbb04 385 variable_name_style.style (), stream);
0426ad51 386 gdb_puts (": ", stream);
dda83cd7 387 }
b96645f1 388
887e7158
TT
389 common_val_print (value_field (val, i), stream, recurse + 1, &opts,
390 this);
b96645f1
MG
391 }
392
393 if (options->prettyformat)
394 {
0426ad51 395 gdb_puts ("\n", stream);
b96645f1
MG
396 print_spaces_filtered (2 * recurse, stream);
397 }
398
399 if (is_tuple || is_tuple_struct)
0426ad51 400 gdb_puts (")", stream);
b96645f1 401 else
0426ad51 402 gdb_puts ("}", stream);
b96645f1
MG
403}
404
1c485265 405/* See rust-lang.h. */
c9317f21 406
1c485265
AB
407void
408rust_language::print_enum (struct value *val, struct ui_file *stream,
409 int recurse,
410 const struct value_print_options *options) const
c9317f21
TT
411{
412 struct value_print_options opts = *options;
5f56f7cb 413 struct type *type = check_typedef (value_type (val));
c9317f21
TT
414
415 opts.deref_ref = 0;
416
9c6a1327 417 gdb_assert (rust_enum_p (type));
50888e42
SM
418 gdb::array_view<const gdb_byte> view
419 (value_contents_for_printing (val).data (),
420 TYPE_LENGTH (value_type (val)));
9c6a1327
TT
421 type = resolve_dynamic_type (type, view, value_address (val));
422
098b2108
TT
423 if (rust_empty_enum_p (type))
424 {
425 /* Print the enum type name here to be more clear. */
7f6aba03 426 fprintf_filtered (stream, _("%s {%p[<No data fields>%p]}"),
7d93a1e0 427 type->name (),
7f6aba03 428 metadata_style.style ().ptr (), nullptr);
098b2108
TT
429 return;
430 }
431
9c6a1327
TT
432 int variant_fieldno = rust_enum_variant (type);
433 val = value_field (val, variant_fieldno);
940da03e 434 struct type *variant_type = type->field (variant_fieldno).type ();
c9317f21 435
1f704f76 436 int nfields = variant_type->num_fields ();
c9317f21
TT
437
438 bool is_tuple = rust_tuple_struct_type_p (variant_type);
439
7d93a1e0 440 fprintf_filtered (stream, "%s", variant_type->name ());
c9317f21
TT
441 if (nfields == 0)
442 {
443 /* In case of a nullary variant like 'None', just output
444 the name. */
445 return;
446 }
447
448 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
449 if (is_tuple)
450 fprintf_filtered (stream, "(");
451 else
452 {
453 /* struct variant. */
454 fprintf_filtered (stream, "{");
455 }
456
457 bool first_field = true;
1f704f76 458 for (int j = 0; j < variant_type->num_fields (); j++)
c9317f21
TT
459 {
460 if (!first_field)
0426ad51 461 gdb_puts (", ", stream);
c9317f21
TT
462 first_field = false;
463
464 if (!is_tuple)
3f0cbb04
TT
465 fprintf_filtered (stream, "%ps: ",
466 styled_string (variable_name_style.style (),
33d16dd9 467 variant_type->field (j).name ()));
c9317f21 468
887e7158
TT
469 common_val_print (value_field (val, j), stream, recurse + 1, &opts,
470 this);
c9317f21
TT
471 }
472
473 if (is_tuple)
0426ad51 474 gdb_puts (")", stream);
c9317f21 475 else
0426ad51 476 gdb_puts ("}", stream);
c9317f21
TT
477}
478
c44af4eb
TT
479static const struct generic_val_print_decorations rust_decorations =
480{
481 /* Complex isn't used in Rust, but we provide C-ish values just in
482 case. */
483 "",
484 " + ",
485 " * I",
486 "true",
487 "false",
921d8f54 488 "()",
c44af4eb
TT
489 "[",
490 "]"
491};
492
1c485265
AB
493/* See language.h. */
494
495void
496rust_language::value_print_inner
497 (struct value *val, struct ui_file *stream, int recurse,
498 const struct value_print_options *options) const
5f56f7cb
TT
499{
500 struct value_print_options opts = *options;
501 opts.deref_ref = 1;
502
503 if (opts.prettyformat == Val_prettyformat_default)
504 opts.prettyformat = (opts.prettyformat_structs
505 ? Val_prettyformat : Val_no_prettyformat);
506
507 struct type *type = check_typedef (value_type (val));
78134374 508 switch (type->code ())
c44af4eb
TT
509 {
510 case TYPE_CODE_PTR:
511 {
512 LONGEST low_bound, high_bound;
513
78134374 514 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
c44af4eb
TT
515 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
516 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
5f56f7cb
TT
517 &high_bound))
518 {
519 /* We have a pointer to a byte string, so just print
520 that. */
521 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
522 CORE_ADDR addr = value_as_address (val);
8ee511af 523 struct gdbarch *arch = type->arch ();
c44af4eb 524
5f56f7cb
TT
525 if (opts.addressprint)
526 {
0426ad51
TT
527 gdb_puts (paddress (arch, addr), stream);
528 gdb_puts (" ", stream);
5f56f7cb 529 }
c44af4eb 530
0426ad51 531 gdb_puts ("b", stream);
5f56f7cb
TT
532 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
533 high_bound - low_bound + 1, stream,
534 &opts);
535 break;
536 }
c44af4eb 537 }
5f56f7cb 538 goto generic_print;
c44af4eb 539
c44af4eb
TT
540 case TYPE_CODE_INT:
541 /* Recognize the unit type. */
c6d940a9 542 if (type->is_unsigned () && TYPE_LENGTH (type) == 0
7d93a1e0 543 && type->name () != NULL && strcmp (type->name (), "()") == 0)
c44af4eb 544 {
0426ad51 545 gdb_puts ("()", stream);
c44af4eb
TT
546 break;
547 }
548 goto generic_print;
549
550 case TYPE_CODE_STRING:
551 {
c44af4eb
TT
552 LONGEST low_bound, high_bound;
553
554 if (!get_array_bounds (type, &low_bound, &high_bound))
555 error (_("Could not determine the array bounds"));
556
557 /* If we see a plain TYPE_CODE_STRING, then we're printing a
558 byte string, hence the choice of "ASCII" as the
559 encoding. */
0426ad51 560 gdb_puts ("b", stream);
1c485265 561 printstr (stream, TYPE_TARGET_TYPE (type),
50888e42 562 value_contents_for_printing (val).data (),
1c485265 563 high_bound - low_bound + 1, "ASCII", 0, &opts);
c44af4eb
TT
564 }
565 break;
566
567 case TYPE_CODE_ARRAY:
568 {
569 LONGEST low_bound, high_bound;
570
571 if (get_array_bounds (type, &low_bound, &high_bound)
572 && high_bound - low_bound + 1 == 0)
0426ad51 573 gdb_puts ("[]", stream);
c44af4eb
TT
574 else
575 goto generic_print;
576 }
577 break;
578
579 case TYPE_CODE_UNION:
c9317f21
TT
580 /* Untagged unions are printed as if they are structs. Since
581 the field bit positions overlap in the debuginfo, the code
582 for printing a union is same as that for a struct, the only
583 difference is that the input type will have overlapping
584 fields. */
5f56f7cb 585 val_print_struct (val, stream, recurse, &opts);
c44af4eb
TT
586 break;
587
588 case TYPE_CODE_STRUCT:
c9317f21 589 if (rust_enum_p (type))
1c485265 590 print_enum (val, stream, recurse, &opts);
c9317f21 591 else
5f56f7cb 592 val_print_struct (val, stream, recurse, &opts);
b96645f1 593 break;
c44af4eb 594
b96645f1
MG
595 default:
596 generic_print:
597 /* Nothing special yet. */
5f56f7cb 598 generic_value_print (val, stream, recurse, &opts, &rust_decorations);
b96645f1
MG
599 }
600}
c44af4eb 601
b96645f1 602\f
c44af4eb 603
b96645f1 604static void
c9317f21
TT
605rust_internal_print_type (struct type *type, const char *varstring,
606 struct ui_file *stream, int show, int level,
607 const struct type_print_options *flags,
a33ccfc7 608 bool for_rust_enum, print_offset_data *podata);
b96645f1
MG
609
610/* Print a struct or union typedef. */
611static void
612rust_print_struct_def (struct type *type, const char *varstring,
b50f188d 613 struct ui_file *stream, int show, int level,
c9317f21 614 const struct type_print_options *flags,
a33ccfc7 615 bool for_rust_enum, print_offset_data *podata)
b96645f1 616{
b50f188d
TT
617 /* Print a tuple type simply. */
618 if (rust_tuple_type_p (type))
619 {
0426ad51 620 gdb_puts (type->name (), stream);
b50f188d
TT
621 return;
622 }
c44af4eb 623
b50f188d
TT
624 /* If we see a base class, delegate to C. */
625 if (TYPE_N_BASECLASSES (type) > 0)
626 c_print_type (type, varstring, stream, show, level, flags);
c44af4eb 627
a33ccfc7
TT
628 if (flags->print_offsets)
629 {
630 /* Temporarily bump the level so that the output lines up
631 correctly. */
632 level += 2;
633 }
634
c9317f21
TT
635 /* Compute properties of TYPE here because, in the enum case, the
636 rest of the code ends up looking only at the variant part. */
7d93a1e0 637 const char *tagname = type->name ();
c9317f21
TT
638 bool is_tuple_struct = rust_tuple_struct_type_p (type);
639 bool is_tuple = rust_tuple_type_p (type);
640 bool is_enum = rust_enum_p (type);
b96645f1 641
c9317f21
TT
642 if (for_rust_enum)
643 {
644 /* Already printing an outer enum, so nothing to print here. */
645 }
646 else
647 {
648 /* This code path is also used by unions and enums. */
649 if (is_enum)
650 {
0426ad51 651 gdb_puts ("enum ", stream);
24e99c6c 652 dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
8c2e4e06
SM
653 if (prop != nullptr && prop->kind () == PROP_TYPE)
654 type = prop->original_type ();
c9317f21 655 }
78134374 656 else if (type->code () == TYPE_CODE_STRUCT)
0426ad51 657 gdb_puts ("struct ", stream);
c9317f21 658 else
0426ad51 659 gdb_puts ("union ", stream);
b96645f1 660
c9317f21 661 if (tagname != NULL)
0426ad51 662 gdb_puts (tagname, stream);
c9317f21 663 }
b96645f1 664
1f704f76 665 if (type->num_fields () == 0 && !is_tuple)
b50f188d 666 return;
a33ccfc7 667 if (for_rust_enum && !flags->print_offsets)
0426ad51 668 gdb_puts (is_tuple_struct ? "(" : "{", stream);
c9317f21 669 else
0426ad51 670 gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream);
c44af4eb 671
a33ccfc7
TT
672 /* When printing offsets, we rearrange the fields into storage
673 order. This lets us show holes more clearly. We work using
674 field indices here because it simplifies calls to
675 print_offset_data::update below. */
676 std::vector<int> fields;
1f704f76 677 for (int i = 0; i < type->num_fields (); ++i)
b50f188d 678 {
ceacbf6e 679 if (field_is_static (&type->field (i)))
b50f188d 680 continue;
9c6a1327 681 if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
a33ccfc7
TT
682 continue;
683 fields.push_back (i);
684 }
685 if (flags->print_offsets)
686 std::sort (fields.begin (), fields.end (),
687 [&] (int a, int b)
688 {
b610c045
SM
689 return (type->field (a).loc_bitpos ()
690 < type->field (b).loc_bitpos ());
a33ccfc7
TT
691 });
692
693 for (int i : fields)
694 {
695 QUIT;
696
ceacbf6e 697 gdb_assert (!field_is_static (&type->field (i)));
9c6a1327 698 gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
a33ccfc7
TT
699
700 if (flags->print_offsets)
701 podata->update (type, i, stream);
b50f188d
TT
702
703 /* We'd like to print "pub" here as needed, but rustc
704 doesn't emit the debuginfo, and our types don't have
705 cplus_struct_type attached. */
706
707 /* For a tuple struct we print the type but nothing
708 else. */
a33ccfc7 709 if (!for_rust_enum || flags->print_offsets)
c9317f21
TT
710 print_spaces_filtered (level + 2, stream);
711 if (is_enum)
33d16dd9 712 fputs_styled (type->field (i).name (), variable_name_style.style (),
3f0cbb04 713 stream);
c9317f21 714 else if (!is_tuple_struct)
3f0cbb04
TT
715 fprintf_filtered (stream, "%ps: ",
716 styled_string (variable_name_style.style (),
33d16dd9 717 type->field (i).name ()));
b50f188d 718
940da03e 719 rust_internal_print_type (type->field (i).type (), NULL,
53d7df28 720 stream, (is_enum ? show : show - 1),
a33ccfc7
TT
721 level + 2, flags, is_enum, podata);
722 if (!for_rust_enum || flags->print_offsets)
0426ad51 723 gdb_puts (",\n", stream);
a33ccfc7
TT
724 /* Note that this check of "I" is ok because we only sorted the
725 fields by offset when print_offsets was set, so we won't take
726 this branch in that case. */
1f704f76 727 else if (i + 1 < type->num_fields ())
0426ad51 728 gdb_puts (", ", stream);
b50f188d 729 }
c44af4eb 730
a33ccfc7
TT
731 if (flags->print_offsets)
732 {
733 /* Undo the temporary level increase we did above. */
734 level -= 2;
735 podata->finish (type, level, stream);
736 print_spaces_filtered (print_offset_data::indentation, stream);
737 if (level == 0)
738 print_spaces_filtered (2, stream);
739 }
740 if (!for_rust_enum || flags->print_offsets)
c9317f21 741 print_spaces_filtered (level, stream);
0426ad51 742 gdb_puts (is_tuple_struct ? ")" : "}", stream);
c44af4eb
TT
743}
744
c44af4eb
TT
745/* la_print_type implementation for Rust. */
746
747static void
c9317f21
TT
748rust_internal_print_type (struct type *type, const char *varstring,
749 struct ui_file *stream, int show, int level,
750 const struct type_print_options *flags,
a33ccfc7 751 bool for_rust_enum, print_offset_data *podata)
c44af4eb 752{
c44af4eb
TT
753 QUIT;
754 if (show <= 0
7d93a1e0 755 && type->name () != NULL)
c44af4eb 756 {
921d8f54 757 /* Rust calls the unit type "void" in its debuginfo,
dda83cd7 758 but we don't want to print it as that. */
78134374 759 if (type->code () == TYPE_CODE_VOID)
0426ad51 760 gdb_puts ("()", stream);
921d8f54 761 else
0426ad51 762 gdb_puts (type->name (), stream);
c44af4eb
TT
763 return;
764 }
765
766 type = check_typedef (type);
78134374 767 switch (type->code ())
c44af4eb 768 {
921d8f54 769 case TYPE_CODE_VOID:
c9317f21
TT
770 /* If we have an enum, we've already printed the type's
771 unqualified name, and there is nothing else to print
772 here. */
773 if (!for_rust_enum)
0426ad51 774 gdb_puts ("()", stream);
921d8f54
MG
775 break;
776
c44af4eb
TT
777 case TYPE_CODE_FUNC:
778 /* Delegate varargs to the C printer. */
a409645d 779 if (type->has_varargs ())
c44af4eb
TT
780 goto c_printer;
781
0426ad51 782 gdb_puts ("fn ", stream);
c44af4eb 783 if (varstring != NULL)
0426ad51
TT
784 gdb_puts (varstring, stream);
785 gdb_puts ("(", stream);
1f704f76 786 for (int i = 0; i < type->num_fields (); ++i)
c44af4eb
TT
787 {
788 QUIT;
789 if (i > 0)
0426ad51 790 gdb_puts (", ", stream);
940da03e 791 rust_internal_print_type (type->field (i).type (), "", stream,
a33ccfc7 792 -1, 0, flags, false, podata);
c44af4eb 793 }
0426ad51 794 gdb_puts (")", stream);
921d8f54 795 /* If it returns unit, we can omit the return type. */
78134374 796 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
dda83cd7 797 {
0426ad51 798 gdb_puts (" -> ", stream);
dda83cd7 799 rust_internal_print_type (TYPE_TARGET_TYPE (type), "", stream,
a33ccfc7 800 -1, 0, flags, false, podata);
dda83cd7 801 }
c44af4eb
TT
802 break;
803
804 case TYPE_CODE_ARRAY:
805 {
806 LONGEST low_bound, high_bound;
807
0426ad51 808 gdb_puts ("[", stream);
c9317f21 809 rust_internal_print_type (TYPE_TARGET_TYPE (type), NULL,
a33ccfc7
TT
810 stream, show - 1, level, flags, false,
811 podata);
c44af4eb 812
cf88be68
SM
813 if (type->bounds ()->high.kind () == PROP_LOCEXPR
814 || type->bounds ()->high.kind () == PROP_LOCLIST)
e6cf65f2 815 fprintf_filtered (stream, "; variable length");
c44af4eb 816 else if (get_array_bounds (type, &low_bound, &high_bound))
e6cf65f2 817 fprintf_filtered (stream, "; %s",
c44af4eb 818 plongest (high_bound - low_bound + 1));
0426ad51 819 gdb_puts ("]", stream);
c44af4eb
TT
820 }
821 break;
822
c9317f21 823 case TYPE_CODE_UNION:
c44af4eb 824 case TYPE_CODE_STRUCT:
c9317f21 825 rust_print_struct_def (type, varstring, stream, show, level, flags,
a33ccfc7 826 for_rust_enum, podata);
c44af4eb
TT
827 break;
828
829 case TYPE_CODE_ENUM:
830 {
b926417a 831 int len = 0;
c44af4eb 832
0426ad51 833 gdb_puts ("enum ", stream);
7d93a1e0 834 if (type->name () != NULL)
c44af4eb 835 {
0426ad51
TT
836 gdb_puts (type->name (), stream);
837 gdb_puts (" ", stream);
7d93a1e0 838 len = strlen (type->name ());
c44af4eb 839 }
0426ad51 840 gdb_puts ("{\n", stream);
c44af4eb 841
1f704f76 842 for (int i = 0; i < type->num_fields (); ++i)
c44af4eb 843 {
33d16dd9 844 const char *name = type->field (i).name ();
c44af4eb
TT
845
846 QUIT;
847
848 if (len > 0
7d93a1e0 849 && strncmp (name, type->name (), len) == 0
c44af4eb
TT
850 && name[len] == ':'
851 && name[len + 1] == ':')
852 name += len + 2;
32f47895
TT
853 fprintf_filtered (stream, "%*s%ps,\n",
854 level + 2, "",
855 styled_string (variable_name_style.style (),
856 name));
c44af4eb
TT
857 }
858
0426ad51 859 gdb_puts ("}", stream);
c44af4eb 860 }
73fc52c4
TT
861 break;
862
863 case TYPE_CODE_PTR:
864 {
7d93a1e0 865 if (type->name () != nullptr)
0426ad51 866 gdb_puts (type->name (), stream);
73fc52c4
TT
867 else
868 {
869 /* We currently can't distinguish between pointers and
870 references. */
0426ad51 871 gdb_puts ("*mut ", stream);
73fc52c4
TT
872 type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
873 }
874 }
c44af4eb
TT
875 break;
876
c44af4eb
TT
877 default:
878 c_printer:
879 c_print_type (type, varstring, stream, show, level, flags);
880 }
881}
882
883\f
884
c44af4eb
TT
885/* Like arch_composite_type, but uses TYPE to decide how to allocate
886 -- either on an obstack or on a gdbarch. */
887
888static struct type *
889rust_composite_type (struct type *original,
890 const char *name,
891 const char *field1, struct type *type1,
892 const char *field2, struct type *type2)
893{
894 struct type *result = alloc_type_copy (original);
895 int i, nfields, bitpos;
896
897 nfields = 0;
898 if (field1 != NULL)
899 ++nfields;
900 if (field2 != NULL)
901 ++nfields;
902
67607e24 903 result->set_code (TYPE_CODE_STRUCT);
d0e39ea2 904 result->set_name (name);
c44af4eb 905
5e33d5f4 906 result->set_num_fields (nfields);
3cabb6b0
SM
907 result->set_fields
908 ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
c44af4eb
TT
909
910 i = 0;
911 bitpos = 0;
912 if (field1 != NULL)
913 {
ceacbf6e 914 struct field *field = &result->field (i);
c44af4eb 915
cd3f655c 916 field->set_loc_bitpos (bitpos);
c44af4eb
TT
917 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
918
d3fd12df 919 field->set_name (field1);
5d14b6e5 920 field->set_type (type1);
c44af4eb
TT
921 ++i;
922 }
923 if (field2 != NULL)
924 {
ceacbf6e 925 struct field *field = &result->field (i);
2fff16dd 926 unsigned align = type_align (type2);
c44af4eb
TT
927
928 if (align != 0)
929 {
930 int delta;
931
932 align *= TARGET_CHAR_BIT;
933 delta = bitpos % align;
934 if (delta != 0)
935 bitpos += align - delta;
936 }
cd3f655c 937 field->set_loc_bitpos (bitpos);
c44af4eb 938
d3fd12df 939 field->set_name (field2);
5d14b6e5 940 field->set_type (type2);
c44af4eb
TT
941 ++i;
942 }
943
944 if (i > 0)
945 TYPE_LENGTH (result)
b610c045 946 = (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT +
940da03e 947 TYPE_LENGTH (result->field (i - 1).type ()));
c44af4eb
TT
948 return result;
949}
950
951/* See rust-lang.h. */
952
953struct type *
954rust_slice_type (const char *name, struct type *elt_type,
955 struct type *usize_type)
956{
957 struct type *type;
958
959 elt_type = lookup_pointer_type (elt_type);
960 type = rust_composite_type (elt_type, name,
961 "data_ptr", elt_type,
962 "length", usize_type);
963
964 return type;
965}
966
c44af4eb
TT
967\f
968
01739a3b 969/* A helper for rust_evaluate_subexp that handles OP_RANGE. */
c44af4eb 970
9db6b6dd 971struct value *
d148f803
TT
972rust_range (struct type *expect_type, struct expression *exp,
973 enum noside noside, enum range_flag kind,
974 struct value *low, struct value *high)
c44af4eb 975{
c44af4eb
TT
976 struct value *addrval, *result;
977 CORE_ADDR addr;
978 struct type *range_type;
979 struct type *index_type;
980 struct type *temp_type;
981 const char *name;
982
2f1b18db 983 bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE);
c44af4eb 984
c44af4eb
TT
985 if (low == NULL)
986 {
987 if (high == NULL)
988 {
989 index_type = NULL;
990 name = "std::ops::RangeFull";
991 }
992 else
993 {
994 index_type = value_type (high);
6873858b
TT
995 name = (inclusive
996 ? "std::ops::RangeToInclusive" : "std::ops::RangeTo");
c44af4eb
TT
997 }
998 }
999 else
1000 {
1001 if (high == NULL)
1002 {
1003 index_type = value_type (low);
1004 name = "std::ops::RangeFrom";
1005 }
1006 else
1007 {
1008 if (!types_equal (value_type (low), value_type (high)))
1009 error (_("Range expression with different types"));
1010 index_type = value_type (low);
6873858b 1011 name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range";
c44af4eb
TT
1012 }
1013 }
1014
1015 /* If we don't have an index type, just allocate this on the
1016 arch. Here any type will do. */
1017 temp_type = (index_type == NULL
1018 ? language_bool_type (exp->language_defn, exp->gdbarch)
1019 : index_type);
1020 /* It would be nicer to cache the range type. */
1021 range_type = rust_composite_type (temp_type, name,
1022 low == NULL ? NULL : "start", index_type,
1023 high == NULL ? NULL : "end", index_type);
1024
1025 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1026 return value_zero (range_type, lval_memory);
1027
1028 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1029 addr = value_as_long (addrval);
1030 result = value_at_lazy (range_type, addr);
1031
1032 if (low != NULL)
1033 {
158cc4fe 1034 struct value *start = value_struct_elt (&result, {}, "start", NULL,
c44af4eb
TT
1035 "range");
1036
1037 value_assign (start, low);
1038 }
1039
1040 if (high != NULL)
1041 {
158cc4fe 1042 struct value *end = value_struct_elt (&result, {}, "end", NULL,
c44af4eb
TT
1043 "range");
1044
1045 value_assign (end, high);
1046 }
1047
1048 result = value_at_lazy (range_type, addr);
1049 return result;
1050}
1051
1052/* A helper function to compute the range and kind given a range
1053 value. TYPE is the type of the range value. RANGE is the range
1054 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1055 parameters might be filled in, or might not be, depending on the
1056 kind of range this is. KIND will always be set to the appropriate
1057 value describing the kind of range, and this can be used to
1058 determine whether LOW or HIGH are valid. */
1059
1060static void
1061rust_compute_range (struct type *type, struct value *range,
1062 LONGEST *low, LONGEST *high,
f2d8e4c5 1063 range_flags *kind)
c44af4eb
TT
1064{
1065 int i;
1066
1067 *low = 0;
1068 *high = 0;
2f1b18db 1069 *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
c44af4eb 1070
1f704f76 1071 if (type->num_fields () == 0)
c44af4eb
TT
1072 return;
1073
1074 i = 0;
33d16dd9 1075 if (strcmp (type->field (0).name (), "start") == 0)
c44af4eb 1076 {
2f1b18db 1077 *kind = RANGE_HIGH_BOUND_DEFAULT;
c44af4eb
TT
1078 *low = value_as_long (value_field (range, 0));
1079 ++i;
1080 }
1f704f76 1081 if (type->num_fields () > i
33d16dd9 1082 && strcmp (type->field (i).name (), "end") == 0)
c44af4eb 1083 {
2f1b18db
AB
1084 *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT)
1085 ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD);
c44af4eb 1086 *high = value_as_long (value_field (range, i));
6873858b
TT
1087
1088 if (rust_inclusive_range_type_p (type))
1089 ++*high;
c44af4eb
TT
1090 }
1091}
1092
1093/* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1094
6ce1ad67 1095struct value *
984af2cb
TT
1096rust_subscript (struct type *expect_type, struct expression *exp,
1097 enum noside noside, bool for_addr,
1098 struct value *lhs, struct value *rhs)
c44af4eb 1099{
984af2cb 1100 struct value *result;
c44af4eb 1101 struct type *rhstype;
45f4ed92 1102 LONGEST low, high_bound;
c44af4eb 1103 /* Initialized to appease the compiler. */
f2d8e4c5 1104 range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT;
45f4ed92 1105 LONGEST high = 0;
c44af4eb
TT
1106 int want_slice = 0;
1107
c44af4eb
TT
1108 rhstype = check_typedef (value_type (rhs));
1109 if (rust_range_type_p (rhstype))
1110 {
1111 if (!for_addr)
1112 error (_("Can't take slice of array without '&'"));
1113 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1114 want_slice = 1;
1115 }
1116 else
1117 low = value_as_long (rhs);
1118
b3e3859b 1119 struct type *type = check_typedef (value_type (lhs));
c44af4eb
TT
1120 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1121 {
b3e3859b 1122 struct type *base_type = nullptr;
78134374 1123 if (type->code () == TYPE_CODE_ARRAY)
b3e3859b
TT
1124 base_type = TYPE_TARGET_TYPE (type);
1125 else if (rust_slice_type_p (type))
1126 {
1f704f76 1127 for (int i = 0; i < type->num_fields (); ++i)
b3e3859b 1128 {
33d16dd9 1129 if (strcmp (type->field (i).name (), "data_ptr") == 0)
b3e3859b 1130 {
940da03e 1131 base_type = TYPE_TARGET_TYPE (type->field (i).type ());
b3e3859b
TT
1132 break;
1133 }
1134 }
1135 if (base_type == nullptr)
1136 error (_("Could not find 'data_ptr' in slice type"));
1137 }
78134374 1138 else if (type->code () == TYPE_CODE_PTR)
b3e3859b
TT
1139 base_type = TYPE_TARGET_TYPE (type);
1140 else
1141 error (_("Cannot subscript non-array type"));
1142
1143 struct type *new_type;
1144 if (want_slice)
1145 {
1146 if (rust_slice_type_p (type))
1147 new_type = type;
1148 else
1149 {
1150 struct type *usize
1151 = language_lookup_primitive_type (exp->language_defn,
1152 exp->gdbarch,
1153 "usize");
1154 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1155 }
1156 }
1157 else
1158 new_type = base_type;
c44af4eb 1159
b3e3859b 1160 return value_zero (new_type, VALUE_LVAL (lhs));
c44af4eb
TT
1161 }
1162 else
1163 {
1164 LONGEST low_bound;
1165 struct value *base;
c44af4eb 1166
78134374 1167 if (type->code () == TYPE_CODE_ARRAY)
c44af4eb
TT
1168 {
1169 base = lhs;
1170 if (!get_array_bounds (type, &low_bound, &high_bound))
1171 error (_("Can't compute array bounds"));
1172 if (low_bound != 0)
1173 error (_("Found array with non-zero lower bound"));
1174 ++high_bound;
1175 }
1176 else if (rust_slice_type_p (type))
1177 {
1178 struct value *len;
1179
158cc4fe
AB
1180 base = value_struct_elt (&lhs, {}, "data_ptr", NULL, "slice");
1181 len = value_struct_elt (&lhs, {}, "length", NULL, "slice");
c44af4eb
TT
1182 low_bound = 0;
1183 high_bound = value_as_long (len);
1184 }
78134374 1185 else if (type->code () == TYPE_CODE_PTR)
42d94011
MG
1186 {
1187 base = lhs;
1188 low_bound = 0;
1189 high_bound = LONGEST_MAX;
1190 }
c44af4eb
TT
1191 else
1192 error (_("Cannot subscript non-array type"));
1193
2f1b18db 1194 if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT))
c44af4eb
TT
1195 low = low_bound;
1196 if (low < 0)
1197 error (_("Index less than zero"));
1198 if (low > high_bound)
1199 error (_("Index greater than length"));
1200
1201 result = value_subscript (base, low);
1202 }
1203
1204 if (for_addr)
1205 {
1206 if (want_slice)
1207 {
1208 struct type *usize, *slice;
1209 CORE_ADDR addr;
1210 struct value *addrval, *tem;
1211
2f1b18db 1212 if (kind & RANGE_HIGH_BOUND_DEFAULT)
c44af4eb
TT
1213 high = high_bound;
1214 if (high < 0)
1215 error (_("High index less than zero"));
1216 if (low > high)
1217 error (_("Low index greater than high index"));
1218 if (high > high_bound)
1219 error (_("High index greater than length"));
1220
1221 usize = language_lookup_primitive_type (exp->language_defn,
1222 exp->gdbarch,
1223 "usize");
45320ffa
TT
1224 const char *new_name = ((type != nullptr
1225 && rust_slice_type_p (type))
7d93a1e0 1226 ? type->name () : "&[*gdb*]");
45320ffa
TT
1227
1228 slice = rust_slice_type (new_name, value_type (result), usize);
c44af4eb
TT
1229
1230 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1231 addr = value_as_long (addrval);
1232 tem = value_at_lazy (slice, addr);
1233
1234 value_assign (value_field (tem, 0), value_addr (result));
1235 value_assign (value_field (tem, 1),
1236 value_from_longest (usize, high - low));
1237
1238 result = value_at_lazy (slice, addr);
1239 }
1240 else
1241 result = value_addr (result);
1242 }
1243
1244 return result;
1245}
1246
f10522c0
TT
1247namespace expr
1248{
d123f9e4 1249
11dd3dce 1250struct value *
f10522c0
TT
1251rust_unop_ind_operation::evaluate (struct type *expect_type,
1252 struct expression *exp,
1253 enum noside noside)
d123f9e4 1254{
f10522c0
TT
1255 if (noside != EVAL_NORMAL)
1256 return unop_ind_operation::evaluate (expect_type, exp, noside);
1257
1258 struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp,
1259 noside);
d123f9e4
TT
1260 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1261 if (trait_ptr != NULL)
1262 value = trait_ptr;
1263
1264 return value_ind (value);
1265}
1266
f10522c0
TT
1267} /* namespace expr */
1268
6fa9831f
TT
1269/* A helper function for UNOP_COMPLEMENT. */
1270
6fab4359 1271struct value *
6fa9831f
TT
1272eval_op_rust_complement (struct type *expect_type, struct expression *exp,
1273 enum noside noside,
6fab4359 1274 enum exp_opcode opcode,
6fa9831f
TT
1275 struct value *value)
1276{
6fa9831f
TT
1277 if (value_type (value)->code () == TYPE_CODE_BOOL)
1278 return value_from_longest (value_type (value), value_logical_not (value));
1279 return value_complement (value);
1280}
1281
05104233
TT
1282/* A helper function for OP_ARRAY. */
1283
6fab4359 1284struct value *
05104233
TT
1285eval_op_rust_array (struct type *expect_type, struct expression *exp,
1286 enum noside noside,
6fab4359 1287 enum exp_opcode opcode,
05104233
TT
1288 struct value *elt, struct value *ncopies)
1289{
1290 int copies = value_as_long (ncopies);
1291 if (copies < 0)
1292 error (_("Array with negative number of elements"));
1293
1294 if (noside == EVAL_NORMAL)
1295 {
1296 int i;
1297 std::vector<struct value *> eltvec (copies);
1298
1299 for (i = 0; i < copies; ++i)
1300 eltvec[i] = elt;
1301 return value_array (0, copies - 1, eltvec.data ());
1302 }
1303 else
1304 {
1305 struct type *arraytype
1306 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1307 return allocate_value (arraytype);
1308 }
1309}
1310
f10522c0
TT
1311namespace expr
1312{
575cae23 1313
e4407a20 1314struct value *
f10522c0
TT
1315rust_struct_anon::evaluate (struct type *expect_type,
1316 struct expression *exp,
1317 enum noside noside)
575cae23 1318{
f10522c0
TT
1319 value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1320 int field_number = std::get<0> (m_storage);
1321
575cae23
TT
1322 struct type *type = value_type (lhs);
1323
1324 if (type->code () == TYPE_CODE_STRUCT)
1325 {
1326 struct type *outer_type = NULL;
1327
1328 if (rust_enum_p (type))
1329 {
46680d22
SM
1330 type = resolve_dynamic_type (type, value_contents (lhs),
1331 value_address (lhs));
575cae23
TT
1332
1333 if (rust_empty_enum_p (type))
1334 error (_("Cannot access field %d of empty enum %s"),
1335 field_number, type->name ());
1336
1337 int fieldno = rust_enum_variant (type);
1338 lhs = value_primitive_field (lhs, 0, fieldno, type);
1339 outer_type = type;
1340 type = value_type (lhs);
1341 }
1342
1343 /* Tuples and tuple structs */
1344 int nfields = type->num_fields ();
1345
1346 if (field_number >= nfields || field_number < 0)
1347 {
1348 if (outer_type != NULL)
1349 error(_("Cannot access field %d of variant %s::%s, "
1350 "there are only %d fields"),
1351 field_number, outer_type->name (),
1352 rust_last_path_segment (type->name ()),
1353 nfields);
1354 else
1355 error(_("Cannot access field %d of %s, "
1356 "there are only %d fields"),
1357 field_number, type->name (), nfields);
1358 }
1359
1360 /* Tuples are tuple structs too. */
1361 if (!rust_tuple_struct_type_p (type))
1362 {
1363 if (outer_type != NULL)
1364 error(_("Variant %s::%s is not a tuple variant"),
1365 outer_type->name (),
1366 rust_last_path_segment (type->name ()));
1367 else
1368 error(_("Attempting to access anonymous field %d "
1369 "of %s, which is not a tuple, tuple struct, or "
1370 "tuple-like variant"),
1371 field_number, type->name ());
1372 }
1373
1374 return value_primitive_field (lhs, 0, field_number, type);
1375 }
1376 else
1377 error(_("Anonymous field access is only allowed on tuples, \
1378tuple structs, and tuple-like enum variants"));
1379}
1380
e4407a20 1381struct value *
f10522c0
TT
1382rust_structop::evaluate (struct type *expect_type,
1383 struct expression *exp,
1384 enum noside noside)
1fa41fc7 1385{
f10522c0
TT
1386 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1387 const char *field_name = std::get<1> (m_storage).c_str ();
1388
1fa41fc7
TT
1389 struct value *result;
1390 struct type *type = value_type (lhs);
1391 if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
1392 {
46680d22
SM
1393 type = resolve_dynamic_type (type, value_contents (lhs),
1394 value_address (lhs));
1fa41fc7
TT
1395
1396 if (rust_empty_enum_p (type))
1397 error (_("Cannot access field %s of empty enum %s"),
1398 field_name, type->name ());
1399
1400 int fieldno = rust_enum_variant (type);
1401 lhs = value_primitive_field (lhs, 0, fieldno, type);
1402
1403 struct type *outer_type = type;
1404 type = value_type (lhs);
1405 if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
1406 error (_("Attempting to access named field %s of tuple "
1407 "variant %s::%s, which has only anonymous fields"),
1408 field_name, outer_type->name (),
1409 rust_last_path_segment (type->name ()));
1410
1411 try
1412 {
158cc4fe 1413 result = value_struct_elt (&lhs, {}, field_name,
1fa41fc7
TT
1414 NULL, "structure");
1415 }
1416 catch (const gdb_exception_error &except)
1417 {
1418 error (_("Could not find field %s of struct variant %s::%s"),
1419 field_name, outer_type->name (),
1420 rust_last_path_segment (type->name ()));
1421 }
1422 }
1423 else
158cc4fe 1424 result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
1fa41fc7
TT
1425 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1426 result = value_zero (value_type (result), VALUE_LVAL (result));
1427 return result;
1428}
1429
5947d337
TT
1430value *
1431rust_aggregate_operation::evaluate (struct type *expect_type,
1432 struct expression *exp,
1433 enum noside noside)
1434{
1435 struct type *type = std::get<0> (m_storage);
1436 CORE_ADDR addr = 0;
1437 struct value *addrval = NULL;
1438 value *result;
1439
1440 if (noside == EVAL_NORMAL)
1441 {
1442 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1443 addr = value_as_long (addrval);
1444 result = value_at_lazy (type, addr);
1445 }
1446
1447 if (std::get<1> (m_storage) != nullptr)
1448 {
1449 struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp,
1450 noside);
1451
1452 if (noside == EVAL_NORMAL)
1453 {
1454 /* This isn't quite right but will do for the time
1455 being, seeing that we can't implement the Copy
1456 trait anyway. */
1457 value_assign (result, init);
1458 }
1459 }
1460
1461 for (const auto &item : std::get<2> (m_storage))
1462 {
1463 value *val = item.second->evaluate (nullptr, exp, noside);
1464 if (noside == EVAL_NORMAL)
1465 {
1466 const char *fieldname = item.first.c_str ();
158cc4fe 1467 value *field = value_struct_elt (&result, {}, fieldname,
5947d337
TT
1468 nullptr, "structure");
1469 value_assign (field, val);
1470 }
1471 }
1472
aa1da9ed 1473 if (noside == EVAL_AVOID_SIDE_EFFECTS)
5947d337
TT
1474 result = allocate_value (type);
1475 else
1476 result = value_at_lazy (type, addr);
1477
1478 return result;
1479}
1480
638fd74a
TT
1481value *
1482rust_structop::evaluate_funcall (struct type *expect_type,
1483 struct expression *exp,
1484 enum noside noside,
1485 const std::vector<operation_up> &ops)
1486{
1487 std::vector<struct value *> args (ops.size () + 1);
1488
1489 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1490 type in order to look up the method. */
1491 args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1492 /* We don't yet implement real Deref semantics. */
1493 while (value_type (args[0])->code () == TYPE_CODE_PTR)
1494 args[0] = value_ind (args[0]);
1495
1496 struct type *type = value_type (args[0]);
1497 if ((type->code () != TYPE_CODE_STRUCT
1498 && type->code () != TYPE_CODE_UNION
1499 && type->code () != TYPE_CODE_ENUM)
1500 || rust_tuple_type_p (type))
1501 error (_("Method calls only supported on struct or enum types"));
1502 if (type->name () == NULL)
1503 error (_("Method call on nameless type"));
1504
1505 std::string name = (std::string (type->name ()) + "::"
1506 + std::get<1> (m_storage));
1507
1508 const struct block *block = get_selected_block (0);
1509 struct block_symbol sym = lookup_symbol (name.c_str (), block,
1510 VAR_DOMAIN, NULL);
1511 if (sym.symbol == NULL)
1512 error (_("Could not find function named '%s'"), name.c_str ());
1513
5f9c5a63 1514 struct type *fn_type = sym.symbol->type ();
638fd74a
TT
1515 if (fn_type->num_fields () == 0)
1516 error (_("Function '%s' takes no arguments"), name.c_str ());
1517
1518 if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR)
1519 args[0] = value_addr (args[0]);
1520
1521 value *function = address_of_variable (sym.symbol, block);
1522
1523 for (int i = 0; i < ops.size (); ++i)
1524 args[i + 1] = ops[i]->evaluate (nullptr, exp, noside);
1525
1526 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1527 return value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1528 return call_function_by_hand (function, NULL, args);
1529}
1530
5947d337
TT
1531}
1532
c44af4eb
TT
1533\f
1534
1c485265 1535/* See language.h. */
0874fd07 1536
1c485265
AB
1537void
1538rust_language::language_arch_info (struct gdbarch *gdbarch,
1539 struct language_arch_info *lai) const
0874fd07 1540{
1c485265 1541 const struct builtin_type *builtin = builtin_type (gdbarch);
87afa652 1542
1c485265
AB
1543 /* Helper function to allow shorter lines below. */
1544 auto add = [&] (struct type * t) -> struct type *
87afa652 1545 {
1c485265
AB
1546 lai->add_primitive_type (t);
1547 return t;
1548 };
1549
1550 struct type *bool_type
1551 = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
1552 add (arch_character_type (gdbarch, 32, 1, "char"));
1553 add (arch_integer_type (gdbarch, 8, 0, "i8"));
1554 struct type *u8_type
1555 = add (arch_integer_type (gdbarch, 8, 1, "u8"));
1556 add (arch_integer_type (gdbarch, 16, 0, "i16"));
1557 add (arch_integer_type (gdbarch, 16, 1, "u16"));
1558 add (arch_integer_type (gdbarch, 32, 0, "i32"));
1559 add (arch_integer_type (gdbarch, 32, 1, "u32"));
1560 add (arch_integer_type (gdbarch, 64, 0, "i64"));
1561 add (arch_integer_type (gdbarch, 64, 1, "u64"));
1562
1563 unsigned int length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1564 add (arch_integer_type (gdbarch, length, 0, "isize"));
1565 struct type *usize_type
1566 = add (arch_integer_type (gdbarch, length, 1, "usize"));
1567
1568 add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
1569 add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
1570 add (arch_integer_type (gdbarch, 0, 1, "()"));
1571
1572 struct type *tem = make_cv_type (1, 0, u8_type, NULL);
1573 add (rust_slice_type ("&str", tem, usize_type));
1574
1575 lai->set_bool_type (bool_type);
1576 lai->set_string_char_type (u8_type);
1577}
39e7ecca 1578
1c485265 1579/* See language.h. */
efdf6a73 1580
1c485265
AB
1581void
1582rust_language::print_type (struct type *type, const char *varstring,
1583 struct ui_file *stream, int show, int level,
1584 const struct type_print_options *flags) const
1585{
fbb46296 1586 print_offset_data podata (flags);
1c485265
AB
1587 rust_internal_print_type (type, varstring, stream, show, level,
1588 flags, false, &podata);
1589}
efdf6a73 1590
1c485265 1591/* See language.h. */
5aba6ebe 1592
1c485265
AB
1593void
1594rust_language::emitchar (int ch, struct type *chtype,
1595 struct ui_file *stream, int quoter) const
1596{
1597 if (!rust_chartype_p (chtype))
1598 generic_emit_char (ch, chtype, stream, quoter,
8ee511af 1599 target_charset (chtype->arch ()));
1c485265
AB
1600 else if (ch == '\\' || ch == quoter)
1601 fprintf_filtered (stream, "\\%c", ch);
1602 else if (ch == '\n')
0426ad51 1603 gdb_puts ("\\n", stream);
1c485265 1604 else if (ch == '\r')
0426ad51 1605 gdb_puts ("\\r", stream);
1c485265 1606 else if (ch == '\t')
0426ad51 1607 gdb_puts ("\\t", stream);
1c485265 1608 else if (ch == '\0')
0426ad51 1609 gdb_puts ("\\0", stream);
1c485265
AB
1610 else if (ch >= 32 && ch <= 127 && isprint (ch))
1611 fputc_filtered (ch, stream);
1612 else if (ch <= 255)
1613 fprintf_filtered (stream, "\\x%02x", ch);
1614 else
1615 fprintf_filtered (stream, "\\u{%06x}", ch);
1616}
5aba6ebe 1617
1c485265 1618/* See language.h. */
b7c6e27d 1619
1c485265
AB
1620bool
1621rust_language::is_string_type_p (struct type *type) const
1622{
1623 LONGEST low_bound, high_bound;
b7c6e27d 1624
1c485265
AB
1625 type = check_typedef (type);
1626 return ((type->code () == TYPE_CODE_STRING)
1627 || (type->code () == TYPE_CODE_PTR
1628 && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
1629 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
1630 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
1631 &high_bound)))
1632 || (type->code () == TYPE_CODE_STRUCT
1633 && !rust_enum_p (type)
1634 && rust_slice_type_p (type)
1635 && strcmp (type->name (), "&str") == 0));
1636}
0874fd07
AB
1637
1638/* Single instance of the Rust language class. */
1639
1640static rust_language rust_language_defn;