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