]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rust-lang.c
Remove some unused variables
[thirdparty/binutils-gdb.git] / gdb / rust-lang.c
CommitLineData
c44af4eb
TT
1/* Rust language support routines for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 2016-2017 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
TT
33#include "rust-lang.h"
34#include "valprint.h"
35#include "varobj.h"
ab8b80a8
TT
36#include <string>
37#include <vector>
c44af4eb 38
c44af4eb
TT
39/* Returns the last segment of a Rust path like foo::bar::baz. Will
40 not handle cases where the last segment contains generics. This
41 will return NULL if the last segment cannot be found. */
42
43static const char *
44rust_last_path_segment (const char * path)
45{
46 const char *result = strrchr (path, ':');
47
48 if (result == NULL)
49 return NULL;
50 return result + 1;
51}
52
03c85b11 53/* See rust-lang.h. */
c44af4eb 54
03c85b11 55std::string
c44af4eb
TT
56rust_crate_for_block (const struct block *block)
57{
58 const char *scope = block_scope (block);
59
60 if (scope[0] == '\0')
03c85b11 61 return std::string ();
c44af4eb 62
03c85b11 63 return std::string (scope, cp_find_first_component (scope));
c44af4eb
TT
64}
65
66/* Information about the discriminant/variant of an enum */
67
68struct disr_info
69{
5698084a
PA
70 /* Name of field. */
71 std::string name;
c44af4eb
TT
72 /* Field number in union. Negative on error. For an encoded enum,
73 the "hidden" member will always be field 1, and the "real" member
74 will always be field 0. */
75 int field_no;
76 /* True if this is an encoded enum that has a single "real" member
77 and a single "hidden" member. */
78 unsigned int is_encoded : 1;
79};
80
81/* The prefix of a specially-encoded enum. */
82
83#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
84
85/* The number of the real field. */
86
87#define RUST_ENCODED_ENUM_REAL 0
88
89/* The number of the hidden field. */
90
91#define RUST_ENCODED_ENUM_HIDDEN 1
92
b96645f1
MG
93/* Whether or not a TYPE_CODE_UNION value is an untagged union
94 as opposed to being a regular Rust enum. */
95static bool
96rust_union_is_untagged (struct type *type)
97{
98 /* Unions must have at least one field. */
99 if (TYPE_NFIELDS (type) == 0)
100 return false;
101 /* If the first field is named, but the name has the rust enum prefix,
b50f188d 102 it is an enum. */
b96645f1
MG
103 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
104 strlen (RUST_ENUM_PREFIX)) == 0)
105 return false;
106 /* Unions only have named fields. */
107 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
108 {
109 if (strlen (TYPE_FIELD_NAME (type, i)) == 0)
110 return false;
111 }
112 return true;
113}
114
c44af4eb
TT
115/* Utility function to get discriminant info for a given value. */
116
117static struct disr_info
118rust_get_disr_info (struct type *type, const gdb_byte *valaddr,
119 int embedded_offset, CORE_ADDR address,
e8b24d9f 120 struct value *val)
c44af4eb
TT
121{
122 int i;
123 struct disr_info ret;
124 struct type *disr_type;
c44af4eb 125 struct value_print_options opts;
c44af4eb
TT
126 const char *name_segment;
127
128 get_no_prettyformat_print_options (&opts);
129
130 ret.field_no = -1;
131 ret.is_encoded = 0;
132
133 if (TYPE_NFIELDS (type) == 0)
134 error (_("Encountered void enum value"));
135
136 /* If an enum has two values where one is empty and the other holds
137 a pointer that cannot be zero; then the Rust compiler optimizes
138 away the discriminant and instead uses a zero value in the
139 pointer field to indicate the empty variant. */
140 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
141 strlen (RUST_ENUM_PREFIX)) == 0)
142 {
8001f118 143 char *tail, *token, *saveptr = NULL;
c44af4eb
TT
144 unsigned long fieldno;
145 struct type *member_type;
146 LONGEST value;
147
148 ret.is_encoded = 1;
149
150 if (TYPE_NFIELDS (type) != 1)
151 error (_("Only expected one field in %s type"), RUST_ENUM_PREFIX);
152
b5a4b3c5
MG
153 /* Optimized enums have only one field. */
154 member_type = TYPE_FIELD_TYPE (type, 0);
155
03c85b11
TT
156 std::string name (TYPE_FIELD_NAME (type, 0));
157 tail = &name[0] + strlen (RUST_ENUM_PREFIX);
b5a4b3c5
MG
158
159 /* The location of the value that doubles as a discriminant is
160 stored in the name of the field, as
161 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
162 where the fieldnos are the indices of the fields that should be
163 traversed in order to find the field (which may be several fields deep)
164 and the variantname is the name of the variant of the case when the
165 field is zero. */
a405c228
MG
166 for (token = strtok_r (tail, "$", &saveptr);
167 token != NULL;
168 token = strtok_r (NULL, "$", &saveptr))
b5a4b3c5 169 {
9e8a8ea8
TT
170 if (sscanf (token, "%lu", &fieldno) != 1)
171 {
172 /* We have reached the enum name, which cannot start
173 with a digit. */
174 break;
175 }
b5a4b3c5 176 if (fieldno >= TYPE_NFIELDS (member_type))
9e8a8ea8
TT
177 error (_("%s refers to field after end of member type"),
178 RUST_ENUM_PREFIX);
b5a4b3c5
MG
179
180 embedded_offset += TYPE_FIELD_BITPOS (member_type, fieldno) / 8;
181 member_type = TYPE_FIELD_TYPE (member_type, fieldno);
182 }
183
a405c228 184 if (token == NULL)
c44af4eb 185 error (_("Invalid form for %s"), RUST_ENUM_PREFIX);
9e8a8ea8 186 value = unpack_long (member_type, valaddr + embedded_offset);
c44af4eb 187
c44af4eb
TT
188 if (value == 0)
189 {
190 ret.field_no = RUST_ENCODED_ENUM_HIDDEN;
5698084a 191 ret.name = std::string (TYPE_NAME (type)) + "::" + token;
c44af4eb
TT
192 }
193 else
194 {
195 ret.field_no = RUST_ENCODED_ENUM_REAL;
5698084a
PA
196 ret.name = (std::string (TYPE_NAME (type)) + "::"
197 + rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 0))));
c44af4eb
TT
198 }
199
200 return ret;
201 }
202
203 disr_type = TYPE_FIELD_TYPE (type, 0);
204
205 if (TYPE_NFIELDS (disr_type) == 0)
206 {
207 /* This is a bounds check and should never be hit unless Rust
208 has changed its debuginfo format. */
209 error (_("Could not find enum discriminant field"));
210 }
51a789c3
MG
211 else if (TYPE_NFIELDS (type) == 1)
212 {
213 /* Sometimes univariant enums are encoded without a
214 discriminant. In that case, treating it as an encoded enum
215 with the first field being the actual type works. */
216 const char *field_name = TYPE_NAME (TYPE_FIELD_TYPE (type, 0));
217 const char *last = rust_last_path_segment (field_name);
d93f4d96 218 ret.name = std::string (TYPE_NAME (type)) + "::" + last;
51a789c3
MG
219 ret.field_no = RUST_ENCODED_ENUM_REAL;
220 ret.is_encoded = 1;
221 return ret;
222 }
c44af4eb
TT
223
224 if (strcmp (TYPE_FIELD_NAME (disr_type, 0), "RUST$ENUM$DISR") != 0)
225 error (_("Rust debug format has changed"));
226
d7e74731 227 string_file temp_file;
c44af4eb
TT
228 /* The first value of the first field (or any field)
229 is the discriminant value. */
e8b24d9f 230 c_val_print (TYPE_FIELD_TYPE (disr_type, 0),
c44af4eb
TT
231 (embedded_offset + TYPE_FIELD_BITPOS (type, 0) / 8
232 + TYPE_FIELD_BITPOS (disr_type, 0) / 8),
d7e74731 233 address, &temp_file,
c44af4eb
TT
234 0, val, &opts);
235
d7e74731 236 ret.name = std::move (temp_file.string ());
5698084a 237 name_segment = rust_last_path_segment (ret.name.c_str ());
c44af4eb
TT
238 if (name_segment != NULL)
239 {
240 for (i = 0; i < TYPE_NFIELDS (type); ++i)
241 {
242 /* Sadly, the discriminant value paths do not match the type
243 field name paths ('core::option::Option::Some' vs
244 'core::option::Some'). However, enum variant names are
245 unique in the last path segment and the generics are not
246 part of this path, so we can just compare those. This is
247 hackish and would be better fixed by improving rustc's
248 metadata for enums. */
249 const char *field_type = TYPE_NAME (TYPE_FIELD_TYPE (type, i));
250
251 if (field_type != NULL
252 && strcmp (name_segment,
253 rust_last_path_segment (field_type)) == 0)
254 {
255 ret.field_no = i;
256 break;
257 }
258 }
259 }
260
5698084a 261 if (ret.field_no == -1 && !ret.name.empty ())
c44af4eb
TT
262 {
263 /* Somehow the discriminant wasn't found. */
c44af4eb 264 error (_("Could not find variant of %s with discriminant %s"),
5698084a 265 TYPE_TAG_NAME (type), ret.name.c_str ());
c44af4eb
TT
266 }
267
c44af4eb
TT
268 return ret;
269}
270
271/* See rust-lang.h. */
272
65c40c95 273bool
c44af4eb
TT
274rust_tuple_type_p (struct type *type)
275{
276 /* The current implementation is a bit of a hack, but there's
277 nothing else in the debuginfo to distinguish a tuple from a
278 struct. */
279 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
280 && TYPE_TAG_NAME (type) != NULL
281 && TYPE_TAG_NAME (type)[0] == '(');
282}
283
284
285/* Return true if all non-static fields of a structlike type are in a
286 sequence like __0, __1, __2. OFFSET lets us skip fields. */
287
65c40c95 288static bool
c44af4eb
TT
289rust_underscore_fields (struct type *type, int offset)
290{
291 int i, field_number;
292
293 field_number = 0;
294
295 if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
65c40c95 296 return false;
c44af4eb
TT
297 for (i = 0; i < TYPE_NFIELDS (type); ++i)
298 {
299 if (!field_is_static (&TYPE_FIELD (type, i)))
300 {
301 if (offset > 0)
302 offset--;
303 else
304 {
305 char buf[20];
306
307 xsnprintf (buf, sizeof (buf), "__%d", field_number);
308 if (strcmp (buf, TYPE_FIELD_NAME (type, i)) != 0)
65c40c95 309 return false;
c44af4eb
TT
310 field_number++;
311 }
312 }
313 }
65c40c95 314 return true;
c44af4eb
TT
315}
316
317/* See rust-lang.h. */
318
65c40c95 319bool
c44af4eb
TT
320rust_tuple_struct_type_p (struct type *type)
321{
12df5c00
TT
322 /* This is just an approximation until DWARF can represent Rust more
323 precisely. We exclude zero-length structs because they may not
324 be tuple structs, and there's no way to tell. */
325 return TYPE_NFIELDS (type) > 0 && rust_underscore_fields (type, 0);
c44af4eb
TT
326}
327
328/* Return true if a variant TYPE is a tuple variant, false otherwise. */
329
65c40c95 330static bool
c44af4eb
TT
331rust_tuple_variant_type_p (struct type *type)
332{
333 /* First field is discriminant */
334 return rust_underscore_fields (type, 1);
335}
336
337/* Return true if TYPE is a slice type, otherwise false. */
338
65c40c95 339static bool
c44af4eb
TT
340rust_slice_type_p (struct type *type)
341{
342 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
343 && TYPE_TAG_NAME (type) != NULL
01af5e0d
TT
344 && (strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0
345 || strcmp (TYPE_TAG_NAME (type), "&str") == 0));
c44af4eb
TT
346}
347
348/* Return true if TYPE is a range type, otherwise false. */
349
65c40c95 350static bool
c44af4eb
TT
351rust_range_type_p (struct type *type)
352{
353 int i;
354
355 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
356 || TYPE_NFIELDS (type) > 2
357 || TYPE_TAG_NAME (type) == NULL
358 || strstr (TYPE_TAG_NAME (type), "::Range") == NULL)
65c40c95 359 return false;
c44af4eb
TT
360
361 if (TYPE_NFIELDS (type) == 0)
65c40c95 362 return true;
c44af4eb
TT
363
364 i = 0;
365 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
366 {
367 if (TYPE_NFIELDS (type) == 1)
65c40c95 368 return true;
c44af4eb
TT
369 i = 1;
370 }
371 else if (TYPE_NFIELDS (type) == 2)
372 {
373 /* First field had to be "start". */
65c40c95 374 return false;
c44af4eb
TT
375 }
376
377 return strcmp (TYPE_FIELD_NAME (type, i), "end") == 0;
378}
379
380/* Return true if TYPE seems to be the type "u8", otherwise false. */
381
65c40c95 382static bool
c44af4eb
TT
383rust_u8_type_p (struct type *type)
384{
385 return (TYPE_CODE (type) == TYPE_CODE_INT
386 && TYPE_UNSIGNED (type)
387 && TYPE_LENGTH (type) == 1);
388}
389
390/* Return true if TYPE is a Rust character type. */
391
65c40c95 392static bool
c44af4eb
TT
393rust_chartype_p (struct type *type)
394{
395 return (TYPE_CODE (type) == TYPE_CODE_CHAR
396 && TYPE_LENGTH (type) == 4
397 && TYPE_UNSIGNED (type));
398}
399
71a3c369
TT
400/* If VALUE represents a trait object pointer, return the underlying
401 pointer with the correct (i.e., runtime) type. Otherwise, return
402 NULL. */
403
404static struct value *
405rust_get_trait_object_pointer (struct value *value)
406{
407 struct type *type = check_typedef (value_type (value));
408
409 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
410 return NULL;
411
412 /* Try to be a bit resilient if the ABI changes. */
413 int vtable_field = 0;
414 for (int i = 0; i < 2; ++i)
415 {
416 if (strcmp (TYPE_FIELD_NAME (type, i), "vtable") == 0)
417 vtable_field = i;
418 else if (strcmp (TYPE_FIELD_NAME (type, i), "pointer") != 0)
419 return NULL;
420 }
421
422 CORE_ADDR vtable = value_as_address (value_field (value, vtable_field));
423 struct symbol *symbol = find_symbol_at_address (vtable);
cf724bc9 424 if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE)
71a3c369
TT
425 return NULL;
426
427 struct rust_vtable_symbol *vtable_sym
428 = static_cast<struct rust_vtable_symbol *> (symbol);
429 struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type);
430 return value_cast (pointer_type, value_field (value, 1 - vtable_field));
431}
432
c44af4eb
TT
433\f
434
435/* la_emitchar implementation for Rust. */
436
437static void
438rust_emitchar (int c, struct type *type, struct ui_file *stream, int quoter)
439{
440 if (!rust_chartype_p (type))
441 generic_emit_char (c, type, stream, quoter,
442 target_charset (get_type_arch (type)));
443 else if (c == '\\' || c == quoter)
444 fprintf_filtered (stream, "\\%c", c);
445 else if (c == '\n')
446 fputs_filtered ("\\n", stream);
447 else if (c == '\r')
448 fputs_filtered ("\\r", stream);
449 else if (c == '\t')
450 fputs_filtered ("\\t", stream);
451 else if (c == '\0')
452 fputs_filtered ("\\0", stream);
453 else if (c >= 32 && c <= 127 && isprint (c))
454 fputc_filtered (c, stream);
455 else if (c <= 255)
456 fprintf_filtered (stream, "\\x%02x", c);
457 else
458 fprintf_filtered (stream, "\\u{%06x}", c);
459}
460
461/* la_printchar implementation for Rust. */
462
463static void
464rust_printchar (int c, struct type *type, struct ui_file *stream)
465{
466 fputs_filtered ("'", stream);
467 LA_EMIT_CHAR (c, type, stream, '\'');
468 fputs_filtered ("'", stream);
469}
470
471/* la_printstr implementation for Rust. */
472
473static void
474rust_printstr (struct ui_file *stream, struct type *type,
475 const gdb_byte *string, unsigned int length,
476 const char *user_encoding, int force_ellipses,
477 const struct value_print_options *options)
478{
479 /* Rust always uses UTF-8, but let the caller override this if need
480 be. */
481 const char *encoding = user_encoding;
482 if (user_encoding == NULL || !*user_encoding)
483 {
484 /* In Rust strings, characters are "u8". */
485 if (rust_u8_type_p (type))
486 encoding = "UTF-8";
487 else
488 {
489 /* This is probably some C string, so let's let C deal with
490 it. */
491 c_printstr (stream, type, string, length, user_encoding,
492 force_ellipses, options);
493 return;
494 }
495 }
496
497 /* This is not ideal as it doesn't use our character printer. */
498 generic_printstr (stream, type, string, length, encoding, force_ellipses,
499 '"', 0, options);
500}
501
502\f
503
45320ffa
TT
504/* Helper function to print a string slice. */
505
506static void
507rust_val_print_str (struct ui_file *stream, struct value *val,
508 const struct value_print_options *options)
509{
510 struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
511 "slice");
512 struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
513
514 val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
515 value_as_address (base), value_as_long (len), stream,
516 options);
517}
518
b96645f1
MG
519/* rust_print_type branch for structs and untagged unions. */
520
521static void
e8b24d9f
YQ
522val_print_struct (struct type *type, int embedded_offset,
523 CORE_ADDR address, struct ui_file *stream,
524 int recurse, struct value *val,
20955dbf 525 const struct value_print_options *options)
b96645f1
MG
526{
527 int i;
528 int first_field;
45320ffa
TT
529
530 if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
531 {
532 rust_val_print_str (stream, val, options);
533 return;
534 }
535
65c40c95
TT
536 bool is_tuple = rust_tuple_type_p (type);
537 bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
b96645f1
MG
538 struct value_print_options opts;
539
540 if (!is_tuple)
541 {
542 if (TYPE_TAG_NAME (type) != NULL)
543 fprintf_filtered (stream, "%s", TYPE_TAG_NAME (type));
544
545 if (TYPE_NFIELDS (type) == 0)
546 return;
547
548 if (TYPE_TAG_NAME (type) != NULL)
549 fputs_filtered (" ", stream);
550 }
551
552 if (is_tuple || is_tuple_struct)
553 fputs_filtered ("(", stream);
554 else
555 fputs_filtered ("{", stream);
556
557 opts = *options;
558 opts.deref_ref = 0;
559
560 first_field = 1;
561 for (i = 0; i < TYPE_NFIELDS (type); ++i)
562 {
563 if (field_is_static (&TYPE_FIELD (type, i)))
564 continue;
565
566 if (!first_field)
567 fputs_filtered (",", stream);
568
569 if (options->prettyformat)
570 {
b50f188d
TT
571 fputs_filtered ("\n", stream);
572 print_spaces_filtered (2 + 2 * recurse, stream);
b96645f1
MG
573 }
574 else if (!first_field)
575 fputs_filtered (" ", stream);
576
577 first_field = 0;
578
579 if (!is_tuple && !is_tuple_struct)
580 {
b50f188d
TT
581 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
582 fputs_filtered (": ", stream);
b96645f1
MG
583 }
584
585 val_print (TYPE_FIELD_TYPE (type, i),
b50f188d
TT
586 embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
587 address,
588 stream, recurse + 1, val, &opts,
589 current_language);
b96645f1
MG
590 }
591
592 if (options->prettyformat)
593 {
594 fputs_filtered ("\n", stream);
595 print_spaces_filtered (2 * recurse, stream);
596 }
597
598 if (is_tuple || is_tuple_struct)
599 fputs_filtered (")", stream);
600 else
601 fputs_filtered ("}", stream);
602}
603
c44af4eb
TT
604static const struct generic_val_print_decorations rust_decorations =
605{
606 /* Complex isn't used in Rust, but we provide C-ish values just in
607 case. */
608 "",
609 " + ",
610 " * I",
611 "true",
612 "false",
921d8f54 613 "()",
c44af4eb
TT
614 "[",
615 "]"
616};
617
618/* la_val_print implementation for Rust. */
619
620static void
e8b24d9f 621rust_val_print (struct type *type, int embedded_offset,
c44af4eb 622 CORE_ADDR address, struct ui_file *stream, int recurse,
e8b24d9f 623 struct value *val,
c44af4eb
TT
624 const struct value_print_options *options)
625{
e8b24d9f
YQ
626 const gdb_byte *valaddr = value_contents_for_printing (val);
627
c44af4eb
TT
628 type = check_typedef (type);
629 switch (TYPE_CODE (type))
630 {
631 case TYPE_CODE_PTR:
632 {
633 LONGEST low_bound, high_bound;
634
635 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
636 && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
637 && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
638 &high_bound)) {
639 /* We have a pointer to a byte string, so just print
640 that. */
641 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
642 CORE_ADDR addr;
643 struct gdbarch *arch = get_type_arch (type);
644 int unit_size = gdbarch_addressable_memory_unit_size (arch);
645
646 addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
647 if (options->addressprint)
648 {
649 fputs_filtered (paddress (arch, addr), stream);
650 fputs_filtered (" ", stream);
651 }
652
653 fputs_filtered ("b", stream);
654 val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
655 high_bound - low_bound + 1, stream,
656 options);
657 break;
658 }
659 }
660 /* Fall through. */
661
662 case TYPE_CODE_METHODPTR:
663 case TYPE_CODE_MEMBERPTR:
e8b24d9f 664 c_val_print (type, embedded_offset, address, stream,
c44af4eb
TT
665 recurse, val, options);
666 break;
667
668 case TYPE_CODE_INT:
669 /* Recognize the unit type. */
670 if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0
671 && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0)
672 {
673 fputs_filtered ("()", stream);
674 break;
675 }
676 goto generic_print;
677
678 case TYPE_CODE_STRING:
679 {
680 struct gdbarch *arch = get_type_arch (type);
681 int unit_size = gdbarch_addressable_memory_unit_size (arch);
682 LONGEST low_bound, high_bound;
683
684 if (!get_array_bounds (type, &low_bound, &high_bound))
685 error (_("Could not determine the array bounds"));
686
687 /* If we see a plain TYPE_CODE_STRING, then we're printing a
688 byte string, hence the choice of "ASCII" as the
689 encoding. */
690 fputs_filtered ("b", stream);
691 rust_printstr (stream, TYPE_TARGET_TYPE (type),
692 valaddr + embedded_offset * unit_size,
693 high_bound - low_bound + 1, "ASCII", 0, options);
694 }
695 break;
696
697 case TYPE_CODE_ARRAY:
698 {
699 LONGEST low_bound, high_bound;
700
701 if (get_array_bounds (type, &low_bound, &high_bound)
702 && high_bound - low_bound + 1 == 0)
703 fputs_filtered ("[]", stream);
704 else
705 goto generic_print;
706 }
707 break;
708
709 case TYPE_CODE_UNION:
710 {
711 int j, nfields, first_field, is_tuple, start;
712 struct type *variant_type;
713 struct disr_info disr;
714 struct value_print_options opts;
c44af4eb 715
b50f188d
TT
716 /* Untagged unions are printed as if they are structs.
717 Since the field bit positions overlap in the debuginfo,
718 the code for printing a union is same as that for a struct,
719 the only difference is that the input type will have overlapping
720 fields. */
721 if (rust_union_is_untagged (type))
722 {
723 val_print_struct (type, embedded_offset, address, stream,
724 recurse, val, options);
725 break;
726 }
b96645f1 727
c44af4eb
TT
728 opts = *options;
729 opts.deref_ref = 0;
730
731 disr = rust_get_disr_info (type, valaddr, embedded_offset, address,
732 val);
c44af4eb
TT
733
734 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
735 {
5698084a
PA
736 fprintf_filtered (stream, "%s", disr.name.c_str ());
737 break;
c44af4eb
TT
738 }
739
740 first_field = 1;
741 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
742 nfields = TYPE_NFIELDS (variant_type);
743
744 is_tuple = (disr.is_encoded
745 ? rust_tuple_struct_type_p (variant_type)
746 : rust_tuple_variant_type_p (variant_type));
747 start = disr.is_encoded ? 0 : 1;
748
749 if (nfields > start)
750 {
751 /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */
752 if (is_tuple)
5698084a 753 fprintf_filtered (stream, "%s(", disr.name.c_str ());
c44af4eb
TT
754 else
755 {
756 /* struct variant. */
5698084a 757 fprintf_filtered (stream, "%s{", disr.name.c_str ());
c44af4eb
TT
758 }
759 }
760 else
761 {
762 /* In case of a nullary variant like 'None', just output
763 the name. */
5698084a
PA
764 fprintf_filtered (stream, "%s", disr.name.c_str ());
765 break;
c44af4eb
TT
766 }
767
768 for (j = start; j < TYPE_NFIELDS (variant_type); j++)
769 {
770 if (!first_field)
771 fputs_filtered (", ", stream);
772 first_field = 0;
773
774 if (!is_tuple)
775 fprintf_filtered (stream, "%s: ",
776 TYPE_FIELD_NAME (variant_type, j));
777
778 val_print (TYPE_FIELD_TYPE (variant_type, j),
c44af4eb
TT
779 (embedded_offset
780 + TYPE_FIELD_BITPOS (type, disr.field_no) / 8
781 + TYPE_FIELD_BITPOS (variant_type, j) / 8),
782 address,
783 stream, recurse + 1, val, &opts,
784 current_language);
785 }
786
787 if (is_tuple)
788 fputs_filtered (")", stream);
789 else
790 fputs_filtered ("}", stream);
c44af4eb
TT
791 }
792 break;
793
794 case TYPE_CODE_STRUCT:
e8b24d9f
YQ
795 val_print_struct (type, embedded_offset, address, stream,
796 recurse, val, options);
b96645f1 797 break;
c44af4eb 798
b96645f1
MG
799 default:
800 generic_print:
801 /* Nothing special yet. */
e8b24d9f 802 generic_val_print (type, embedded_offset, address, stream,
b96645f1
MG
803 recurse, val, options, &rust_decorations);
804 }
805}
c44af4eb 806
b96645f1 807\f
c44af4eb 808
b96645f1
MG
809static void
810rust_print_type (struct type *type, const char *varstring,
811 struct ui_file *stream, int show, int level,
812 const struct type_print_options *flags);
813
814/* Print a struct or union typedef. */
815static void
816rust_print_struct_def (struct type *type, const char *varstring,
b50f188d
TT
817 struct ui_file *stream, int show, int level,
818 const struct type_print_options *flags)
b96645f1 819{
65c40c95
TT
820 bool is_tuple_struct;
821 int i;
b96645f1 822
b50f188d
TT
823 /* Print a tuple type simply. */
824 if (rust_tuple_type_p (type))
825 {
826 fputs_filtered (TYPE_TAG_NAME (type), stream);
827 return;
828 }
c44af4eb 829
b50f188d
TT
830 /* If we see a base class, delegate to C. */
831 if (TYPE_N_BASECLASSES (type) > 0)
832 c_print_type (type, varstring, stream, show, level, flags);
c44af4eb 833
b96645f1
MG
834 /* This code path is also used by unions. */
835 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
b50f188d 836 fputs_filtered ("struct ", stream);
b96645f1 837 else
b50f188d 838 fputs_filtered ("union ", stream);
b96645f1 839
b50f188d
TT
840 if (TYPE_TAG_NAME (type) != NULL)
841 fputs_filtered (TYPE_TAG_NAME (type), stream);
b96645f1 842
b50f188d 843 is_tuple_struct = rust_tuple_struct_type_p (type);
b96645f1 844
b50f188d
TT
845 if (TYPE_NFIELDS (type) == 0 && !rust_tuple_type_p (type))
846 return;
847 fputs_filtered (is_tuple_struct ? " (\n" : " {\n", stream);
c44af4eb 848
b50f188d
TT
849 for (i = 0; i < TYPE_NFIELDS (type); ++i)
850 {
b50f188d
TT
851 QUIT;
852 if (field_is_static (&TYPE_FIELD (type, i)))
853 continue;
854
855 /* We'd like to print "pub" here as needed, but rustc
856 doesn't emit the debuginfo, and our types don't have
857 cplus_struct_type attached. */
858
859 /* For a tuple struct we print the type but nothing
860 else. */
861 print_spaces_filtered (level + 2, stream);
862 if (!is_tuple_struct)
863 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
864
865 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
866 stream, show - 1, level + 2,
867 flags);
868 fputs_filtered (",\n", stream);
869 }
c44af4eb 870
b50f188d 871 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
c44af4eb
TT
872}
873
c44af4eb
TT
874/* la_print_typedef implementation for Rust. */
875
876static void
877rust_print_typedef (struct type *type,
878 struct symbol *new_symbol,
879 struct ui_file *stream)
880{
881 type = check_typedef (type);
882 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
883 type_print (type, "", stream, 0);
884 fprintf_filtered (stream, ";\n");
885}
886
887/* la_print_type implementation for Rust. */
888
889static void
890rust_print_type (struct type *type, const char *varstring,
891 struct ui_file *stream, int show, int level,
892 const struct type_print_options *flags)
893{
894 int i;
895
896 QUIT;
897 if (show <= 0
898 && TYPE_NAME (type) != NULL)
899 {
921d8f54
MG
900 /* Rust calls the unit type "void" in its debuginfo,
901 but we don't want to print it as that. */
902 if (TYPE_CODE (type) == TYPE_CODE_VOID)
903 fputs_filtered ("()", stream);
904 else
905 fputs_filtered (TYPE_NAME (type), stream);
c44af4eb
TT
906 return;
907 }
908
909 type = check_typedef (type);
910 switch (TYPE_CODE (type))
911 {
921d8f54
MG
912 case TYPE_CODE_VOID:
913 fputs_filtered ("()", stream);
914 break;
915
c44af4eb
TT
916 case TYPE_CODE_FUNC:
917 /* Delegate varargs to the C printer. */
918 if (TYPE_VARARGS (type))
919 goto c_printer;
920
921 fputs_filtered ("fn ", stream);
922 if (varstring != NULL)
923 fputs_filtered (varstring, stream);
924 fputs_filtered ("(", stream);
925 for (i = 0; i < TYPE_NFIELDS (type); ++i)
926 {
927 QUIT;
928 if (i > 0)
929 fputs_filtered (", ", stream);
930 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
931 flags);
932 }
921d8f54
MG
933 fputs_filtered (")", stream);
934 /* If it returns unit, we can omit the return type. */
935 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
936 {
937 fputs_filtered (" -> ", stream);
938 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
939 }
c44af4eb
TT
940 break;
941
942 case TYPE_CODE_ARRAY:
943 {
944 LONGEST low_bound, high_bound;
945
946 fputs_filtered ("[", stream);
947 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
948 stream, show - 1, level, flags);
c44af4eb
TT
949
950 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
951 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
e6cf65f2 952 fprintf_filtered (stream, "; variable length");
c44af4eb 953 else if (get_array_bounds (type, &low_bound, &high_bound))
e6cf65f2 954 fprintf_filtered (stream, "; %s",
c44af4eb
TT
955 plongest (high_bound - low_bound + 1));
956 fputs_filtered ("]", stream);
957 }
958 break;
959
960 case TYPE_CODE_STRUCT:
b50f188d 961 rust_print_struct_def (type, varstring, stream, show, level, flags);
c44af4eb
TT
962 break;
963
964 case TYPE_CODE_ENUM:
965 {
966 int i, len = 0;
967
968 fputs_filtered ("enum ", stream);
969 if (TYPE_TAG_NAME (type) != NULL)
970 {
971 fputs_filtered (TYPE_TAG_NAME (type), stream);
972 fputs_filtered (" ", stream);
973 len = strlen (TYPE_TAG_NAME (type));
974 }
975 fputs_filtered ("{\n", stream);
976
977 for (i = 0; i < TYPE_NFIELDS (type); ++i)
978 {
979 const char *name = TYPE_FIELD_NAME (type, i);
980
981 QUIT;
982
983 if (len > 0
984 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
985 && name[len] == ':'
986 && name[len + 1] == ':')
987 name += len + 2;
988 fprintfi_filtered (level + 2, stream, "%s,\n", name);
989 }
990
991 fputs_filtered ("}", stream);
992 }
993 break;
994
995 case TYPE_CODE_UNION:
996 {
b5a4b3c5 997 /* ADT enums. */
798a7429 998 int i;
b5a4b3c5
MG
999 /* Skip the discriminant field. */
1000 int skip_to = 1;
c44af4eb 1001
b50f188d
TT
1002 /* Unions and structs have the same syntax in Rust,
1003 the only difference is that structs are declared with `struct`
1004 and union with `union`. This difference is handled in the struct
1005 printer. */
1006 if (rust_union_is_untagged (type))
1007 {
1008 rust_print_struct_def (type, varstring, stream, show, level, flags);
1009 break;
1010 }
b96645f1 1011
c44af4eb
TT
1012 fputs_filtered ("enum ", stream);
1013 if (TYPE_TAG_NAME (type) != NULL)
1014 {
1015 fputs_filtered (TYPE_TAG_NAME (type), stream);
1016 fputs_filtered (" ", stream);
c44af4eb 1017 }
b5a4b3c5
MG
1018 fputs_filtered ("{\n", stream);
1019
9e8a8ea8
TT
1020 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
1021 strlen (RUST_ENUM_PREFIX)) == 0)
1022 {
1023 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
1024 if (zero_field != NULL && strlen (zero_field) > 1)
1025 {
1026 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
1027 /* There is no explicit discriminant field, skip nothing. */
1028 skip_to = 0;
1029 }
1030 }
f0fd41c1
TT
1031 else if (TYPE_NFIELDS (type) == 1)
1032 skip_to = 0;
c44af4eb
TT
1033
1034 for (i = 0; i < TYPE_NFIELDS (type); ++i)
1035 {
1036 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
1037 const char *name
1038 = rust_last_path_segment (TYPE_NAME (variant_type));
1039
1040 fprintfi_filtered (level + 2, stream, "%s", name);
1041
b5a4b3c5 1042 if (TYPE_NFIELDS (variant_type) > skip_to)
c44af4eb
TT
1043 {
1044 int first = 1;
f0fd41c1
TT
1045 bool is_tuple = (TYPE_NFIELDS (type) == 1
1046 ? rust_tuple_struct_type_p (variant_type)
1047 : rust_tuple_variant_type_p (variant_type));
c44af4eb
TT
1048 int j;
1049
1050 fputs_filtered (is_tuple ? "(" : "{", stream);
b5a4b3c5 1051 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
c44af4eb
TT
1052 {
1053 if (first)
1054 first = 0;
1055 else
1056 fputs_filtered (", ", stream);
1057
1058 if (!is_tuple)
1059 fprintf_filtered (stream, "%s: ",
1060 TYPE_FIELD_NAME (variant_type, j));
1061
1062 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
1063 stream, show - 1, level + 2,
1064 flags);
1065 }
1066 fputs_filtered (is_tuple ? ")" : "}", stream);
1067 }
1068
1069 fputs_filtered (",\n", stream);
1070 }
1071
1072 fputs_filtered ("}", stream);
1073 }
1074 break;
1075
1076 default:
1077 c_printer:
1078 c_print_type (type, varstring, stream, show, level, flags);
1079 }
1080}
1081
1082\f
1083
1084/* Compute the alignment of the type T. */
1085
1086static int
1087rust_type_alignment (struct type *t)
1088{
1089 t = check_typedef (t);
1090 switch (TYPE_CODE (t))
1091 {
1092 default:
1093 error (_("Could not compute alignment of type"));
1094
1095 case TYPE_CODE_PTR:
1096 case TYPE_CODE_ENUM:
1097 case TYPE_CODE_INT:
1098 case TYPE_CODE_FLT:
1099 case TYPE_CODE_REF:
1100 case TYPE_CODE_CHAR:
1101 case TYPE_CODE_BOOL:
1102 return TYPE_LENGTH (t);
1103
1104 case TYPE_CODE_ARRAY:
1105 case TYPE_CODE_COMPLEX:
1106 return rust_type_alignment (TYPE_TARGET_TYPE (t));
1107
1108 case TYPE_CODE_STRUCT:
1109 case TYPE_CODE_UNION:
1110 {
1111 int i;
1112 int align = 1;
1113
1114 for (i = 0; i < TYPE_NFIELDS (t); ++i)
1115 {
1116 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
1117 if (a > align)
1118 align = a;
1119 }
1120 return align;
1121 }
1122 }
1123}
1124
1125/* Like arch_composite_type, but uses TYPE to decide how to allocate
1126 -- either on an obstack or on a gdbarch. */
1127
1128static struct type *
1129rust_composite_type (struct type *original,
1130 const char *name,
1131 const char *field1, struct type *type1,
1132 const char *field2, struct type *type2)
1133{
1134 struct type *result = alloc_type_copy (original);
1135 int i, nfields, bitpos;
1136
1137 nfields = 0;
1138 if (field1 != NULL)
1139 ++nfields;
1140 if (field2 != NULL)
1141 ++nfields;
1142
1143 TYPE_CODE (result) = TYPE_CODE_STRUCT;
1144 TYPE_NAME (result) = name;
1145 TYPE_TAG_NAME (result) = name;
1146
1147 TYPE_NFIELDS (result) = nfields;
1148 TYPE_FIELDS (result)
1149 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1150
1151 i = 0;
1152 bitpos = 0;
1153 if (field1 != NULL)
1154 {
1155 struct field *field = &TYPE_FIELD (result, i);
1156
1157 SET_FIELD_BITPOS (*field, bitpos);
1158 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1159
1160 FIELD_NAME (*field) = field1;
1161 FIELD_TYPE (*field) = type1;
1162 ++i;
1163 }
1164 if (field2 != NULL)
1165 {
1166 struct field *field = &TYPE_FIELD (result, i);
1167 int align = rust_type_alignment (type2);
1168
1169 if (align != 0)
1170 {
1171 int delta;
1172
1173 align *= TARGET_CHAR_BIT;
1174 delta = bitpos % align;
1175 if (delta != 0)
1176 bitpos += align - delta;
1177 }
1178 SET_FIELD_BITPOS (*field, bitpos);
1179
1180 FIELD_NAME (*field) = field2;
1181 FIELD_TYPE (*field) = type2;
1182 ++i;
1183 }
1184
1185 if (i > 0)
1186 TYPE_LENGTH (result)
1187 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1188 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1189 return result;
1190}
1191
1192/* See rust-lang.h. */
1193
1194struct type *
1195rust_slice_type (const char *name, struct type *elt_type,
1196 struct type *usize_type)
1197{
1198 struct type *type;
1199
1200 elt_type = lookup_pointer_type (elt_type);
1201 type = rust_composite_type (elt_type, name,
1202 "data_ptr", elt_type,
1203 "length", usize_type);
1204
1205 return type;
1206}
1207
1208enum rust_primitive_types
1209{
1210 rust_primitive_bool,
1211 rust_primitive_char,
1212 rust_primitive_i8,
1213 rust_primitive_u8,
1214 rust_primitive_i16,
1215 rust_primitive_u16,
1216 rust_primitive_i32,
1217 rust_primitive_u32,
1218 rust_primitive_i64,
1219 rust_primitive_u64,
1220 rust_primitive_isize,
1221 rust_primitive_usize,
1222 rust_primitive_f32,
1223 rust_primitive_f64,
1224 rust_primitive_unit,
1225 rust_primitive_str,
1226 nr_rust_primitive_types
1227};
1228
1229/* la_language_arch_info implementation for Rust. */
1230
1231static void
1232rust_language_arch_info (struct gdbarch *gdbarch,
1233 struct language_arch_info *lai)
1234{
1235 const struct builtin_type *builtin = builtin_type (gdbarch);
1236 struct type *tem;
1237 struct type **types;
1238 unsigned int length;
1239
1240 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1241 struct type *);
1242
1243 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1244 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1245 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1246 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1247 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1248 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1249 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1250 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1251 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1252 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1253
1254 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1255 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1256 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1257
49f190bc
UW
1258 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1259 floatformats_ieee_single);
1260 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1261 floatformats_ieee_double);
c44af4eb
TT
1262
1263 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1264
1265 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1266 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1267 types[rust_primitive_usize]);
1268
1269 lai->primitive_type_vector = types;
1270 lai->bool_type_default = types[rust_primitive_bool];
1271 lai->string_char_type = types[rust_primitive_u8];
1272}
1273
1274\f
1275
1276/* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1277
1278static struct value *
1279rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1280{
1281 int i;
1282 int num_args = exp->elts[*pos + 1].longconst;
1283 const char *method;
c44af4eb 1284 struct value *function, *result, *arg0;
c44af4eb
TT
1285 struct type *type, *fn_type;
1286 const struct block *block;
1287 struct block_symbol sym;
1288
1289 /* For an ordinary function call we can simply defer to the
1290 generic implementation. */
1291 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1292 return evaluate_subexp_standard (NULL, exp, pos, noside);
1293
1294 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1295 *pos += 4;
1296 method = &exp->elts[*pos + 1].string;
1297 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1298
1299 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1300 type in order to look up the method. */
1301 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1302
1303 if (noside == EVAL_SKIP)
1304 {
1305 for (i = 0; i < num_args; ++i)
1306 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1307 return arg0;
1308 }
1309
ab8b80a8 1310 std::vector<struct value *> args (num_args + 1);
c44af4eb
TT
1311 args[0] = arg0;
1312
1313 /* We don't yet implement real Deref semantics. */
1314 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1315 args[0] = value_ind (args[0]);
1316
1317 type = value_type (args[0]);
1318 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1319 && TYPE_CODE (type) != TYPE_CODE_UNION
1320 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1321 || rust_tuple_type_p (type))
1322 error (_("Method calls only supported on struct or enum types"));
1323 if (TYPE_TAG_NAME (type) == NULL)
1324 error (_("Method call on nameless type"));
1325
ab8b80a8 1326 std::string name = std::string (TYPE_TAG_NAME (type)) + "::" + method;
c44af4eb
TT
1327
1328 block = get_selected_block (0);
ab8b80a8 1329 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
c44af4eb 1330 if (sym.symbol == NULL)
ab8b80a8 1331 error (_("Could not find function named '%s'"), name.c_str ());
c44af4eb
TT
1332
1333 fn_type = SYMBOL_TYPE (sym.symbol);
1334 if (TYPE_NFIELDS (fn_type) == 0)
ab8b80a8 1335 error (_("Function '%s' takes no arguments"), name.c_str ());
c44af4eb
TT
1336
1337 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1338 args[0] = value_addr (args[0]);
1339
1340 function = address_of_variable (sym.symbol, block);
1341
1342 for (i = 0; i < num_args; ++i)
1343 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1344
1345 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1346 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1347 else
7022349d 1348 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
c44af4eb
TT
1349 return result;
1350}
1351
01739a3b 1352/* A helper for rust_evaluate_subexp that handles OP_RANGE. */
c44af4eb
TT
1353
1354static struct value *
1355rust_range (struct expression *exp, int *pos, enum noside noside)
1356{
01739a3b 1357 enum range_type kind;
c44af4eb
TT
1358 struct value *low = NULL, *high = NULL;
1359 struct value *addrval, *result;
1360 CORE_ADDR addr;
1361 struct type *range_type;
1362 struct type *index_type;
1363 struct type *temp_type;
1364 const char *name;
1365
01739a3b 1366 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
c44af4eb
TT
1367 *pos += 3;
1368
1369 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1370 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1371 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1372 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1373
1374 if (noside == EVAL_SKIP)
1375 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1376
1377 if (low == NULL)
1378 {
1379 if (high == NULL)
1380 {
1381 index_type = NULL;
1382 name = "std::ops::RangeFull";
1383 }
1384 else
1385 {
1386 index_type = value_type (high);
1387 name = "std::ops::RangeTo";
1388 }
1389 }
1390 else
1391 {
1392 if (high == NULL)
1393 {
1394 index_type = value_type (low);
1395 name = "std::ops::RangeFrom";
1396 }
1397 else
1398 {
1399 if (!types_equal (value_type (low), value_type (high)))
1400 error (_("Range expression with different types"));
1401 index_type = value_type (low);
1402 name = "std::ops::Range";
1403 }
1404 }
1405
1406 /* If we don't have an index type, just allocate this on the
1407 arch. Here any type will do. */
1408 temp_type = (index_type == NULL
1409 ? language_bool_type (exp->language_defn, exp->gdbarch)
1410 : index_type);
1411 /* It would be nicer to cache the range type. */
1412 range_type = rust_composite_type (temp_type, name,
1413 low == NULL ? NULL : "start", index_type,
1414 high == NULL ? NULL : "end", index_type);
1415
1416 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1417 return value_zero (range_type, lval_memory);
1418
1419 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1420 addr = value_as_long (addrval);
1421 result = value_at_lazy (range_type, addr);
1422
1423 if (low != NULL)
1424 {
1425 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1426 "range");
1427
1428 value_assign (start, low);
1429 }
1430
1431 if (high != NULL)
1432 {
1433 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1434 "range");
1435
1436 value_assign (end, high);
1437 }
1438
1439 result = value_at_lazy (range_type, addr);
1440 return result;
1441}
1442
1443/* A helper function to compute the range and kind given a range
1444 value. TYPE is the type of the range value. RANGE is the range
1445 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1446 parameters might be filled in, or might not be, depending on the
1447 kind of range this is. KIND will always be set to the appropriate
1448 value describing the kind of range, and this can be used to
1449 determine whether LOW or HIGH are valid. */
1450
1451static void
1452rust_compute_range (struct type *type, struct value *range,
1453 LONGEST *low, LONGEST *high,
01739a3b 1454 enum range_type *kind)
c44af4eb
TT
1455{
1456 int i;
1457
1458 *low = 0;
1459 *high = 0;
1460 *kind = BOTH_BOUND_DEFAULT;
1461
1462 if (TYPE_NFIELDS (type) == 0)
1463 return;
1464
1465 i = 0;
1466 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1467 {
1468 *kind = HIGH_BOUND_DEFAULT;
1469 *low = value_as_long (value_field (range, 0));
1470 ++i;
1471 }
1472 if (TYPE_NFIELDS (type) > i
1473 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1474 {
1475 *kind = (*kind == BOTH_BOUND_DEFAULT
1476 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1477 *high = value_as_long (value_field (range, i));
1478 }
1479}
1480
1481/* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1482
1483static struct value *
1484rust_subscript (struct expression *exp, int *pos, enum noside noside,
1485 int for_addr)
1486{
1487 struct value *lhs, *rhs, *result;
1488 struct type *rhstype;
45f4ed92 1489 LONGEST low, high_bound;
c44af4eb 1490 /* Initialized to appease the compiler. */
01739a3b 1491 enum range_type kind = BOTH_BOUND_DEFAULT;
45f4ed92 1492 LONGEST high = 0;
c44af4eb
TT
1493 int want_slice = 0;
1494
1495 ++*pos;
1496 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1497 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1498
1499 if (noside == EVAL_SKIP)
1500 return lhs;
1501
1502 rhstype = check_typedef (value_type (rhs));
1503 if (rust_range_type_p (rhstype))
1504 {
1505 if (!for_addr)
1506 error (_("Can't take slice of array without '&'"));
1507 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1508 want_slice = 1;
1509 }
1510 else
1511 low = value_as_long (rhs);
1512
b3e3859b 1513 struct type *type = check_typedef (value_type (lhs));
c44af4eb
TT
1514 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1515 {
b3e3859b
TT
1516 struct type *base_type = nullptr;
1517 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1518 base_type = TYPE_TARGET_TYPE (type);
1519 else if (rust_slice_type_p (type))
1520 {
1521 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1522 {
1523 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1524 {
1525 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1526 break;
1527 }
1528 }
1529 if (base_type == nullptr)
1530 error (_("Could not find 'data_ptr' in slice type"));
1531 }
1532 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1533 base_type = TYPE_TARGET_TYPE (type);
1534 else
1535 error (_("Cannot subscript non-array type"));
1536
1537 struct type *new_type;
1538 if (want_slice)
1539 {
1540 if (rust_slice_type_p (type))
1541 new_type = type;
1542 else
1543 {
1544 struct type *usize
1545 = language_lookup_primitive_type (exp->language_defn,
1546 exp->gdbarch,
1547 "usize");
1548 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1549 }
1550 }
1551 else
1552 new_type = base_type;
c44af4eb 1553
b3e3859b 1554 return value_zero (new_type, VALUE_LVAL (lhs));
c44af4eb
TT
1555 }
1556 else
1557 {
1558 LONGEST low_bound;
1559 struct value *base;
c44af4eb
TT
1560
1561 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1562 {
1563 base = lhs;
1564 if (!get_array_bounds (type, &low_bound, &high_bound))
1565 error (_("Can't compute array bounds"));
1566 if (low_bound != 0)
1567 error (_("Found array with non-zero lower bound"));
1568 ++high_bound;
1569 }
1570 else if (rust_slice_type_p (type))
1571 {
1572 struct value *len;
1573
1574 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1575 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1576 low_bound = 0;
1577 high_bound = value_as_long (len);
1578 }
42d94011
MG
1579 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1580 {
1581 base = lhs;
1582 low_bound = 0;
1583 high_bound = LONGEST_MAX;
1584 }
c44af4eb
TT
1585 else
1586 error (_("Cannot subscript non-array type"));
1587
1588 if (want_slice
1589 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1590 low = low_bound;
1591 if (low < 0)
1592 error (_("Index less than zero"));
1593 if (low > high_bound)
1594 error (_("Index greater than length"));
1595
1596 result = value_subscript (base, low);
1597 }
1598
1599 if (for_addr)
1600 {
1601 if (want_slice)
1602 {
1603 struct type *usize, *slice;
1604 CORE_ADDR addr;
1605 struct value *addrval, *tem;
1606
1607 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1608 high = high_bound;
1609 if (high < 0)
1610 error (_("High index less than zero"));
1611 if (low > high)
1612 error (_("Low index greater than high index"));
1613 if (high > high_bound)
1614 error (_("High index greater than length"));
1615
1616 usize = language_lookup_primitive_type (exp->language_defn,
1617 exp->gdbarch,
1618 "usize");
45320ffa
TT
1619 const char *new_name = ((type != nullptr
1620 && rust_slice_type_p (type))
1621 ? TYPE_NAME (type) : "&[*gdb*]");
1622
1623 slice = rust_slice_type (new_name, value_type (result), usize);
c44af4eb
TT
1624
1625 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1626 addr = value_as_long (addrval);
1627 tem = value_at_lazy (slice, addr);
1628
1629 value_assign (value_field (tem, 0), value_addr (result));
1630 value_assign (value_field (tem, 1),
1631 value_from_longest (usize, high - low));
1632
1633 result = value_at_lazy (slice, addr);
1634 }
1635 else
1636 result = value_addr (result);
1637 }
1638
1639 return result;
1640}
1641
1642/* evaluate_exp implementation for Rust. */
1643
1644static struct value *
1645rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1646 int *pos, enum noside noside)
1647{
1648 struct value *result;
1649
1650 switch (exp->elts[*pos].opcode)
1651 {
71a3c369
TT
1652 case UNOP_IND:
1653 {
1654 if (noside != EVAL_NORMAL)
1655 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1656 else
1657 {
1658 ++*pos;
1659 struct value *value = evaluate_subexp (expect_type, exp, pos,
1660 noside);
1661
1662 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1663 if (trait_ptr != NULL)
1664 value = trait_ptr;
1665
1666 result = value_ind (value);
1667 }
1668 }
1669 break;
1670
c44af4eb
TT
1671 case UNOP_COMPLEMENT:
1672 {
1673 struct value *value;
1674
1675 ++*pos;
1676 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1677 if (noside == EVAL_SKIP)
1678 {
1679 /* Preserving the type is enough. */
1680 return value;
1681 }
1682 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1683 result = value_from_longest (value_type (value),
1684 value_logical_not (value));
1685 else
1686 result = value_complement (value);
1687 }
1688 break;
1689
1690 case BINOP_SUBSCRIPT:
1691 result = rust_subscript (exp, pos, noside, 0);
1692 break;
1693
1694 case OP_FUNCALL:
1695 result = rust_evaluate_funcall (exp, pos, noside);
1696 break;
1697
1698 case OP_AGGREGATE:
1699 {
1700 int pc = (*pos)++;
1701 struct type *type = exp->elts[pc + 1].type;
1702 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1703 int i;
1704 CORE_ADDR addr = 0;
1705 struct value *addrval = NULL;
1706
1707 *pos += 3;
1708
1709 if (noside == EVAL_NORMAL)
1710 {
1711 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1712 addr = value_as_long (addrval);
1713 result = value_at_lazy (type, addr);
1714 }
1715
1716 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1717 {
1718 struct value *init;
1719
1720 ++*pos;
1721 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1722 if (noside == EVAL_NORMAL)
1723 {
1724 /* This isn't quite right but will do for the time
1725 being, seeing that we can't implement the Copy
1726 trait anyway. */
1727 value_assign (result, init);
1728 }
1729
1730 --arglen;
1731 }
1732
1733 gdb_assert (arglen % 2 == 0);
1734 for (i = 0; i < arglen; i += 2)
1735 {
1736 int len;
1737 const char *fieldname;
1738 struct value *value, *field;
1739
1740 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1741 ++*pos;
1742 len = longest_to_int (exp->elts[*pos].longconst);
1743 ++*pos;
1744 fieldname = &exp->elts[*pos].string;
1745 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1746
1747 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1748 if (noside == EVAL_NORMAL)
1749 {
1750 field = value_struct_elt (&result, NULL, fieldname, NULL,
1751 "structure");
1752 value_assign (field, value);
1753 }
1754 }
1755
1756 if (noside == EVAL_SKIP)
1757 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1758 1);
1759 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1760 result = allocate_value (type);
1761 else
1762 result = value_at_lazy (type, addr);
1763 }
1764 break;
1765
1766 case OP_RUST_ARRAY:
1767 {
1768 int pc = (*pos)++;
1769 int copies;
1770 struct value *elt;
1771 struct value *ncopies;
1772
1773 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1774 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1775 copies = value_as_long (ncopies);
1776 if (copies < 0)
1777 error (_("Array with negative number of elements"));
1778
1779 if (noside == EVAL_NORMAL)
1780 {
c44af4eb 1781 int i;
ab8b80a8 1782 std::vector<struct value *> eltvec (copies);
c44af4eb
TT
1783
1784 for (i = 0; i < copies; ++i)
1785 eltvec[i] = elt;
ab8b80a8 1786 result = value_array (0, copies - 1, eltvec.data ());
c44af4eb
TT
1787 }
1788 else
1789 {
1790 struct type *arraytype
1791 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1792 result = allocate_value (arraytype);
1793 }
1794 }
1795 break;
1796
1797 case STRUCTOP_ANONYMOUS:
1798 {
1799 /* Anonymous field access, i.e. foo.1. */
1800 struct value *lhs;
1801 int pc, field_number, nfields;
1802 struct type *type, *variant_type;
1803 struct disr_info disr;
1804
1805 pc = (*pos)++;
1806 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1807 (*pos) += 2;
1808 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1809
1810 type = value_type (lhs);
b96645f1
MG
1811 /* Untagged unions can't have anonymous field access since
1812 they can only have named fields. */
1813 if (TYPE_CODE (type) == TYPE_CODE_UNION
1814 && !rust_union_is_untagged (type))
c44af4eb 1815 {
c44af4eb
TT
1816 disr = rust_get_disr_info (type, value_contents (lhs),
1817 value_embedded_offset (lhs),
1818 value_address (lhs), lhs);
1819
c44af4eb
TT
1820 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1821 {
1822 variant_type = NULL;
1823 nfields = 0;
1824 }
1825 else
1826 {
1827 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1828 nfields = TYPE_NFIELDS (variant_type);
1829 }
1830
1831 if (!disr.is_encoded)
1832 ++field_number;
1833
1834 if (field_number >= nfields || field_number < 0)
1835 error(_("Cannot access field %d of variant %s, \
1836there are only %d fields"),
1837 disr.is_encoded ? field_number : field_number - 1,
5698084a 1838 disr.name.c_str (),
c44af4eb
TT
1839 disr.is_encoded ? nfields : nfields - 1);
1840
1841 if (!(disr.is_encoded
1842 ? rust_tuple_struct_type_p (variant_type)
1843 : rust_tuple_variant_type_p (variant_type)))
5698084a 1844 error(_("Variant %s is not a tuple variant"), disr.name.c_str ());
c44af4eb
TT
1845
1846 result = value_primitive_field (lhs, 0, field_number,
1847 variant_type);
c44af4eb
TT
1848 }
1849 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1850 {
1851 /* Tuples and tuple structs */
1852 nfields = TYPE_NFIELDS(type);
1853
1854 if (field_number >= nfields || field_number < 0)
1855 error(_("Cannot access field %d of %s, there are only %d fields"),
1856 field_number, TYPE_TAG_NAME (type), nfields);
1857
1858 /* Tuples are tuple structs too. */
1859 if (!rust_tuple_struct_type_p (type))
1860 error(_("Attempting to access anonymous field %d of %s, which is \
1861not a tuple, tuple struct, or tuple-like variant"),
1862 field_number, TYPE_TAG_NAME (type));
1863
1864 result = value_primitive_field (lhs, 0, field_number, type);
1865 }
1866 else
1867 error(_("Anonymous field access is only allowed on tuples, \
1868tuple structs, and tuple-like enum variants"));
1869 }
1870 break;
1871
1872 case STRUCTOP_STRUCT:
1873 {
6830f270 1874 struct value *lhs;
c44af4eb
TT
1875 struct type *type;
1876 int tem, pc;
1877
1878 pc = (*pos)++;
1879 tem = longest_to_int (exp->elts[pc + 1].longconst);
1880 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1881 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1882
6830f270 1883 const char *field_name = &exp->elts[pc + 2].string;
c44af4eb 1884 type = value_type (lhs);
b96645f1
MG
1885 if (TYPE_CODE (type) == TYPE_CODE_UNION
1886 && !rust_union_is_untagged (type))
c44af4eb
TT
1887 {
1888 int i, start;
1889 struct disr_info disr;
6830f270 1890 struct type *variant_type;
c44af4eb
TT
1891
1892 disr = rust_get_disr_info (type, value_contents (lhs),
1893 value_embedded_offset (lhs),
1894 value_address (lhs), lhs);
1895
c44af4eb
TT
1896 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1897 error(_("Could not find field %s of struct variant %s"),
5698084a 1898 field_name, disr.name.c_str ());
c44af4eb
TT
1899
1900 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1901
1902 if (variant_type == NULL
51a789c3
MG
1903 || (disr.is_encoded
1904 ? rust_tuple_struct_type_p (variant_type)
1905 : rust_tuple_variant_type_p (variant_type)))
c44af4eb
TT
1906 error(_("Attempting to access named field %s of tuple variant %s, \
1907which has only anonymous fields"),
5698084a 1908 field_name, disr.name.c_str ());
c44af4eb
TT
1909
1910 start = disr.is_encoded ? 0 : 1;
1911 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1912 {
1913 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1914 field_name) == 0) {
1915 result = value_primitive_field (lhs, 0, i, variant_type);
1916 break;
1917 }
1918 }
1919
1920 if (i == TYPE_NFIELDS (variant_type))
1921 /* We didn't find it. */
1922 error(_("Could not find field %s of struct variant %s"),
5698084a 1923 field_name, disr.name.c_str ());
c44af4eb
TT
1924 }
1925 else
1926 {
6830f270
TT
1927 result = value_struct_elt (&lhs, NULL, field_name, NULL,
1928 "structure");
1929 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1930 result = value_zero (value_type (result), VALUE_LVAL (result));
c44af4eb
TT
1931 }
1932 }
1933 break;
1934
01739a3b 1935 case OP_RANGE:
c44af4eb
TT
1936 result = rust_range (exp, pos, noside);
1937 break;
1938
1939 case UNOP_ADDR:
1940 /* We might have &array[range], in which case we need to make a
1941 slice. */
1942 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1943 {
1944 ++*pos;
1945 result = rust_subscript (exp, pos, noside, 1);
1946 break;
1947 }
1948 /* Fall through. */
1949 default:
1950 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1951 break;
1952 }
1953
1954 return result;
1955}
1956
1957/* operator_length implementation for Rust. */
1958
1959static void
1960rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1961 int *argsp)
1962{
1963 int oplen = 1;
1964 int args = 0;
1965
1966 switch (exp->elts[pc - 1].opcode)
1967 {
1968 case OP_AGGREGATE:
1969 /* We handle aggregate as a type and argument count. The first
1970 argument might be OP_OTHERS. After that the arguments
1971 alternate: first an OP_NAME, then an expression. */
1972 oplen = 4;
1973 args = longest_to_int (exp->elts[pc - 2].longconst);
1974 break;
1975
1976 case OP_OTHERS:
1977 oplen = 1;
1978 args = 1;
1979 break;
1980
1981 case STRUCTOP_ANONYMOUS:
1982 oplen = 3;
1983 args = 1;
1984 break;
1985
1986 case OP_RUST_ARRAY:
1987 oplen = 1;
1988 args = 2;
1989 break;
1990
1991 default:
1992 operator_length_standard (exp, pc, oplenp, argsp);
1993 return;
1994 }
1995
1996 *oplenp = oplen;
1997 *argsp = args;
1998}
1999
2000/* op_name implementation for Rust. */
2001
a121b7c1 2002static const char *
c44af4eb
TT
2003rust_op_name (enum exp_opcode opcode)
2004{
2005 switch (opcode)
2006 {
2007 case OP_AGGREGATE:
2008 return "OP_AGGREGATE";
2009 case OP_OTHERS:
2010 return "OP_OTHERS";
2011 default:
2012 return op_name_standard (opcode);
2013 }
2014}
2015
2016/* dump_subexp_body implementation for Rust. */
2017
2018static int
2019rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
2020 int elt)
2021{
2022 switch (exp->elts[elt].opcode)
2023 {
2024 case OP_AGGREGATE:
2025 {
2026 int length = longest_to_int (exp->elts[elt + 2].longconst);
2027 int i;
2028
2029 fprintf_filtered (stream, "Type @");
2030 gdb_print_host_address (exp->elts[elt + 1].type, stream);
2031 fprintf_filtered (stream, " (");
2032 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
2033 fprintf_filtered (stream, "), length %d", length);
2034
2035 elt += 4;
2036 for (i = 0; i < length; ++i)
2037 elt = dump_subexp (exp, stream, elt);
2038 }
2039 break;
2040
2041 case OP_STRING:
2042 case OP_NAME:
2043 {
2044 LONGEST len = exp->elts[elt + 1].longconst;
2045
2046 fprintf_filtered (stream, "%s: %s",
2047 (exp->elts[elt].opcode == OP_STRING
2048 ? "string" : "name"),
2049 &exp->elts[elt + 2].string);
2050 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
2051 }
2052 break;
2053
2054 case OP_OTHERS:
2055 elt = dump_subexp (exp, stream, elt + 1);
2056 break;
2057
2058 case STRUCTOP_ANONYMOUS:
2059 {
2060 int field_number;
2061
68f2f2e3 2062 field_number = longest_to_int (exp->elts[elt + 1].longconst);
c44af4eb
TT
2063
2064 fprintf_filtered (stream, "Field number: %d", field_number);
68f2f2e3 2065 elt = dump_subexp (exp, stream, elt + 3);
c44af4eb
TT
2066 }
2067 break;
2068
2069 case OP_RUST_ARRAY:
68f2f2e3 2070 ++elt;
c44af4eb
TT
2071 break;
2072
2073 default:
2074 elt = dump_subexp_body_standard (exp, stream, elt);
2075 break;
2076 }
2077
2078 return elt;
2079}
2080
2081/* print_subexp implementation for Rust. */
2082
2083static void
2084rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
2085 enum precedence prec)
2086{
2087 switch (exp->elts[*pos].opcode)
2088 {
2089 case OP_AGGREGATE:
2090 {
2091 int length = longest_to_int (exp->elts[*pos + 2].longconst);
2092 int i;
2093
2094 type_print (exp->elts[*pos + 1].type, "", stream, 0);
2095 fputs_filtered (" { ", stream);
2096
2097 *pos += 4;
2098 for (i = 0; i < length; ++i)
2099 {
2100 rust_print_subexp (exp, pos, stream, prec);
2101 fputs_filtered (", ", stream);
2102 }
2103 fputs_filtered (" }", stream);
2104 }
2105 break;
2106
2107 case OP_NAME:
2108 {
2109 LONGEST len = exp->elts[*pos + 1].longconst;
2110
2111 fputs_filtered (&exp->elts[*pos + 2].string, stream);
2112 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
2113 }
2114 break;
2115
2116 case OP_OTHERS:
2117 {
2118 fputs_filtered ("<<others>> (", stream);
2119 ++*pos;
2120 rust_print_subexp (exp, pos, stream, prec);
2121 fputs_filtered (")", stream);
2122 }
2123 break;
2124
2125 case STRUCTOP_ANONYMOUS:
2126 {
2127 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
2128
2129 (*pos) += 3;
2130 print_subexp (exp, pos, stream, PREC_SUFFIX);
2131 fprintf_filtered (stream, ".%d", tem);
2132 }
256afbc2 2133 break;
c44af4eb
TT
2134
2135 case OP_RUST_ARRAY:
2136 ++*pos;
2137 fprintf_filtered (stream, "[");
2138 rust_print_subexp (exp, pos, stream, prec);
2139 fprintf_filtered (stream, "; ");
2140 rust_print_subexp (exp, pos, stream, prec);
2141 fprintf_filtered (stream, "]");
2142 break;
2143
2144 default:
2145 print_subexp_standard (exp, pos, stream, prec);
2146 break;
2147 }
2148}
2149
2150/* operator_check implementation for Rust. */
2151
2152static int
2153rust_operator_check (struct expression *exp, int pos,
2154 int (*objfile_func) (struct objfile *objfile,
2155 void *data),
2156 void *data)
2157{
2158 switch (exp->elts[pos].opcode)
2159 {
2160 case OP_AGGREGATE:
2161 {
2162 struct type *type = exp->elts[pos + 1].type;
2163 struct objfile *objfile = TYPE_OBJFILE (type);
2164
2165 if (objfile != NULL && (*objfile_func) (objfile, data))
2166 return 1;
2167 }
2168 break;
2169
2170 case OP_OTHERS:
2171 case OP_NAME:
2172 case OP_RUST_ARRAY:
2173 break;
2174
2175 default:
2176 return operator_check_standard (exp, pos, objfile_func, data);
2177 }
2178
2179 return 0;
2180}
2181
2182\f
2183
2184/* Implementation of la_lookup_symbol_nonlocal for Rust. */
2185
2186static struct block_symbol
2187rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2188 const char *name,
2189 const struct block *block,
2190 const domain_enum domain)
2191{
2192 struct block_symbol result = {NULL, NULL};
2193
2194 if (symbol_lookup_debug)
2195 {
2196 fprintf_unfiltered (gdb_stdlog,
2197 "rust_lookup_symbol_non_local"
2198 " (%s, %s (scope %s), %s)\n",
2199 name, host_address_to_string (block),
2200 block_scope (block), domain_name (domain));
2201 }
2202
2203 /* Look up bare names in the block's scope. */
2204 if (name[cp_find_first_component (name)] == '\0')
2205 {
2206 const char *scope = block_scope (block);
2207
2208 if (scope[0] != '\0')
2209 {
ab8b80a8 2210 std::string scopedname = std::string (scope) + "::" + name;
c44af4eb 2211
ab8b80a8 2212 result = lookup_symbol_in_static_block (scopedname.c_str (), block,
c44af4eb
TT
2213 domain);
2214 if (result.symbol == NULL)
ab8b80a8 2215 result = lookup_global_symbol (scopedname.c_str (), block, domain);
c44af4eb
TT
2216 }
2217 }
2218 return result;
2219}
2220
2221\f
2222
8b302db8
TT
2223/* la_sniff_from_mangled_name for Rust. */
2224
2225static int
2226rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2227{
2228 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2229 return *demangled != NULL;
2230}
2231
2232\f
2233
43cc5389
TT
2234/* la_watch_location_expression for Rust. */
2235
2236static gdb::unique_xmalloc_ptr<char>
2237rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2238{
2239 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2240 std::string name = type_to_string (type);
2241 return gdb::unique_xmalloc_ptr<char>
2242 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2243 name.c_str ()));
2244}
2245
2246\f
2247
c44af4eb
TT
2248static const struct exp_descriptor exp_descriptor_rust =
2249{
2250 rust_print_subexp,
2251 rust_operator_length,
2252 rust_operator_check,
2253 rust_op_name,
2254 rust_dump_subexp_body,
2255 rust_evaluate_subexp
2256};
2257
56618e20
TT
2258static const char *rust_extensions[] =
2259{
2260 ".rs", NULL
2261};
2262
47e77640 2263extern const struct language_defn rust_language_defn =
c44af4eb
TT
2264{
2265 "rust",
2266 "Rust",
2267 language_rust,
2268 range_check_on,
2269 case_sensitive_on,
2270 array_row_major,
2271 macro_expansion_no,
56618e20 2272 rust_extensions,
c44af4eb
TT
2273 &exp_descriptor_rust,
2274 rust_parse,
2275 rustyyerror,
2276 null_post_parser,
2277 rust_printchar, /* Print a character constant */
2278 rust_printstr, /* Function to print string constant */
2279 rust_emitchar, /* Print a single char */
2280 rust_print_type, /* Print a type using appropriate syntax */
2281 rust_print_typedef, /* Print a typedef using appropriate syntax */
2282 rust_val_print, /* Print a value using appropriate syntax */
2283 c_value_print, /* Print a top-level value */
2284 default_read_var_value, /* la_read_var_value */
2285 NULL, /* Language specific skip_trampoline */
2286 NULL, /* name_of_this */
2287 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2288 basic_lookup_transparent_type,/* lookup_transparent_type */
2289 gdb_demangle, /* Language specific symbol demangler */
8b302db8 2290 rust_sniff_from_mangled_name,
c44af4eb
TT
2291 NULL, /* Language specific
2292 class_name_from_physname */
2293 c_op_print_tab, /* expression operators for printing */
2294 1, /* c-style arrays */
2295 0, /* String lower bound */
2296 default_word_break_characters,
eb3ff9a5 2297 default_collect_symbol_completion_matches,
c44af4eb
TT
2298 rust_language_arch_info,
2299 default_print_array_index,
2300 default_pass_by_reference,
2301 c_get_string,
43cc5389 2302 rust_watch_location_expression,
b5ec771e 2303 NULL, /* la_get_symbol_name_matcher */
c44af4eb 2304 iterate_over_symbols,
5ffa0793 2305 default_search_name_hash,
c44af4eb
TT
2306 &default_varobj_ops,
2307 NULL,
2308 NULL,
2309 LANG_MAGIC
2310};