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