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