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