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