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