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