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