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