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