]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rust-lang.c
Make template_symbol derive from symbol
[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);
424 if (symbol == NULL || !symbol->is_rust_vtable)
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 {
851 const char *name;
b96645f1 852
b50f188d
TT
853 QUIT;
854 if (field_is_static (&TYPE_FIELD (type, i)))
855 continue;
856
857 /* We'd like to print "pub" here as needed, but rustc
858 doesn't emit the debuginfo, and our types don't have
859 cplus_struct_type attached. */
860
861 /* For a tuple struct we print the type but nothing
862 else. */
863 print_spaces_filtered (level + 2, stream);
864 if (!is_tuple_struct)
865 fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
866
867 rust_print_type (TYPE_FIELD_TYPE (type, i), NULL,
868 stream, show - 1, level + 2,
869 flags);
870 fputs_filtered (",\n", stream);
871 }
c44af4eb 872
b50f188d 873 fprintfi_filtered (level, stream, is_tuple_struct ? ")" : "}");
c44af4eb
TT
874}
875
c44af4eb
TT
876/* la_print_typedef implementation for Rust. */
877
878static void
879rust_print_typedef (struct type *type,
880 struct symbol *new_symbol,
881 struct ui_file *stream)
882{
883 type = check_typedef (type);
884 fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
885 type_print (type, "", stream, 0);
886 fprintf_filtered (stream, ";\n");
887}
888
889/* la_print_type implementation for Rust. */
890
891static void
892rust_print_type (struct type *type, const char *varstring,
893 struct ui_file *stream, int show, int level,
894 const struct type_print_options *flags)
895{
896 int i;
897
898 QUIT;
899 if (show <= 0
900 && TYPE_NAME (type) != NULL)
901 {
921d8f54
MG
902 /* Rust calls the unit type "void" in its debuginfo,
903 but we don't want to print it as that. */
904 if (TYPE_CODE (type) == TYPE_CODE_VOID)
905 fputs_filtered ("()", stream);
906 else
907 fputs_filtered (TYPE_NAME (type), stream);
c44af4eb
TT
908 return;
909 }
910
911 type = check_typedef (type);
912 switch (TYPE_CODE (type))
913 {
921d8f54
MG
914 case TYPE_CODE_VOID:
915 fputs_filtered ("()", stream);
916 break;
917
c44af4eb
TT
918 case TYPE_CODE_FUNC:
919 /* Delegate varargs to the C printer. */
920 if (TYPE_VARARGS (type))
921 goto c_printer;
922
923 fputs_filtered ("fn ", stream);
924 if (varstring != NULL)
925 fputs_filtered (varstring, stream);
926 fputs_filtered ("(", stream);
927 for (i = 0; i < TYPE_NFIELDS (type); ++i)
928 {
929 QUIT;
930 if (i > 0)
931 fputs_filtered (", ", stream);
932 rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0,
933 flags);
934 }
921d8f54
MG
935 fputs_filtered (")", stream);
936 /* If it returns unit, we can omit the return type. */
937 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
938 {
939 fputs_filtered (" -> ", stream);
940 rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags);
941 }
c44af4eb
TT
942 break;
943
944 case TYPE_CODE_ARRAY:
945 {
946 LONGEST low_bound, high_bound;
947
948 fputs_filtered ("[", stream);
949 rust_print_type (TYPE_TARGET_TYPE (type), NULL,
950 stream, show - 1, level, flags);
c44af4eb
TT
951
952 if (TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCEXPR
953 || TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (type)) == PROP_LOCLIST)
e6cf65f2 954 fprintf_filtered (stream, "; variable length");
c44af4eb 955 else if (get_array_bounds (type, &low_bound, &high_bound))
e6cf65f2 956 fprintf_filtered (stream, "; %s",
c44af4eb
TT
957 plongest (high_bound - low_bound + 1));
958 fputs_filtered ("]", stream);
959 }
960 break;
961
962 case TYPE_CODE_STRUCT:
b50f188d 963 rust_print_struct_def (type, varstring, stream, show, level, flags);
c44af4eb
TT
964 break;
965
966 case TYPE_CODE_ENUM:
967 {
968 int i, len = 0;
969
970 fputs_filtered ("enum ", stream);
971 if (TYPE_TAG_NAME (type) != NULL)
972 {
973 fputs_filtered (TYPE_TAG_NAME (type), stream);
974 fputs_filtered (" ", stream);
975 len = strlen (TYPE_TAG_NAME (type));
976 }
977 fputs_filtered ("{\n", stream);
978
979 for (i = 0; i < TYPE_NFIELDS (type); ++i)
980 {
981 const char *name = TYPE_FIELD_NAME (type, i);
982
983 QUIT;
984
985 if (len > 0
986 && strncmp (name, TYPE_TAG_NAME (type), len) == 0
987 && name[len] == ':'
988 && name[len + 1] == ':')
989 name += len + 2;
990 fprintfi_filtered (level + 2, stream, "%s,\n", name);
991 }
992
993 fputs_filtered ("}", stream);
994 }
995 break;
996
997 case TYPE_CODE_UNION:
998 {
b5a4b3c5 999 /* ADT enums. */
c44af4eb 1000 int i, len = 0;
b5a4b3c5
MG
1001 /* Skip the discriminant field. */
1002 int skip_to = 1;
c44af4eb 1003
b50f188d
TT
1004 /* Unions and structs have the same syntax in Rust,
1005 the only difference is that structs are declared with `struct`
1006 and union with `union`. This difference is handled in the struct
1007 printer. */
1008 if (rust_union_is_untagged (type))
1009 {
1010 rust_print_struct_def (type, varstring, stream, show, level, flags);
1011 break;
1012 }
b96645f1 1013
c44af4eb
TT
1014 fputs_filtered ("enum ", stream);
1015 if (TYPE_TAG_NAME (type) != NULL)
1016 {
1017 fputs_filtered (TYPE_TAG_NAME (type), stream);
1018 fputs_filtered (" ", stream);
c44af4eb 1019 }
b5a4b3c5
MG
1020 fputs_filtered ("{\n", stream);
1021
9e8a8ea8
TT
1022 if (strncmp (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX,
1023 strlen (RUST_ENUM_PREFIX)) == 0)
1024 {
1025 const char *zero_field = strrchr (TYPE_FIELD_NAME (type, 0), '$');
1026 if (zero_field != NULL && strlen (zero_field) > 1)
1027 {
1028 fprintfi_filtered (level + 2, stream, "%s,\n", zero_field + 1);
1029 /* There is no explicit discriminant field, skip nothing. */
1030 skip_to = 0;
1031 }
1032 }
f0fd41c1
TT
1033 else if (TYPE_NFIELDS (type) == 1)
1034 skip_to = 0;
c44af4eb
TT
1035
1036 for (i = 0; i < TYPE_NFIELDS (type); ++i)
1037 {
1038 struct type *variant_type = TYPE_FIELD_TYPE (type, i);
1039 const char *name
1040 = rust_last_path_segment (TYPE_NAME (variant_type));
1041
1042 fprintfi_filtered (level + 2, stream, "%s", name);
1043
b5a4b3c5 1044 if (TYPE_NFIELDS (variant_type) > skip_to)
c44af4eb
TT
1045 {
1046 int first = 1;
f0fd41c1
TT
1047 bool is_tuple = (TYPE_NFIELDS (type) == 1
1048 ? rust_tuple_struct_type_p (variant_type)
1049 : rust_tuple_variant_type_p (variant_type));
c44af4eb
TT
1050 int j;
1051
1052 fputs_filtered (is_tuple ? "(" : "{", stream);
b5a4b3c5 1053 for (j = skip_to; j < TYPE_NFIELDS (variant_type); j++)
c44af4eb
TT
1054 {
1055 if (first)
1056 first = 0;
1057 else
1058 fputs_filtered (", ", stream);
1059
1060 if (!is_tuple)
1061 fprintf_filtered (stream, "%s: ",
1062 TYPE_FIELD_NAME (variant_type, j));
1063
1064 rust_print_type (TYPE_FIELD_TYPE (variant_type, j), NULL,
1065 stream, show - 1, level + 2,
1066 flags);
1067 }
1068 fputs_filtered (is_tuple ? ")" : "}", stream);
1069 }
1070
1071 fputs_filtered (",\n", stream);
1072 }
1073
1074 fputs_filtered ("}", stream);
1075 }
1076 break;
1077
1078 default:
1079 c_printer:
1080 c_print_type (type, varstring, stream, show, level, flags);
1081 }
1082}
1083
1084\f
1085
1086/* Compute the alignment of the type T. */
1087
1088static int
1089rust_type_alignment (struct type *t)
1090{
1091 t = check_typedef (t);
1092 switch (TYPE_CODE (t))
1093 {
1094 default:
1095 error (_("Could not compute alignment of type"));
1096
1097 case TYPE_CODE_PTR:
1098 case TYPE_CODE_ENUM:
1099 case TYPE_CODE_INT:
1100 case TYPE_CODE_FLT:
1101 case TYPE_CODE_REF:
1102 case TYPE_CODE_CHAR:
1103 case TYPE_CODE_BOOL:
1104 return TYPE_LENGTH (t);
1105
1106 case TYPE_CODE_ARRAY:
1107 case TYPE_CODE_COMPLEX:
1108 return rust_type_alignment (TYPE_TARGET_TYPE (t));
1109
1110 case TYPE_CODE_STRUCT:
1111 case TYPE_CODE_UNION:
1112 {
1113 int i;
1114 int align = 1;
1115
1116 for (i = 0; i < TYPE_NFIELDS (t); ++i)
1117 {
1118 int a = rust_type_alignment (TYPE_FIELD_TYPE (t, i));
1119 if (a > align)
1120 align = a;
1121 }
1122 return align;
1123 }
1124 }
1125}
1126
1127/* Like arch_composite_type, but uses TYPE to decide how to allocate
1128 -- either on an obstack or on a gdbarch. */
1129
1130static struct type *
1131rust_composite_type (struct type *original,
1132 const char *name,
1133 const char *field1, struct type *type1,
1134 const char *field2, struct type *type2)
1135{
1136 struct type *result = alloc_type_copy (original);
1137 int i, nfields, bitpos;
1138
1139 nfields = 0;
1140 if (field1 != NULL)
1141 ++nfields;
1142 if (field2 != NULL)
1143 ++nfields;
1144
1145 TYPE_CODE (result) = TYPE_CODE_STRUCT;
1146 TYPE_NAME (result) = name;
1147 TYPE_TAG_NAME (result) = name;
1148
1149 TYPE_NFIELDS (result) = nfields;
1150 TYPE_FIELDS (result)
1151 = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
1152
1153 i = 0;
1154 bitpos = 0;
1155 if (field1 != NULL)
1156 {
1157 struct field *field = &TYPE_FIELD (result, i);
1158
1159 SET_FIELD_BITPOS (*field, bitpos);
1160 bitpos += TYPE_LENGTH (type1) * TARGET_CHAR_BIT;
1161
1162 FIELD_NAME (*field) = field1;
1163 FIELD_TYPE (*field) = type1;
1164 ++i;
1165 }
1166 if (field2 != NULL)
1167 {
1168 struct field *field = &TYPE_FIELD (result, i);
1169 int align = rust_type_alignment (type2);
1170
1171 if (align != 0)
1172 {
1173 int delta;
1174
1175 align *= TARGET_CHAR_BIT;
1176 delta = bitpos % align;
1177 if (delta != 0)
1178 bitpos += align - delta;
1179 }
1180 SET_FIELD_BITPOS (*field, bitpos);
1181
1182 FIELD_NAME (*field) = field2;
1183 FIELD_TYPE (*field) = type2;
1184 ++i;
1185 }
1186
1187 if (i > 0)
1188 TYPE_LENGTH (result)
1189 = (TYPE_FIELD_BITPOS (result, i - 1) / TARGET_CHAR_BIT +
1190 TYPE_LENGTH (TYPE_FIELD_TYPE (result, i - 1)));
1191 return result;
1192}
1193
1194/* See rust-lang.h. */
1195
1196struct type *
1197rust_slice_type (const char *name, struct type *elt_type,
1198 struct type *usize_type)
1199{
1200 struct type *type;
1201
1202 elt_type = lookup_pointer_type (elt_type);
1203 type = rust_composite_type (elt_type, name,
1204 "data_ptr", elt_type,
1205 "length", usize_type);
1206
1207 return type;
1208}
1209
1210enum rust_primitive_types
1211{
1212 rust_primitive_bool,
1213 rust_primitive_char,
1214 rust_primitive_i8,
1215 rust_primitive_u8,
1216 rust_primitive_i16,
1217 rust_primitive_u16,
1218 rust_primitive_i32,
1219 rust_primitive_u32,
1220 rust_primitive_i64,
1221 rust_primitive_u64,
1222 rust_primitive_isize,
1223 rust_primitive_usize,
1224 rust_primitive_f32,
1225 rust_primitive_f64,
1226 rust_primitive_unit,
1227 rust_primitive_str,
1228 nr_rust_primitive_types
1229};
1230
1231/* la_language_arch_info implementation for Rust. */
1232
1233static void
1234rust_language_arch_info (struct gdbarch *gdbarch,
1235 struct language_arch_info *lai)
1236{
1237 const struct builtin_type *builtin = builtin_type (gdbarch);
1238 struct type *tem;
1239 struct type **types;
1240 unsigned int length;
1241
1242 types = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_rust_primitive_types + 1,
1243 struct type *);
1244
1245 types[rust_primitive_bool] = arch_boolean_type (gdbarch, 8, 1, "bool");
1246 types[rust_primitive_char] = arch_character_type (gdbarch, 32, 1, "char");
1247 types[rust_primitive_i8] = arch_integer_type (gdbarch, 8, 0, "i8");
1248 types[rust_primitive_u8] = arch_integer_type (gdbarch, 8, 1, "u8");
1249 types[rust_primitive_i16] = arch_integer_type (gdbarch, 16, 0, "i16");
1250 types[rust_primitive_u16] = arch_integer_type (gdbarch, 16, 1, "u16");
1251 types[rust_primitive_i32] = arch_integer_type (gdbarch, 32, 0, "i32");
1252 types[rust_primitive_u32] = arch_integer_type (gdbarch, 32, 1, "u32");
1253 types[rust_primitive_i64] = arch_integer_type (gdbarch, 64, 0, "i64");
1254 types[rust_primitive_u64] = arch_integer_type (gdbarch, 64, 1, "u64");
1255
1256 length = 8 * TYPE_LENGTH (builtin->builtin_data_ptr);
1257 types[rust_primitive_isize] = arch_integer_type (gdbarch, length, 0, "isize");
1258 types[rust_primitive_usize] = arch_integer_type (gdbarch, length, 1, "usize");
1259
49f190bc
UW
1260 types[rust_primitive_f32] = arch_float_type (gdbarch, 32, "f32",
1261 floatformats_ieee_single);
1262 types[rust_primitive_f64] = arch_float_type (gdbarch, 64, "f64",
1263 floatformats_ieee_double);
c44af4eb
TT
1264
1265 types[rust_primitive_unit] = arch_integer_type (gdbarch, 0, 1, "()");
1266
1267 tem = make_cv_type (1, 0, types[rust_primitive_u8], NULL);
1268 types[rust_primitive_str] = rust_slice_type ("&str", tem,
1269 types[rust_primitive_usize]);
1270
1271 lai->primitive_type_vector = types;
1272 lai->bool_type_default = types[rust_primitive_bool];
1273 lai->string_char_type = types[rust_primitive_u8];
1274}
1275
1276\f
1277
1278/* A helper for rust_evaluate_subexp that handles OP_FUNCALL. */
1279
1280static struct value *
1281rust_evaluate_funcall (struct expression *exp, int *pos, enum noside noside)
1282{
1283 int i;
1284 int num_args = exp->elts[*pos + 1].longconst;
1285 const char *method;
c44af4eb 1286 struct value *function, *result, *arg0;
c44af4eb
TT
1287 struct type *type, *fn_type;
1288 const struct block *block;
1289 struct block_symbol sym;
1290
1291 /* For an ordinary function call we can simply defer to the
1292 generic implementation. */
1293 if (exp->elts[*pos + 3].opcode != STRUCTOP_STRUCT)
1294 return evaluate_subexp_standard (NULL, exp, pos, noside);
1295
1296 /* Skip over the OP_FUNCALL and the STRUCTOP_STRUCT. */
1297 *pos += 4;
1298 method = &exp->elts[*pos + 1].string;
1299 *pos += 3 + BYTES_TO_EXP_ELEM (exp->elts[*pos].longconst + 1);
1300
1301 /* Evaluate the argument to STRUCTOP_STRUCT, then find its
1302 type in order to look up the method. */
1303 arg0 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1304
1305 if (noside == EVAL_SKIP)
1306 {
1307 for (i = 0; i < num_args; ++i)
1308 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1309 return arg0;
1310 }
1311
ab8b80a8 1312 std::vector<struct value *> args (num_args + 1);
c44af4eb
TT
1313 args[0] = arg0;
1314
1315 /* We don't yet implement real Deref semantics. */
1316 while (TYPE_CODE (value_type (args[0])) == TYPE_CODE_PTR)
1317 args[0] = value_ind (args[0]);
1318
1319 type = value_type (args[0]);
1320 if ((TYPE_CODE (type) != TYPE_CODE_STRUCT
1321 && TYPE_CODE (type) != TYPE_CODE_UNION
1322 && TYPE_CODE (type) != TYPE_CODE_ENUM)
1323 || rust_tuple_type_p (type))
1324 error (_("Method calls only supported on struct or enum types"));
1325 if (TYPE_TAG_NAME (type) == NULL)
1326 error (_("Method call on nameless type"));
1327
ab8b80a8 1328 std::string name = std::string (TYPE_TAG_NAME (type)) + "::" + method;
c44af4eb
TT
1329
1330 block = get_selected_block (0);
ab8b80a8 1331 sym = lookup_symbol (name.c_str (), block, VAR_DOMAIN, NULL);
c44af4eb 1332 if (sym.symbol == NULL)
ab8b80a8 1333 error (_("Could not find function named '%s'"), name.c_str ());
c44af4eb
TT
1334
1335 fn_type = SYMBOL_TYPE (sym.symbol);
1336 if (TYPE_NFIELDS (fn_type) == 0)
ab8b80a8 1337 error (_("Function '%s' takes no arguments"), name.c_str ());
c44af4eb
TT
1338
1339 if (TYPE_CODE (TYPE_FIELD_TYPE (fn_type, 0)) == TYPE_CODE_PTR)
1340 args[0] = value_addr (args[0]);
1341
1342 function = address_of_variable (sym.symbol, block);
1343
1344 for (i = 0; i < num_args; ++i)
1345 args[i + 1] = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1346
1347 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1348 result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
1349 else
7022349d 1350 result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
c44af4eb
TT
1351 return result;
1352}
1353
01739a3b 1354/* A helper for rust_evaluate_subexp that handles OP_RANGE. */
c44af4eb
TT
1355
1356static struct value *
1357rust_range (struct expression *exp, int *pos, enum noside noside)
1358{
01739a3b 1359 enum range_type kind;
c44af4eb
TT
1360 struct value *low = NULL, *high = NULL;
1361 struct value *addrval, *result;
1362 CORE_ADDR addr;
1363 struct type *range_type;
1364 struct type *index_type;
1365 struct type *temp_type;
1366 const char *name;
1367
01739a3b 1368 kind = (enum range_type) longest_to_int (exp->elts[*pos + 1].longconst);
c44af4eb
TT
1369 *pos += 3;
1370
1371 if (kind == HIGH_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1372 low = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1373 if (kind == LOW_BOUND_DEFAULT || kind == NONE_BOUND_DEFAULT)
1374 high = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1375
1376 if (noside == EVAL_SKIP)
1377 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
1378
1379 if (low == NULL)
1380 {
1381 if (high == NULL)
1382 {
1383 index_type = NULL;
1384 name = "std::ops::RangeFull";
1385 }
1386 else
1387 {
1388 index_type = value_type (high);
1389 name = "std::ops::RangeTo";
1390 }
1391 }
1392 else
1393 {
1394 if (high == NULL)
1395 {
1396 index_type = value_type (low);
1397 name = "std::ops::RangeFrom";
1398 }
1399 else
1400 {
1401 if (!types_equal (value_type (low), value_type (high)))
1402 error (_("Range expression with different types"));
1403 index_type = value_type (low);
1404 name = "std::ops::Range";
1405 }
1406 }
1407
1408 /* If we don't have an index type, just allocate this on the
1409 arch. Here any type will do. */
1410 temp_type = (index_type == NULL
1411 ? language_bool_type (exp->language_defn, exp->gdbarch)
1412 : index_type);
1413 /* It would be nicer to cache the range type. */
1414 range_type = rust_composite_type (temp_type, name,
1415 low == NULL ? NULL : "start", index_type,
1416 high == NULL ? NULL : "end", index_type);
1417
1418 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1419 return value_zero (range_type, lval_memory);
1420
1421 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (range_type));
1422 addr = value_as_long (addrval);
1423 result = value_at_lazy (range_type, addr);
1424
1425 if (low != NULL)
1426 {
1427 struct value *start = value_struct_elt (&result, NULL, "start", NULL,
1428 "range");
1429
1430 value_assign (start, low);
1431 }
1432
1433 if (high != NULL)
1434 {
1435 struct value *end = value_struct_elt (&result, NULL, "end", NULL,
1436 "range");
1437
1438 value_assign (end, high);
1439 }
1440
1441 result = value_at_lazy (range_type, addr);
1442 return result;
1443}
1444
1445/* A helper function to compute the range and kind given a range
1446 value. TYPE is the type of the range value. RANGE is the range
1447 value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH
1448 parameters might be filled in, or might not be, depending on the
1449 kind of range this is. KIND will always be set to the appropriate
1450 value describing the kind of range, and this can be used to
1451 determine whether LOW or HIGH are valid. */
1452
1453static void
1454rust_compute_range (struct type *type, struct value *range,
1455 LONGEST *low, LONGEST *high,
01739a3b 1456 enum range_type *kind)
c44af4eb
TT
1457{
1458 int i;
1459
1460 *low = 0;
1461 *high = 0;
1462 *kind = BOTH_BOUND_DEFAULT;
1463
1464 if (TYPE_NFIELDS (type) == 0)
1465 return;
1466
1467 i = 0;
1468 if (strcmp (TYPE_FIELD_NAME (type, 0), "start") == 0)
1469 {
1470 *kind = HIGH_BOUND_DEFAULT;
1471 *low = value_as_long (value_field (range, 0));
1472 ++i;
1473 }
1474 if (TYPE_NFIELDS (type) > i
1475 && strcmp (TYPE_FIELD_NAME (type, i), "end") == 0)
1476 {
1477 *kind = (*kind == BOTH_BOUND_DEFAULT
1478 ? LOW_BOUND_DEFAULT : NONE_BOUND_DEFAULT);
1479 *high = value_as_long (value_field (range, i));
1480 }
1481}
1482
1483/* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */
1484
1485static struct value *
1486rust_subscript (struct expression *exp, int *pos, enum noside noside,
1487 int for_addr)
1488{
1489 struct value *lhs, *rhs, *result;
1490 struct type *rhstype;
45f4ed92 1491 LONGEST low, high_bound;
c44af4eb 1492 /* Initialized to appease the compiler. */
01739a3b 1493 enum range_type kind = BOTH_BOUND_DEFAULT;
45f4ed92 1494 LONGEST high = 0;
c44af4eb
TT
1495 int want_slice = 0;
1496
1497 ++*pos;
1498 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1499 rhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1500
1501 if (noside == EVAL_SKIP)
1502 return lhs;
1503
1504 rhstype = check_typedef (value_type (rhs));
1505 if (rust_range_type_p (rhstype))
1506 {
1507 if (!for_addr)
1508 error (_("Can't take slice of array without '&'"));
1509 rust_compute_range (rhstype, rhs, &low, &high, &kind);
1510 want_slice = 1;
1511 }
1512 else
1513 low = value_as_long (rhs);
1514
b3e3859b 1515 struct type *type = check_typedef (value_type (lhs));
c44af4eb
TT
1516 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1517 {
b3e3859b
TT
1518 struct type *base_type = nullptr;
1519 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1520 base_type = TYPE_TARGET_TYPE (type);
1521 else if (rust_slice_type_p (type))
1522 {
1523 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
1524 {
1525 if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
1526 {
1527 base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
1528 break;
1529 }
1530 }
1531 if (base_type == nullptr)
1532 error (_("Could not find 'data_ptr' in slice type"));
1533 }
1534 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1535 base_type = TYPE_TARGET_TYPE (type);
1536 else
1537 error (_("Cannot subscript non-array type"));
1538
1539 struct type *new_type;
1540 if (want_slice)
1541 {
1542 if (rust_slice_type_p (type))
1543 new_type = type;
1544 else
1545 {
1546 struct type *usize
1547 = language_lookup_primitive_type (exp->language_defn,
1548 exp->gdbarch,
1549 "usize");
1550 new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
1551 }
1552 }
1553 else
1554 new_type = base_type;
c44af4eb 1555
b3e3859b 1556 return value_zero (new_type, VALUE_LVAL (lhs));
c44af4eb
TT
1557 }
1558 else
1559 {
1560 LONGEST low_bound;
1561 struct value *base;
c44af4eb
TT
1562
1563 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1564 {
1565 base = lhs;
1566 if (!get_array_bounds (type, &low_bound, &high_bound))
1567 error (_("Can't compute array bounds"));
1568 if (low_bound != 0)
1569 error (_("Found array with non-zero lower bound"));
1570 ++high_bound;
1571 }
1572 else if (rust_slice_type_p (type))
1573 {
1574 struct value *len;
1575
1576 base = value_struct_elt (&lhs, NULL, "data_ptr", NULL, "slice");
1577 len = value_struct_elt (&lhs, NULL, "length", NULL, "slice");
1578 low_bound = 0;
1579 high_bound = value_as_long (len);
1580 }
42d94011
MG
1581 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
1582 {
1583 base = lhs;
1584 low_bound = 0;
1585 high_bound = LONGEST_MAX;
1586 }
c44af4eb
TT
1587 else
1588 error (_("Cannot subscript non-array type"));
1589
1590 if (want_slice
1591 && (kind == BOTH_BOUND_DEFAULT || kind == LOW_BOUND_DEFAULT))
1592 low = low_bound;
1593 if (low < 0)
1594 error (_("Index less than zero"));
1595 if (low > high_bound)
1596 error (_("Index greater than length"));
1597
1598 result = value_subscript (base, low);
1599 }
1600
1601 if (for_addr)
1602 {
1603 if (want_slice)
1604 {
1605 struct type *usize, *slice;
1606 CORE_ADDR addr;
1607 struct value *addrval, *tem;
1608
1609 if (kind == BOTH_BOUND_DEFAULT || kind == HIGH_BOUND_DEFAULT)
1610 high = high_bound;
1611 if (high < 0)
1612 error (_("High index less than zero"));
1613 if (low > high)
1614 error (_("Low index greater than high index"));
1615 if (high > high_bound)
1616 error (_("High index greater than length"));
1617
1618 usize = language_lookup_primitive_type (exp->language_defn,
1619 exp->gdbarch,
1620 "usize");
45320ffa
TT
1621 const char *new_name = ((type != nullptr
1622 && rust_slice_type_p (type))
1623 ? TYPE_NAME (type) : "&[*gdb*]");
1624
1625 slice = rust_slice_type (new_name, value_type (result), usize);
c44af4eb
TT
1626
1627 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
1628 addr = value_as_long (addrval);
1629 tem = value_at_lazy (slice, addr);
1630
1631 value_assign (value_field (tem, 0), value_addr (result));
1632 value_assign (value_field (tem, 1),
1633 value_from_longest (usize, high - low));
1634
1635 result = value_at_lazy (slice, addr);
1636 }
1637 else
1638 result = value_addr (result);
1639 }
1640
1641 return result;
1642}
1643
1644/* evaluate_exp implementation for Rust. */
1645
1646static struct value *
1647rust_evaluate_subexp (struct type *expect_type, struct expression *exp,
1648 int *pos, enum noside noside)
1649{
1650 struct value *result;
1651
1652 switch (exp->elts[*pos].opcode)
1653 {
71a3c369
TT
1654 case UNOP_IND:
1655 {
1656 if (noside != EVAL_NORMAL)
1657 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1658 else
1659 {
1660 ++*pos;
1661 struct value *value = evaluate_subexp (expect_type, exp, pos,
1662 noside);
1663
1664 struct value *trait_ptr = rust_get_trait_object_pointer (value);
1665 if (trait_ptr != NULL)
1666 value = trait_ptr;
1667
1668 result = value_ind (value);
1669 }
1670 }
1671 break;
1672
c44af4eb
TT
1673 case UNOP_COMPLEMENT:
1674 {
1675 struct value *value;
1676
1677 ++*pos;
1678 value = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1679 if (noside == EVAL_SKIP)
1680 {
1681 /* Preserving the type is enough. */
1682 return value;
1683 }
1684 if (TYPE_CODE (value_type (value)) == TYPE_CODE_BOOL)
1685 result = value_from_longest (value_type (value),
1686 value_logical_not (value));
1687 else
1688 result = value_complement (value);
1689 }
1690 break;
1691
1692 case BINOP_SUBSCRIPT:
1693 result = rust_subscript (exp, pos, noside, 0);
1694 break;
1695
1696 case OP_FUNCALL:
1697 result = rust_evaluate_funcall (exp, pos, noside);
1698 break;
1699
1700 case OP_AGGREGATE:
1701 {
1702 int pc = (*pos)++;
1703 struct type *type = exp->elts[pc + 1].type;
1704 int arglen = longest_to_int (exp->elts[pc + 2].longconst);
1705 int i;
1706 CORE_ADDR addr = 0;
1707 struct value *addrval = NULL;
1708
1709 *pos += 3;
1710
1711 if (noside == EVAL_NORMAL)
1712 {
1713 addrval = value_allocate_space_in_inferior (TYPE_LENGTH (type));
1714 addr = value_as_long (addrval);
1715 result = value_at_lazy (type, addr);
1716 }
1717
1718 if (arglen > 0 && exp->elts[*pos].opcode == OP_OTHERS)
1719 {
1720 struct value *init;
1721
1722 ++*pos;
1723 init = rust_evaluate_subexp (NULL, exp, pos, noside);
1724 if (noside == EVAL_NORMAL)
1725 {
1726 /* This isn't quite right but will do for the time
1727 being, seeing that we can't implement the Copy
1728 trait anyway. */
1729 value_assign (result, init);
1730 }
1731
1732 --arglen;
1733 }
1734
1735 gdb_assert (arglen % 2 == 0);
1736 for (i = 0; i < arglen; i += 2)
1737 {
1738 int len;
1739 const char *fieldname;
1740 struct value *value, *field;
1741
1742 gdb_assert (exp->elts[*pos].opcode == OP_NAME);
1743 ++*pos;
1744 len = longest_to_int (exp->elts[*pos].longconst);
1745 ++*pos;
1746 fieldname = &exp->elts[*pos].string;
1747 *pos += 2 + BYTES_TO_EXP_ELEM (len + 1);
1748
1749 value = rust_evaluate_subexp (NULL, exp, pos, noside);
1750 if (noside == EVAL_NORMAL)
1751 {
1752 field = value_struct_elt (&result, NULL, fieldname, NULL,
1753 "structure");
1754 value_assign (field, value);
1755 }
1756 }
1757
1758 if (noside == EVAL_SKIP)
1759 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
1760 1);
1761 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1762 result = allocate_value (type);
1763 else
1764 result = value_at_lazy (type, addr);
1765 }
1766 break;
1767
1768 case OP_RUST_ARRAY:
1769 {
1770 int pc = (*pos)++;
1771 int copies;
1772 struct value *elt;
1773 struct value *ncopies;
1774
1775 elt = rust_evaluate_subexp (NULL, exp, pos, noside);
1776 ncopies = rust_evaluate_subexp (NULL, exp, pos, noside);
1777 copies = value_as_long (ncopies);
1778 if (copies < 0)
1779 error (_("Array with negative number of elements"));
1780
1781 if (noside == EVAL_NORMAL)
1782 {
1783 CORE_ADDR addr;
1784 int i;
ab8b80a8 1785 std::vector<struct value *> eltvec (copies);
c44af4eb
TT
1786
1787 for (i = 0; i < copies; ++i)
1788 eltvec[i] = elt;
ab8b80a8 1789 result = value_array (0, copies - 1, eltvec.data ());
c44af4eb
TT
1790 }
1791 else
1792 {
1793 struct type *arraytype
1794 = lookup_array_range_type (value_type (elt), 0, copies - 1);
1795 result = allocate_value (arraytype);
1796 }
1797 }
1798 break;
1799
1800 case STRUCTOP_ANONYMOUS:
1801 {
1802 /* Anonymous field access, i.e. foo.1. */
1803 struct value *lhs;
1804 int pc, field_number, nfields;
1805 struct type *type, *variant_type;
1806 struct disr_info disr;
1807
1808 pc = (*pos)++;
1809 field_number = longest_to_int (exp->elts[pc + 1].longconst);
1810 (*pos) += 2;
1811 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1812
1813 type = value_type (lhs);
b96645f1
MG
1814 /* Untagged unions can't have anonymous field access since
1815 they can only have named fields. */
1816 if (TYPE_CODE (type) == TYPE_CODE_UNION
1817 && !rust_union_is_untagged (type))
c44af4eb 1818 {
c44af4eb
TT
1819 disr = rust_get_disr_info (type, value_contents (lhs),
1820 value_embedded_offset (lhs),
1821 value_address (lhs), lhs);
1822
c44af4eb
TT
1823 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1824 {
1825 variant_type = NULL;
1826 nfields = 0;
1827 }
1828 else
1829 {
1830 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1831 nfields = TYPE_NFIELDS (variant_type);
1832 }
1833
1834 if (!disr.is_encoded)
1835 ++field_number;
1836
1837 if (field_number >= nfields || field_number < 0)
1838 error(_("Cannot access field %d of variant %s, \
1839there are only %d fields"),
1840 disr.is_encoded ? field_number : field_number - 1,
5698084a 1841 disr.name.c_str (),
c44af4eb
TT
1842 disr.is_encoded ? nfields : nfields - 1);
1843
1844 if (!(disr.is_encoded
1845 ? rust_tuple_struct_type_p (variant_type)
1846 : rust_tuple_variant_type_p (variant_type)))
5698084a 1847 error(_("Variant %s is not a tuple variant"), disr.name.c_str ());
c44af4eb
TT
1848
1849 result = value_primitive_field (lhs, 0, field_number,
1850 variant_type);
c44af4eb
TT
1851 }
1852 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1853 {
1854 /* Tuples and tuple structs */
1855 nfields = TYPE_NFIELDS(type);
1856
1857 if (field_number >= nfields || field_number < 0)
1858 error(_("Cannot access field %d of %s, there are only %d fields"),
1859 field_number, TYPE_TAG_NAME (type), nfields);
1860
1861 /* Tuples are tuple structs too. */
1862 if (!rust_tuple_struct_type_p (type))
1863 error(_("Attempting to access anonymous field %d of %s, which is \
1864not a tuple, tuple struct, or tuple-like variant"),
1865 field_number, TYPE_TAG_NAME (type));
1866
1867 result = value_primitive_field (lhs, 0, field_number, type);
1868 }
1869 else
1870 error(_("Anonymous field access is only allowed on tuples, \
1871tuple structs, and tuple-like enum variants"));
1872 }
1873 break;
1874
1875 case STRUCTOP_STRUCT:
1876 {
6830f270 1877 struct value *lhs;
c44af4eb
TT
1878 struct type *type;
1879 int tem, pc;
1880
1881 pc = (*pos)++;
1882 tem = longest_to_int (exp->elts[pc + 1].longconst);
1883 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1884 lhs = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1885
6830f270 1886 const char *field_name = &exp->elts[pc + 2].string;
c44af4eb 1887 type = value_type (lhs);
b96645f1
MG
1888 if (TYPE_CODE (type) == TYPE_CODE_UNION
1889 && !rust_union_is_untagged (type))
c44af4eb
TT
1890 {
1891 int i, start;
1892 struct disr_info disr;
6830f270 1893 struct type *variant_type;
c44af4eb
TT
1894
1895 disr = rust_get_disr_info (type, value_contents (lhs),
1896 value_embedded_offset (lhs),
1897 value_address (lhs), lhs);
1898
c44af4eb
TT
1899 if (disr.is_encoded && disr.field_no == RUST_ENCODED_ENUM_HIDDEN)
1900 error(_("Could not find field %s of struct variant %s"),
5698084a 1901 field_name, disr.name.c_str ());
c44af4eb
TT
1902
1903 variant_type = TYPE_FIELD_TYPE (type, disr.field_no);
1904
1905 if (variant_type == NULL
51a789c3
MG
1906 || (disr.is_encoded
1907 ? rust_tuple_struct_type_p (variant_type)
1908 : rust_tuple_variant_type_p (variant_type)))
c44af4eb
TT
1909 error(_("Attempting to access named field %s of tuple variant %s, \
1910which has only anonymous fields"),
5698084a 1911 field_name, disr.name.c_str ());
c44af4eb
TT
1912
1913 start = disr.is_encoded ? 0 : 1;
1914 for (i = start; i < TYPE_NFIELDS (variant_type); i++)
1915 {
1916 if (strcmp (TYPE_FIELD_NAME (variant_type, i),
1917 field_name) == 0) {
1918 result = value_primitive_field (lhs, 0, i, variant_type);
1919 break;
1920 }
1921 }
1922
1923 if (i == TYPE_NFIELDS (variant_type))
1924 /* We didn't find it. */
1925 error(_("Could not find field %s of struct variant %s"),
5698084a 1926 field_name, disr.name.c_str ());
c44af4eb
TT
1927 }
1928 else
1929 {
6830f270
TT
1930 result = value_struct_elt (&lhs, NULL, field_name, NULL,
1931 "structure");
1932 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1933 result = value_zero (value_type (result), VALUE_LVAL (result));
c44af4eb
TT
1934 }
1935 }
1936 break;
1937
01739a3b 1938 case OP_RANGE:
c44af4eb
TT
1939 result = rust_range (exp, pos, noside);
1940 break;
1941
1942 case UNOP_ADDR:
1943 /* We might have &array[range], in which case we need to make a
1944 slice. */
1945 if (exp->elts[*pos + 1].opcode == BINOP_SUBSCRIPT)
1946 {
1947 ++*pos;
1948 result = rust_subscript (exp, pos, noside, 1);
1949 break;
1950 }
1951 /* Fall through. */
1952 default:
1953 result = evaluate_subexp_standard (expect_type, exp, pos, noside);
1954 break;
1955 }
1956
1957 return result;
1958}
1959
1960/* operator_length implementation for Rust. */
1961
1962static void
1963rust_operator_length (const struct expression *exp, int pc, int *oplenp,
1964 int *argsp)
1965{
1966 int oplen = 1;
1967 int args = 0;
1968
1969 switch (exp->elts[pc - 1].opcode)
1970 {
1971 case OP_AGGREGATE:
1972 /* We handle aggregate as a type and argument count. The first
1973 argument might be OP_OTHERS. After that the arguments
1974 alternate: first an OP_NAME, then an expression. */
1975 oplen = 4;
1976 args = longest_to_int (exp->elts[pc - 2].longconst);
1977 break;
1978
1979 case OP_OTHERS:
1980 oplen = 1;
1981 args = 1;
1982 break;
1983
1984 case STRUCTOP_ANONYMOUS:
1985 oplen = 3;
1986 args = 1;
1987 break;
1988
1989 case OP_RUST_ARRAY:
1990 oplen = 1;
1991 args = 2;
1992 break;
1993
1994 default:
1995 operator_length_standard (exp, pc, oplenp, argsp);
1996 return;
1997 }
1998
1999 *oplenp = oplen;
2000 *argsp = args;
2001}
2002
2003/* op_name implementation for Rust. */
2004
a121b7c1 2005static const char *
c44af4eb
TT
2006rust_op_name (enum exp_opcode opcode)
2007{
2008 switch (opcode)
2009 {
2010 case OP_AGGREGATE:
2011 return "OP_AGGREGATE";
2012 case OP_OTHERS:
2013 return "OP_OTHERS";
2014 default:
2015 return op_name_standard (opcode);
2016 }
2017}
2018
2019/* dump_subexp_body implementation for Rust. */
2020
2021static int
2022rust_dump_subexp_body (struct expression *exp, struct ui_file *stream,
2023 int elt)
2024{
2025 switch (exp->elts[elt].opcode)
2026 {
2027 case OP_AGGREGATE:
2028 {
2029 int length = longest_to_int (exp->elts[elt + 2].longconst);
2030 int i;
2031
2032 fprintf_filtered (stream, "Type @");
2033 gdb_print_host_address (exp->elts[elt + 1].type, stream);
2034 fprintf_filtered (stream, " (");
2035 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
2036 fprintf_filtered (stream, "), length %d", length);
2037
2038 elt += 4;
2039 for (i = 0; i < length; ++i)
2040 elt = dump_subexp (exp, stream, elt);
2041 }
2042 break;
2043
2044 case OP_STRING:
2045 case OP_NAME:
2046 {
2047 LONGEST len = exp->elts[elt + 1].longconst;
2048
2049 fprintf_filtered (stream, "%s: %s",
2050 (exp->elts[elt].opcode == OP_STRING
2051 ? "string" : "name"),
2052 &exp->elts[elt + 2].string);
2053 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
2054 }
2055 break;
2056
2057 case OP_OTHERS:
2058 elt = dump_subexp (exp, stream, elt + 1);
2059 break;
2060
2061 case STRUCTOP_ANONYMOUS:
2062 {
2063 int field_number;
2064
68f2f2e3 2065 field_number = longest_to_int (exp->elts[elt + 1].longconst);
c44af4eb
TT
2066
2067 fprintf_filtered (stream, "Field number: %d", field_number);
68f2f2e3 2068 elt = dump_subexp (exp, stream, elt + 3);
c44af4eb
TT
2069 }
2070 break;
2071
2072 case OP_RUST_ARRAY:
68f2f2e3 2073 ++elt;
c44af4eb
TT
2074 break;
2075
2076 default:
2077 elt = dump_subexp_body_standard (exp, stream, elt);
2078 break;
2079 }
2080
2081 return elt;
2082}
2083
2084/* print_subexp implementation for Rust. */
2085
2086static void
2087rust_print_subexp (struct expression *exp, int *pos, struct ui_file *stream,
2088 enum precedence prec)
2089{
2090 switch (exp->elts[*pos].opcode)
2091 {
2092 case OP_AGGREGATE:
2093 {
2094 int length = longest_to_int (exp->elts[*pos + 2].longconst);
2095 int i;
2096
2097 type_print (exp->elts[*pos + 1].type, "", stream, 0);
2098 fputs_filtered (" { ", stream);
2099
2100 *pos += 4;
2101 for (i = 0; i < length; ++i)
2102 {
2103 rust_print_subexp (exp, pos, stream, prec);
2104 fputs_filtered (", ", stream);
2105 }
2106 fputs_filtered (" }", stream);
2107 }
2108 break;
2109
2110 case OP_NAME:
2111 {
2112 LONGEST len = exp->elts[*pos + 1].longconst;
2113
2114 fputs_filtered (&exp->elts[*pos + 2].string, stream);
2115 *pos += 4 + BYTES_TO_EXP_ELEM (len + 1);
2116 }
2117 break;
2118
2119 case OP_OTHERS:
2120 {
2121 fputs_filtered ("<<others>> (", stream);
2122 ++*pos;
2123 rust_print_subexp (exp, pos, stream, prec);
2124 fputs_filtered (")", stream);
2125 }
2126 break;
2127
2128 case STRUCTOP_ANONYMOUS:
2129 {
2130 int tem = longest_to_int (exp->elts[*pos + 1].longconst);
2131
2132 (*pos) += 3;
2133 print_subexp (exp, pos, stream, PREC_SUFFIX);
2134 fprintf_filtered (stream, ".%d", tem);
2135 }
256afbc2 2136 break;
c44af4eb
TT
2137
2138 case OP_RUST_ARRAY:
2139 ++*pos;
2140 fprintf_filtered (stream, "[");
2141 rust_print_subexp (exp, pos, stream, prec);
2142 fprintf_filtered (stream, "; ");
2143 rust_print_subexp (exp, pos, stream, prec);
2144 fprintf_filtered (stream, "]");
2145 break;
2146
2147 default:
2148 print_subexp_standard (exp, pos, stream, prec);
2149 break;
2150 }
2151}
2152
2153/* operator_check implementation for Rust. */
2154
2155static int
2156rust_operator_check (struct expression *exp, int pos,
2157 int (*objfile_func) (struct objfile *objfile,
2158 void *data),
2159 void *data)
2160{
2161 switch (exp->elts[pos].opcode)
2162 {
2163 case OP_AGGREGATE:
2164 {
2165 struct type *type = exp->elts[pos + 1].type;
2166 struct objfile *objfile = TYPE_OBJFILE (type);
2167
2168 if (objfile != NULL && (*objfile_func) (objfile, data))
2169 return 1;
2170 }
2171 break;
2172
2173 case OP_OTHERS:
2174 case OP_NAME:
2175 case OP_RUST_ARRAY:
2176 break;
2177
2178 default:
2179 return operator_check_standard (exp, pos, objfile_func, data);
2180 }
2181
2182 return 0;
2183}
2184
2185\f
2186
2187/* Implementation of la_lookup_symbol_nonlocal for Rust. */
2188
2189static struct block_symbol
2190rust_lookup_symbol_nonlocal (const struct language_defn *langdef,
2191 const char *name,
2192 const struct block *block,
2193 const domain_enum domain)
2194{
2195 struct block_symbol result = {NULL, NULL};
2196
2197 if (symbol_lookup_debug)
2198 {
2199 fprintf_unfiltered (gdb_stdlog,
2200 "rust_lookup_symbol_non_local"
2201 " (%s, %s (scope %s), %s)\n",
2202 name, host_address_to_string (block),
2203 block_scope (block), domain_name (domain));
2204 }
2205
2206 /* Look up bare names in the block's scope. */
2207 if (name[cp_find_first_component (name)] == '\0')
2208 {
2209 const char *scope = block_scope (block);
2210
2211 if (scope[0] != '\0')
2212 {
ab8b80a8 2213 std::string scopedname = std::string (scope) + "::" + name;
c44af4eb 2214
ab8b80a8 2215 result = lookup_symbol_in_static_block (scopedname.c_str (), block,
c44af4eb
TT
2216 domain);
2217 if (result.symbol == NULL)
ab8b80a8 2218 result = lookup_global_symbol (scopedname.c_str (), block, domain);
c44af4eb
TT
2219 }
2220 }
2221 return result;
2222}
2223
2224\f
2225
8b302db8
TT
2226/* la_sniff_from_mangled_name for Rust. */
2227
2228static int
2229rust_sniff_from_mangled_name (const char *mangled, char **demangled)
2230{
2231 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
2232 return *demangled != NULL;
2233}
2234
2235\f
2236
43cc5389
TT
2237/* la_watch_location_expression for Rust. */
2238
2239static gdb::unique_xmalloc_ptr<char>
2240rust_watch_location_expression (struct type *type, CORE_ADDR addr)
2241{
2242 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
2243 std::string name = type_to_string (type);
2244 return gdb::unique_xmalloc_ptr<char>
2245 (xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
2246 name.c_str ()));
2247}
2248
2249\f
2250
c44af4eb
TT
2251static const struct exp_descriptor exp_descriptor_rust =
2252{
2253 rust_print_subexp,
2254 rust_operator_length,
2255 rust_operator_check,
2256 rust_op_name,
2257 rust_dump_subexp_body,
2258 rust_evaluate_subexp
2259};
2260
56618e20
TT
2261static const char *rust_extensions[] =
2262{
2263 ".rs", NULL
2264};
2265
47e77640 2266extern const struct language_defn rust_language_defn =
c44af4eb
TT
2267{
2268 "rust",
2269 "Rust",
2270 language_rust,
2271 range_check_on,
2272 case_sensitive_on,
2273 array_row_major,
2274 macro_expansion_no,
56618e20 2275 rust_extensions,
c44af4eb
TT
2276 &exp_descriptor_rust,
2277 rust_parse,
2278 rustyyerror,
2279 null_post_parser,
2280 rust_printchar, /* Print a character constant */
2281 rust_printstr, /* Function to print string constant */
2282 rust_emitchar, /* Print a single char */
2283 rust_print_type, /* Print a type using appropriate syntax */
2284 rust_print_typedef, /* Print a typedef using appropriate syntax */
2285 rust_val_print, /* Print a value using appropriate syntax */
2286 c_value_print, /* Print a top-level value */
2287 default_read_var_value, /* la_read_var_value */
2288 NULL, /* Language specific skip_trampoline */
2289 NULL, /* name_of_this */
2290 rust_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
2291 basic_lookup_transparent_type,/* lookup_transparent_type */
2292 gdb_demangle, /* Language specific symbol demangler */
8b302db8 2293 rust_sniff_from_mangled_name,
c44af4eb
TT
2294 NULL, /* Language specific
2295 class_name_from_physname */
2296 c_op_print_tab, /* expression operators for printing */
2297 1, /* c-style arrays */
2298 0, /* String lower bound */
2299 default_word_break_characters,
eb3ff9a5 2300 default_collect_symbol_completion_matches,
c44af4eb
TT
2301 rust_language_arch_info,
2302 default_print_array_index,
2303 default_pass_by_reference,
2304 c_get_string,
43cc5389 2305 rust_watch_location_expression,
b5ec771e 2306 NULL, /* la_get_symbol_name_matcher */
c44af4eb 2307 iterate_over_symbols,
5ffa0793 2308 default_search_name_hash,
c44af4eb
TT
2309 &default_varobj_ops,
2310 NULL,
2311 NULL,
2312 LANG_MAGIC
2313};