]>
Commit | Line | Data |
---|---|---|
c44af4eb TT |
1 | /* Rust language support routines for GDB, the GNU debugger. |
2 | ||
d01e8234 | 3 | Copyright (C) 2016-2025 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 | ||
c44af4eb TT |
20 | |
21 | #include <ctype.h> | |
22 | ||
23 | #include "block.h" | |
24 | #include "c-lang.h" | |
25 | #include "charset.h" | |
26 | #include "cp-support.h" | |
8b302db8 | 27 | #include "demangle.h" |
e5dc0d5d | 28 | #include "event-top.h" |
c44af4eb TT |
29 | #include "gdbarch.h" |
30 | #include "infcall.h" | |
31 | #include "objfiles.h" | |
32 | #include "rust-lang.h" | |
a33ccfc7 | 33 | #include "typeprint.h" |
c44af4eb TT |
34 | #include "valprint.h" |
35 | #include "varobj.h" | |
a33ccfc7 | 36 | #include <algorithm> |
ab8b80a8 TT |
37 | #include <string> |
38 | #include <vector> | |
7f6aba03 | 39 | #include "cli/cli-style.h" |
af30c400 | 40 | #include "parser-defs.h" |
6fab4359 | 41 | #include "rust-exp.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 | { | |
3c45e9f9 | 60 | const char *scope = block->scope (); |
c44af4eb TT |
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) |
454977cd | 96 | if (!type->field (i).is_artificial ()) |
9c6a1327 | 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 |
254809b2 TT |
119 | sequence like 0, 1, 2. "__" prefixes are also accepted -- rustc |
120 | emits "__0" but gccrs emits "0". */ | |
c44af4eb | 121 | |
65c40c95 | 122 | static bool |
c9317f21 | 123 | rust_underscore_fields (struct type *type) |
c44af4eb | 124 | { |
db7e13a9 | 125 | int field_number = 0; |
c44af4eb | 126 | |
78134374 | 127 | if (type->code () != TYPE_CODE_STRUCT) |
65c40c95 | 128 | return false; |
db7e13a9 | 129 | for (int i = 0; i < type->num_fields (); ++i) |
c44af4eb | 130 | { |
c819a338 | 131 | if (!type->field (i).is_static ()) |
c44af4eb | 132 | { |
c9317f21 | 133 | char buf[20]; |
c44af4eb | 134 | |
254809b2 TT |
135 | xsnprintf (buf, sizeof (buf), "%d", field_number); |
136 | ||
137 | const char *field_name = type->field (i).name (); | |
138 | if (startswith (field_name, "__")) | |
139 | field_name += 2; | |
140 | if (strcmp (buf, field_name) != 0) | |
c9317f21 TT |
141 | return false; |
142 | field_number++; | |
c44af4eb TT |
143 | } |
144 | } | |
65c40c95 | 145 | return true; |
c44af4eb TT |
146 | } |
147 | ||
148 | /* See rust-lang.h. */ | |
149 | ||
65c40c95 | 150 | bool |
c44af4eb TT |
151 | rust_tuple_struct_type_p (struct type *type) |
152 | { | |
12df5c00 TT |
153 | /* This is just an approximation until DWARF can represent Rust more |
154 | precisely. We exclude zero-length structs because they may not | |
155 | be tuple structs, and there's no way to tell. */ | |
1f704f76 | 156 | return type->num_fields () > 0 && rust_underscore_fields (type); |
c44af4eb TT |
157 | } |
158 | ||
b0dd661f | 159 | /* Return true if TYPE is "slice-like"; false otherwise. */ |
c44af4eb | 160 | |
b0dd661f | 161 | static bool |
49ed499c | 162 | rust_slice_type_p (const struct type *type) |
c44af4eb | 163 | { |
506ec52e TT |
164 | if (type->code () == TYPE_CODE_STRUCT |
165 | && type->name () != NULL | |
166 | && type->num_fields () == 2) | |
167 | { | |
168 | /* The order of fields doesn't matter. While it would be nice | |
169 | to check for artificiality here, the Rust compiler doesn't | |
170 | emit this information. */ | |
171 | const char *n1 = type->field (0).name (); | |
172 | const char *n2 = type->field (1).name (); | |
173 | return ((streq (n1, "data_ptr") && streq (n2, "length")) | |
174 | || (streq (n2, "data_ptr") && streq (n1, "length"))); | |
175 | } | |
176 | return false; | |
c44af4eb TT |
177 | } |
178 | ||
179 | /* Return true if TYPE is a range type, otherwise false. */ | |
180 | ||
65c40c95 | 181 | static bool |
c44af4eb TT |
182 | rust_range_type_p (struct type *type) |
183 | { | |
78134374 | 184 | if (type->code () != TYPE_CODE_STRUCT |
1f704f76 | 185 | || type->num_fields () > 2 |
7d93a1e0 SM |
186 | || type->name () == NULL |
187 | || strstr (type->name (), "::Range") == NULL) | |
65c40c95 | 188 | return false; |
c44af4eb | 189 | |
1f704f76 | 190 | if (type->num_fields () == 0) |
65c40c95 | 191 | return true; |
c44af4eb | 192 | |
db7e13a9 | 193 | int field_num = 0; |
33d16dd9 | 194 | if (strcmp (type->field (0).name (), "start") == 0) |
c44af4eb | 195 | { |
1f704f76 | 196 | if (type->num_fields () == 1) |
65c40c95 | 197 | return true; |
db7e13a9 | 198 | field_num = 1; |
c44af4eb | 199 | } |
1f704f76 | 200 | else if (type->num_fields () == 2) |
c44af4eb TT |
201 | { |
202 | /* First field had to be "start". */ | |
65c40c95 | 203 | return false; |
c44af4eb TT |
204 | } |
205 | ||
db7e13a9 | 206 | return strcmp (type->field (field_num).name (), "end") == 0; |
c44af4eb TT |
207 | } |
208 | ||
6873858b TT |
209 | /* Return true if TYPE is an inclusive range type, otherwise false. |
210 | This is only valid for types which are already known to be range | |
211 | types. */ | |
212 | ||
213 | static bool | |
214 | rust_inclusive_range_type_p (struct type *type) | |
215 | { | |
7d93a1e0 SM |
216 | return (strstr (type->name (), "::RangeInclusive") != NULL |
217 | || strstr (type->name (), "::RangeToInclusive") != NULL); | |
6873858b TT |
218 | } |
219 | ||
c44af4eb TT |
220 | /* Return true if TYPE seems to be the type "u8", otherwise false. */ |
221 | ||
65c40c95 | 222 | static bool |
c44af4eb TT |
223 | rust_u8_type_p (struct type *type) |
224 | { | |
78134374 | 225 | return (type->code () == TYPE_CODE_INT |
c6d940a9 | 226 | && type->is_unsigned () |
df86565b | 227 | && type->length () == 1); |
c44af4eb TT |
228 | } |
229 | ||
230 | /* Return true if TYPE is a Rust character type. */ | |
231 | ||
65c40c95 | 232 | static bool |
c44af4eb TT |
233 | rust_chartype_p (struct type *type) |
234 | { | |
78134374 | 235 | return (type->code () == TYPE_CODE_CHAR |
df86565b | 236 | && type->length () == 4 |
c6d940a9 | 237 | && type->is_unsigned ()); |
c44af4eb TT |
238 | } |
239 | ||
71a3c369 TT |
240 | /* If VALUE represents a trait object pointer, return the underlying |
241 | pointer with the correct (i.e., runtime) type. Otherwise, return | |
242 | NULL. */ | |
243 | ||
244 | static struct value * | |
245 | rust_get_trait_object_pointer (struct value *value) | |
246 | { | |
d0c97917 | 247 | struct type *type = check_typedef (value->type ()); |
71a3c369 | 248 | |
1f704f76 | 249 | if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2) |
71a3c369 TT |
250 | return NULL; |
251 | ||
252 | /* Try to be a bit resilient if the ABI changes. */ | |
253 | int vtable_field = 0; | |
254 | for (int i = 0; i < 2; ++i) | |
255 | { | |
33d16dd9 | 256 | if (strcmp (type->field (i).name (), "vtable") == 0) |
71a3c369 | 257 | vtable_field = i; |
33d16dd9 | 258 | else if (strcmp (type->field (i).name (), "pointer") != 0) |
71a3c369 TT |
259 | return NULL; |
260 | } | |
261 | ||
262 | CORE_ADDR vtable = value_as_address (value_field (value, vtable_field)); | |
263 | struct symbol *symbol = find_symbol_at_address (vtable); | |
cf724bc9 | 264 | if (symbol == NULL || symbol->subclass != SYMBOL_RUST_VTABLE) |
71a3c369 TT |
265 | return NULL; |
266 | ||
267 | struct rust_vtable_symbol *vtable_sym | |
268 | = static_cast<struct rust_vtable_symbol *> (symbol); | |
269 | struct type *pointer_type = lookup_pointer_type (vtable_sym->concrete_type); | |
270 | return value_cast (pointer_type, value_field (value, 1 - vtable_field)); | |
271 | } | |
272 | ||
b0dd661f TT |
273 | /* Find and possibly rewrite the unsized part of a slice-like type. |
274 | ||
275 | This function has two modes. If the out parameters are both NULL, | |
276 | it will return true if an unsized member of IN_TYPE is found. | |
277 | ||
278 | If the out parameters are both non-NULL, it will do the same, but | |
279 | will also rewrite the unsized member's type to be an array of the | |
280 | appropriate type. BOUND is the upper bound of the new array. | |
281 | ||
282 | See convert_slice to understand the different kinds of unsized type | |
283 | and how they are represented. | |
284 | */ | |
285 | static bool | |
286 | rewrite_slice_type (struct type *in_type, struct type **new_type, | |
287 | LONGEST bound, ULONGEST *additional_length) | |
288 | { | |
289 | if (in_type->code () != TYPE_CODE_STRUCT) | |
290 | return false; | |
291 | ||
292 | unsigned nfields = in_type->num_fields (); | |
293 | if (nfields == 0) | |
294 | return false; | |
295 | ||
296 | struct type *rewritten; | |
297 | const field &field = in_type->field (nfields - 1); | |
298 | struct type *field_type = field.type (); | |
299 | if (field.loc_kind () == FIELD_LOC_KIND_BITPOS | |
300 | && field.loc_bitpos () == 8 * in_type->length ()) | |
301 | { | |
302 | if (additional_length == nullptr) | |
303 | return true; | |
304 | rewritten = lookup_array_range_type (field_type, 0, bound); | |
305 | *additional_length = rewritten->length (); | |
306 | } | |
307 | else | |
308 | { | |
309 | if (!rewrite_slice_type (field_type, &rewritten, bound, | |
310 | additional_length)) | |
311 | return false; | |
312 | if (additional_length == nullptr) | |
313 | return true; | |
314 | } | |
315 | ||
316 | struct type *result = copy_type (in_type); | |
317 | result->copy_fields (in_type); | |
318 | result->field (nfields - 1).set_type (rewritten); | |
319 | result->set_length (result->length () + *additional_length); | |
320 | ||
321 | *new_type = result; | |
322 | return true; | |
323 | } | |
324 | ||
325 | /* Convert a Rust slice to its "true" representation. | |
326 | ||
327 | The Rust compiler emits slices as "fat" pointers like: | |
328 | ||
329 | struct { payload *data_ptr; usize length } | |
330 | ||
331 | Any sort of unsized type is emitted this way. | |
332 | ||
333 | If 'payload' is a struct type, then it must be searched to see if | |
334 | the trailing field is unsized. This has to be done recursively (as | |
335 | in, if the final field in the struct type itself has struct type, | |
336 | then that type must be searched). In this scenario, the unsized | |
337 | field can be recognized because it does not contribute to the | |
338 | type's size. | |
339 | ||
340 | If 'payload' does not have a trailing unsized type, or if it is not | |
341 | of struct type, then this slice is "array-like". In this case | |
342 | rewriting will return an array. | |
343 | */ | |
344 | static struct value * | |
345 | convert_slice (struct value *val) | |
346 | { | |
347 | struct type *type = check_typedef (val->type ()); | |
348 | /* This must have been checked by the caller. */ | |
349 | gdb_assert (rust_slice_type_p (type)); | |
350 | ||
351 | struct value *len = value_struct_elt (&val, {}, "length", nullptr, | |
352 | "slice"); | |
353 | LONGEST llen = value_as_long (len); | |
354 | ||
355 | struct value *ptr = value_struct_elt (&val, {}, "data_ptr", nullptr, | |
356 | "slice"); | |
357 | struct type *original_type = ptr->type ()->target_type (); | |
358 | ULONGEST new_length_storage = 0; | |
359 | struct type *new_type = nullptr; | |
360 | if (!rewrite_slice_type (original_type, &new_type, llen - 1, | |
361 | &new_length_storage)) | |
362 | new_type = lookup_array_range_type (original_type, 0, llen - 1); | |
363 | ||
364 | struct value *result = value::allocate_lazy (new_type); | |
365 | result->set_lval (lval_memory); | |
366 | result->set_address (value_as_address (ptr)); | |
367 | result->fetch_lazy (); | |
368 | ||
369 | return result; | |
370 | } | |
371 | ||
372 | /* If TYPE is an array-like slice, return the element type; otherwise | |
373 | return NULL. */ | |
374 | static struct type * | |
375 | rust_array_like_element_type (struct type *type) | |
376 | { | |
377 | /* Caller must check this. */ | |
378 | gdb_assert (rust_slice_type_p (type)); | |
379 | for (int i = 0; i < type->num_fields (); ++i) | |
380 | { | |
381 | if (strcmp (type->field (i).name (), "data_ptr") == 0) | |
382 | { | |
383 | struct type *base_type = type->field (i).type ()->target_type (); | |
384 | if (rewrite_slice_type (base_type, nullptr, 0, nullptr)) | |
385 | return nullptr; | |
386 | return base_type; | |
387 | } | |
388 | } | |
389 | return nullptr; | |
390 | } | |
391 | ||
c44af4eb TT |
392 | \f |
393 | ||
1c485265 | 394 | /* See language.h. */ |
c44af4eb | 395 | |
1c485265 AB |
396 | void |
397 | rust_language::printstr (struct ui_file *stream, struct type *type, | |
398 | const gdb_byte *string, unsigned int length, | |
399 | const char *user_encoding, int force_ellipses, | |
400 | const struct value_print_options *options) const | |
c44af4eb TT |
401 | { |
402 | /* Rust always uses UTF-8, but let the caller override this if need | |
403 | be. */ | |
404 | const char *encoding = user_encoding; | |
405 | if (user_encoding == NULL || !*user_encoding) | |
406 | { | |
407 | /* In Rust strings, characters are "u8". */ | |
408 | if (rust_u8_type_p (type)) | |
409 | encoding = "UTF-8"; | |
410 | else | |
411 | { | |
412 | /* This is probably some C string, so let's let C deal with | |
413 | it. */ | |
3a3bb6eb TT |
414 | language_defn::printstr (stream, type, string, length, |
415 | user_encoding, force_ellipses, | |
416 | options); | |
c44af4eb TT |
417 | return; |
418 | } | |
419 | } | |
420 | ||
421 | /* This is not ideal as it doesn't use our character printer. */ | |
422 | generic_printstr (stream, type, string, length, encoding, force_ellipses, | |
423 | '"', 0, options); | |
424 | } | |
425 | ||
426 | \f | |
427 | ||
506ec52e TT |
428 | static const struct generic_val_print_decorations rust_decorations = |
429 | { | |
430 | /* Complex isn't used in Rust, but we provide C-ish values just in | |
431 | case. */ | |
432 | "", | |
433 | " + ", | |
434 | " * I", | |
435 | "true", | |
436 | "false", | |
437 | "()", | |
438 | "[", | |
439 | "]" | |
440 | }; | |
441 | ||
49ed499c TT |
442 | /* See rust-lang.h. */ |
443 | ||
444 | struct value * | |
445 | rust_slice_to_array (struct value *val) | |
446 | { | |
b0dd661f TT |
447 | val = convert_slice (val); |
448 | if (val->type ()->code () != TYPE_CODE_ARRAY) | |
449 | return nullptr; | |
450 | return val; | |
49ed499c TT |
451 | } |
452 | ||
506ec52e | 453 | /* Helper function to print a slice. */ |
45320ffa | 454 | |
b0dd661f TT |
455 | void |
456 | rust_language::val_print_slice | |
457 | (struct value *val, struct ui_file *stream, int recurse, | |
458 | const struct value_print_options *options) const | |
45320ffa | 459 | { |
b0dd661f | 460 | struct type *orig_type = check_typedef (val->type ()); |
45320ffa | 461 | |
b0dd661f | 462 | val = convert_slice (val); |
d0c97917 | 463 | struct type *type = check_typedef (val->type ()); |
506ec52e | 464 | |
b0dd661f TT |
465 | /* &str is handled here; but for all other slice types it is fine to |
466 | simply print the contents. */ | |
467 | if (orig_type->name () != nullptr | |
468 | && strcmp (orig_type->name (), "&str") == 0) | |
469 | { | |
470 | LONGEST low_bound, high_bound; | |
471 | if (get_array_bounds (type, &low_bound, &high_bound)) | |
506ec52e | 472 | { |
b0dd661f TT |
473 | val_print_string (type->target_type (), "UTF-8", |
474 | val->address (), high_bound - low_bound + 1, | |
475 | stream, options); | |
476 | return; | |
506ec52e TT |
477 | } |
478 | } | |
b0dd661f | 479 | |
251cedae TT |
480 | /* Print the slice type here. This was gdb's historical behavior |
481 | (from before unsized types were generically handled) and helps | |
482 | make it clear that the user is seeing a slice, not an array. | |
483 | Only arrays must be handled as the other cases are handled by | |
484 | value_print_inner. */ | |
485 | if (type->code () == TYPE_CODE_ARRAY) | |
486 | { | |
487 | type_print (orig_type, "", stream, -1); | |
488 | gdb_printf (stream, " "); | |
489 | } | |
490 | ||
b0dd661f | 491 | value_print_inner (val, stream, recurse, options); |
45320ffa TT |
492 | } |
493 | ||
1c485265 | 494 | /* See rust-lang.h. */ |
b96645f1 | 495 | |
1c485265 AB |
496 | void |
497 | rust_language::val_print_struct | |
498 | (struct value *val, struct ui_file *stream, int recurse, | |
499 | const struct value_print_options *options) const | |
b96645f1 | 500 | { |
b96645f1 | 501 | int first_field; |
d0c97917 | 502 | struct type *type = check_typedef (val->type ()); |
45320ffa | 503 | |
506ec52e | 504 | if (rust_slice_type_p (type)) |
45320ffa | 505 | { |
b0dd661f | 506 | val_print_slice (val, stream, recurse, options); |
45320ffa TT |
507 | return; |
508 | } | |
509 | ||
65c40c95 TT |
510 | bool is_tuple = rust_tuple_type_p (type); |
511 | bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type); | |
b96645f1 MG |
512 | struct value_print_options opts; |
513 | ||
514 | if (!is_tuple) | |
515 | { | |
7d93a1e0 | 516 | if (type->name () != NULL) |
6cb06a8c | 517 | gdb_printf (stream, "%s", type->name ()); |
b96645f1 | 518 | |
1f704f76 | 519 | if (type->num_fields () == 0) |
dda83cd7 | 520 | return; |
b96645f1 | 521 | |
7d93a1e0 | 522 | if (type->name () != NULL) |
0426ad51 | 523 | gdb_puts (" ", stream); |
b96645f1 MG |
524 | } |
525 | ||
526 | if (is_tuple || is_tuple_struct) | |
0426ad51 | 527 | gdb_puts ("(", stream); |
b96645f1 | 528 | else |
0426ad51 | 529 | gdb_puts ("{", stream); |
b96645f1 MG |
530 | |
531 | opts = *options; | |
dad6b350 | 532 | opts.deref_ref = false; |
b96645f1 MG |
533 | |
534 | first_field = 1; | |
db7e13a9 | 535 | for (int i = 0; i < type->num_fields (); ++i) |
b96645f1 | 536 | { |
c819a338 | 537 | if (type->field (i).is_static ()) |
dda83cd7 | 538 | continue; |
b96645f1 MG |
539 | |
540 | if (!first_field) | |
0426ad51 | 541 | gdb_puts (",", stream); |
b96645f1 MG |
542 | |
543 | if (options->prettyformat) | |
dda83cd7 | 544 | { |
0426ad51 | 545 | gdb_puts ("\n", stream); |
d0b1020b | 546 | print_spaces (2 + 2 * recurse, stream); |
dda83cd7 | 547 | } |
b96645f1 | 548 | else if (!first_field) |
0426ad51 | 549 | gdb_puts (" ", stream); |
b96645f1 MG |
550 | |
551 | first_field = 0; | |
552 | ||
553 | if (!is_tuple && !is_tuple_struct) | |
dda83cd7 | 554 | { |
33d16dd9 | 555 | fputs_styled (type->field (i).name (), |
3f0cbb04 | 556 | variable_name_style.style (), stream); |
0426ad51 | 557 | gdb_puts (": ", stream); |
dda83cd7 | 558 | } |
b96645f1 | 559 | |
887e7158 TT |
560 | common_val_print (value_field (val, i), stream, recurse + 1, &opts, |
561 | this); | |
b96645f1 MG |
562 | } |
563 | ||
564 | if (options->prettyformat) | |
565 | { | |
0426ad51 | 566 | gdb_puts ("\n", stream); |
d0b1020b | 567 | print_spaces (2 * recurse, stream); |
b96645f1 MG |
568 | } |
569 | ||
570 | if (is_tuple || is_tuple_struct) | |
0426ad51 | 571 | gdb_puts (")", stream); |
b96645f1 | 572 | else |
0426ad51 | 573 | gdb_puts ("}", stream); |
b96645f1 MG |
574 | } |
575 | ||
1c485265 | 576 | /* See rust-lang.h. */ |
c9317f21 | 577 | |
1c485265 AB |
578 | void |
579 | rust_language::print_enum (struct value *val, struct ui_file *stream, | |
580 | int recurse, | |
581 | const struct value_print_options *options) const | |
c9317f21 TT |
582 | { |
583 | struct value_print_options opts = *options; | |
d0c97917 | 584 | struct type *type = check_typedef (val->type ()); |
c9317f21 | 585 | |
dad6b350 | 586 | opts.deref_ref = false; |
c9317f21 | 587 | |
9c6a1327 | 588 | gdb_assert (rust_enum_p (type)); |
50888e42 | 589 | gdb::array_view<const gdb_byte> view |
efaf1ae0 | 590 | (val->contents_for_printing ().data (), |
d0c97917 | 591 | val->type ()->length ()); |
9feb2d07 | 592 | type = resolve_dynamic_type (type, view, val->address ()); |
9c6a1327 | 593 | |
098b2108 TT |
594 | if (rust_empty_enum_p (type)) |
595 | { | |
596 | /* Print the enum type name here to be more clear. */ | |
6cb06a8c TT |
597 | gdb_printf (stream, _("%s {%p[<No data fields>%p]}"), |
598 | type->name (), | |
599 | metadata_style.style ().ptr (), nullptr); | |
098b2108 TT |
600 | return; |
601 | } | |
602 | ||
9c6a1327 | 603 | int variant_fieldno = rust_enum_variant (type); |
debd0556 | 604 | val = val->primitive_field (0, variant_fieldno, type); |
940da03e | 605 | struct type *variant_type = type->field (variant_fieldno).type (); |
c9317f21 | 606 | |
1f704f76 | 607 | int nfields = variant_type->num_fields (); |
c9317f21 TT |
608 | |
609 | bool is_tuple = rust_tuple_struct_type_p (variant_type); | |
610 | ||
6cb06a8c | 611 | gdb_printf (stream, "%s", variant_type->name ()); |
c9317f21 TT |
612 | if (nfields == 0) |
613 | { | |
614 | /* In case of a nullary variant like 'None', just output | |
615 | the name. */ | |
616 | return; | |
617 | } | |
618 | ||
619 | /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */ | |
620 | if (is_tuple) | |
6cb06a8c | 621 | gdb_printf (stream, "("); |
c9317f21 TT |
622 | else |
623 | { | |
624 | /* struct variant. */ | |
6cb06a8c | 625 | gdb_printf (stream, "{"); |
c9317f21 TT |
626 | } |
627 | ||
628 | bool first_field = true; | |
8ac460b7 | 629 | for (int j = 0; j < nfields; j++) |
c9317f21 TT |
630 | { |
631 | if (!first_field) | |
0426ad51 | 632 | gdb_puts (", ", stream); |
c9317f21 TT |
633 | first_field = false; |
634 | ||
635 | if (!is_tuple) | |
6cb06a8c TT |
636 | gdb_printf (stream, "%ps: ", |
637 | styled_string (variable_name_style.style (), | |
638 | variant_type->field (j).name ())); | |
c9317f21 | 639 | |
887e7158 TT |
640 | common_val_print (value_field (val, j), stream, recurse + 1, &opts, |
641 | this); | |
c9317f21 TT |
642 | } |
643 | ||
644 | if (is_tuple) | |
0426ad51 | 645 | gdb_puts (")", stream); |
c9317f21 | 646 | else |
0426ad51 | 647 | gdb_puts ("}", stream); |
c9317f21 TT |
648 | } |
649 | ||
1c485265 AB |
650 | /* See language.h. */ |
651 | ||
652 | void | |
653 | rust_language::value_print_inner | |
654 | (struct value *val, struct ui_file *stream, int recurse, | |
655 | const struct value_print_options *options) const | |
5f56f7cb TT |
656 | { |
657 | struct value_print_options opts = *options; | |
dad6b350 | 658 | opts.deref_ref = true; |
5f56f7cb TT |
659 | |
660 | if (opts.prettyformat == Val_prettyformat_default) | |
661 | opts.prettyformat = (opts.prettyformat_structs | |
662 | ? Val_prettyformat : Val_no_prettyformat); | |
663 | ||
d0c97917 | 664 | struct type *type = check_typedef (val->type ()); |
78134374 | 665 | switch (type->code ()) |
c44af4eb TT |
666 | { |
667 | case TYPE_CODE_PTR: | |
668 | { | |
669 | LONGEST low_bound, high_bound; | |
670 | ||
27710edb SM |
671 | if (type->target_type ()->code () == TYPE_CODE_ARRAY |
672 | && rust_u8_type_p (type->target_type ()->target_type ()) | |
673 | && get_array_bounds (type->target_type (), &low_bound, | |
5f56f7cb TT |
674 | &high_bound)) |
675 | { | |
676 | /* We have a pointer to a byte string, so just print | |
677 | that. */ | |
27710edb | 678 | struct type *elttype = check_typedef (type->target_type ()); |
5f56f7cb | 679 | CORE_ADDR addr = value_as_address (val); |
8ee511af | 680 | struct gdbarch *arch = type->arch (); |
c44af4eb | 681 | |
5f56f7cb TT |
682 | if (opts.addressprint) |
683 | { | |
0426ad51 TT |
684 | gdb_puts (paddress (arch, addr), stream); |
685 | gdb_puts (" ", stream); | |
5f56f7cb | 686 | } |
c44af4eb | 687 | |
0426ad51 | 688 | gdb_puts ("b", stream); |
27710edb | 689 | val_print_string (elttype->target_type (), "ASCII", addr, |
5f56f7cb TT |
690 | high_bound - low_bound + 1, stream, |
691 | &opts); | |
692 | break; | |
693 | } | |
c44af4eb | 694 | } |
5f56f7cb | 695 | goto generic_print; |
c44af4eb | 696 | |
c44af4eb TT |
697 | case TYPE_CODE_INT: |
698 | /* Recognize the unit type. */ | |
df86565b | 699 | if (type->is_unsigned () && type->length () == 0 |
7d93a1e0 | 700 | && type->name () != NULL && strcmp (type->name (), "()") == 0) |
c44af4eb | 701 | { |
0426ad51 | 702 | gdb_puts ("()", stream); |
c44af4eb TT |
703 | break; |
704 | } | |
705 | goto generic_print; | |
706 | ||
707 | case TYPE_CODE_STRING: | |
708 | { | |
c44af4eb TT |
709 | LONGEST low_bound, high_bound; |
710 | ||
711 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
712 | error (_("Could not determine the array bounds")); | |
713 | ||
714 | /* If we see a plain TYPE_CODE_STRING, then we're printing a | |
715 | byte string, hence the choice of "ASCII" as the | |
716 | encoding. */ | |
0426ad51 | 717 | gdb_puts ("b", stream); |
27710edb | 718 | printstr (stream, type->target_type (), |
efaf1ae0 | 719 | val->contents_for_printing ().data (), |
1c485265 | 720 | high_bound - low_bound + 1, "ASCII", 0, &opts); |
c44af4eb TT |
721 | } |
722 | break; | |
723 | ||
724 | case TYPE_CODE_ARRAY: | |
725 | { | |
726 | LONGEST low_bound, high_bound; | |
727 | ||
728 | if (get_array_bounds (type, &low_bound, &high_bound) | |
729 | && high_bound - low_bound + 1 == 0) | |
0426ad51 | 730 | gdb_puts ("[]", stream); |
c44af4eb TT |
731 | else |
732 | goto generic_print; | |
733 | } | |
734 | break; | |
735 | ||
736 | case TYPE_CODE_UNION: | |
c9317f21 TT |
737 | /* Untagged unions are printed as if they are structs. Since |
738 | the field bit positions overlap in the debuginfo, the code | |
739 | for printing a union is same as that for a struct, the only | |
740 | difference is that the input type will have overlapping | |
741 | fields. */ | |
5f56f7cb | 742 | val_print_struct (val, stream, recurse, &opts); |
c44af4eb TT |
743 | break; |
744 | ||
745 | case TYPE_CODE_STRUCT: | |
c9317f21 | 746 | if (rust_enum_p (type)) |
1c485265 | 747 | print_enum (val, stream, recurse, &opts); |
c9317f21 | 748 | else |
5f56f7cb | 749 | val_print_struct (val, stream, recurse, &opts); |
b96645f1 | 750 | break; |
c44af4eb | 751 | |
b96645f1 MG |
752 | default: |
753 | generic_print: | |
754 | /* Nothing special yet. */ | |
5f56f7cb | 755 | generic_value_print (val, stream, recurse, &opts, &rust_decorations); |
b96645f1 MG |
756 | } |
757 | } | |
c44af4eb | 758 | |
1c9cc052 TT |
759 | /* See language.h. */ |
760 | ||
761 | void | |
762 | rust_language::value_print | |
763 | (struct value *val, struct ui_file *stream, | |
764 | const struct value_print_options *options) const | |
765 | { | |
766 | value_print_options opts = *options; | |
767 | opts.deref_ref = true; | |
768 | ||
d0c97917 | 769 | struct type *type = check_typedef (val->type ()); |
1c9cc052 TT |
770 | if (type->is_pointer_or_reference ()) |
771 | { | |
772 | gdb_printf (stream, "("); | |
d0c97917 | 773 | type_print (val->type (), "", stream, -1); |
1c9cc052 TT |
774 | gdb_printf (stream, ") "); |
775 | } | |
776 | ||
777 | return common_val_print (val, stream, 0, &opts, this); | |
778 | } | |
779 | ||
b96645f1 | 780 | \f |
c44af4eb | 781 | |
b96645f1 | 782 | static void |
c9317f21 TT |
783 | rust_internal_print_type (struct type *type, const char *varstring, |
784 | struct ui_file *stream, int show, int level, | |
785 | const struct type_print_options *flags, | |
a33ccfc7 | 786 | bool for_rust_enum, print_offset_data *podata); |
b96645f1 MG |
787 | |
788 | /* Print a struct or union typedef. */ | |
789 | static void | |
790 | rust_print_struct_def (struct type *type, const char *varstring, | |
b50f188d | 791 | struct ui_file *stream, int show, int level, |
c9317f21 | 792 | const struct type_print_options *flags, |
a33ccfc7 | 793 | bool for_rust_enum, print_offset_data *podata) |
b96645f1 | 794 | { |
b50f188d TT |
795 | /* Print a tuple type simply. */ |
796 | if (rust_tuple_type_p (type)) | |
797 | { | |
0426ad51 | 798 | gdb_puts (type->name (), stream); |
b50f188d TT |
799 | return; |
800 | } | |
c44af4eb | 801 | |
b50f188d TT |
802 | /* If we see a base class, delegate to C. */ |
803 | if (TYPE_N_BASECLASSES (type) > 0) | |
1c6fbf42 | 804 | c_print_type (type, varstring, stream, show, level, language_rust, flags); |
c44af4eb | 805 | |
a33ccfc7 TT |
806 | if (flags->print_offsets) |
807 | { | |
808 | /* Temporarily bump the level so that the output lines up | |
809 | correctly. */ | |
810 | level += 2; | |
811 | } | |
812 | ||
c9317f21 TT |
813 | /* Compute properties of TYPE here because, in the enum case, the |
814 | rest of the code ends up looking only at the variant part. */ | |
7d93a1e0 | 815 | const char *tagname = type->name (); |
c9317f21 TT |
816 | bool is_tuple_struct = rust_tuple_struct_type_p (type); |
817 | bool is_tuple = rust_tuple_type_p (type); | |
818 | bool is_enum = rust_enum_p (type); | |
b96645f1 | 819 | |
c9317f21 TT |
820 | if (for_rust_enum) |
821 | { | |
822 | /* Already printing an outer enum, so nothing to print here. */ | |
823 | } | |
824 | else | |
825 | { | |
826 | /* This code path is also used by unions and enums. */ | |
827 | if (is_enum) | |
828 | { | |
0426ad51 | 829 | gdb_puts ("enum ", stream); |
24e99c6c | 830 | dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS); |
8c2e4e06 SM |
831 | if (prop != nullptr && prop->kind () == PROP_TYPE) |
832 | type = prop->original_type (); | |
c9317f21 | 833 | } |
78134374 | 834 | else if (type->code () == TYPE_CODE_STRUCT) |
0426ad51 | 835 | gdb_puts ("struct ", stream); |
c9317f21 | 836 | else |
0426ad51 | 837 | gdb_puts ("union ", stream); |
b96645f1 | 838 | |
c9317f21 | 839 | if (tagname != NULL) |
0426ad51 | 840 | gdb_puts (tagname, stream); |
c9317f21 | 841 | } |
b96645f1 | 842 | |
1f704f76 | 843 | if (type->num_fields () == 0 && !is_tuple) |
b50f188d | 844 | return; |
a33ccfc7 | 845 | if (for_rust_enum && !flags->print_offsets) |
0426ad51 | 846 | gdb_puts (is_tuple_struct ? "(" : "{", stream); |
c9317f21 | 847 | else |
0426ad51 | 848 | gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream); |
c44af4eb | 849 | |
a33ccfc7 TT |
850 | /* When printing offsets, we rearrange the fields into storage |
851 | order. This lets us show holes more clearly. We work using | |
852 | field indices here because it simplifies calls to | |
853 | print_offset_data::update below. */ | |
854 | std::vector<int> fields; | |
1f704f76 | 855 | for (int i = 0; i < type->num_fields (); ++i) |
b50f188d | 856 | { |
c819a338 | 857 | if (type->field (i).is_static ()) |
b50f188d | 858 | continue; |
454977cd | 859 | if (is_enum && type->field (i).is_artificial ()) |
a33ccfc7 TT |
860 | continue; |
861 | fields.push_back (i); | |
862 | } | |
863 | if (flags->print_offsets) | |
864 | std::sort (fields.begin (), fields.end (), | |
865 | [&] (int a, int b) | |
866 | { | |
b610c045 SM |
867 | return (type->field (a).loc_bitpos () |
868 | < type->field (b).loc_bitpos ()); | |
a33ccfc7 TT |
869 | }); |
870 | ||
871 | for (int i : fields) | |
872 | { | |
873 | QUIT; | |
874 | ||
c819a338 | 875 | gdb_assert (!type->field (i).is_static ()); |
454977cd | 876 | gdb_assert (! (is_enum && type->field (i).is_artificial ())); |
a33ccfc7 TT |
877 | |
878 | if (flags->print_offsets) | |
879 | podata->update (type, i, stream); | |
b50f188d TT |
880 | |
881 | /* We'd like to print "pub" here as needed, but rustc | |
882 | doesn't emit the debuginfo, and our types don't have | |
883 | cplus_struct_type attached. */ | |
884 | ||
885 | /* For a tuple struct we print the type but nothing | |
886 | else. */ | |
a33ccfc7 | 887 | if (!for_rust_enum || flags->print_offsets) |
d0b1020b | 888 | print_spaces (level + 2, stream); |
c9317f21 | 889 | if (is_enum) |
33d16dd9 | 890 | fputs_styled (type->field (i).name (), variable_name_style.style (), |
3f0cbb04 | 891 | stream); |
c9317f21 | 892 | else if (!is_tuple_struct) |
6cb06a8c TT |
893 | gdb_printf (stream, "%ps: ", |
894 | styled_string (variable_name_style.style (), | |
895 | type->field (i).name ())); | |
b50f188d | 896 | |
940da03e | 897 | rust_internal_print_type (type->field (i).type (), NULL, |
53d7df28 | 898 | stream, (is_enum ? show : show - 1), |
a33ccfc7 TT |
899 | level + 2, flags, is_enum, podata); |
900 | if (!for_rust_enum || flags->print_offsets) | |
0426ad51 | 901 | gdb_puts (",\n", stream); |
a33ccfc7 TT |
902 | /* Note that this check of "I" is ok because we only sorted the |
903 | fields by offset when print_offsets was set, so we won't take | |
904 | this branch in that case. */ | |
1f704f76 | 905 | else if (i + 1 < type->num_fields ()) |
0426ad51 | 906 | gdb_puts (", ", stream); |
b50f188d | 907 | } |
c44af4eb | 908 | |
a33ccfc7 TT |
909 | if (flags->print_offsets) |
910 | { | |
911 | /* Undo the temporary level increase we did above. */ | |
912 | level -= 2; | |
913 | podata->finish (type, level, stream); | |
d0b1020b | 914 | print_spaces (print_offset_data::indentation, stream); |
a33ccfc7 | 915 | if (level == 0) |
d0b1020b | 916 | print_spaces (2, stream); |
a33ccfc7 TT |
917 | } |
918 | if (!for_rust_enum || flags->print_offsets) | |
d0b1020b | 919 | print_spaces (level, stream); |
0426ad51 | 920 | gdb_puts (is_tuple_struct ? ")" : "}", stream); |
c44af4eb TT |
921 | } |
922 | ||
c44af4eb TT |
923 | /* la_print_type implementation for Rust. */ |
924 | ||
925 | static void | |
c9317f21 TT |
926 | rust_internal_print_type (struct type *type, const char *varstring, |
927 | struct ui_file *stream, int show, int level, | |
928 | const struct type_print_options *flags, | |
a33ccfc7 | 929 | bool for_rust_enum, print_offset_data *podata) |
c44af4eb | 930 | { |
c44af4eb TT |
931 | QUIT; |
932 | if (show <= 0 | |
7d93a1e0 | 933 | && type->name () != NULL) |
c44af4eb | 934 | { |
921d8f54 | 935 | /* Rust calls the unit type "void" in its debuginfo, |
dda83cd7 | 936 | but we don't want to print it as that. */ |
78134374 | 937 | if (type->code () == TYPE_CODE_VOID) |
0426ad51 | 938 | gdb_puts ("()", stream); |
921d8f54 | 939 | else |
0426ad51 | 940 | gdb_puts (type->name (), stream); |
c44af4eb TT |
941 | return; |
942 | } | |
943 | ||
944 | type = check_typedef (type); | |
78134374 | 945 | switch (type->code ()) |
c44af4eb | 946 | { |
921d8f54 | 947 | case TYPE_CODE_VOID: |
c9317f21 TT |
948 | /* If we have an enum, we've already printed the type's |
949 | unqualified name, and there is nothing else to print | |
950 | here. */ | |
951 | if (!for_rust_enum) | |
0426ad51 | 952 | gdb_puts ("()", stream); |
921d8f54 MG |
953 | break; |
954 | ||
c44af4eb TT |
955 | case TYPE_CODE_FUNC: |
956 | /* Delegate varargs to the C printer. */ | |
a409645d | 957 | if (type->has_varargs ()) |
c44af4eb TT |
958 | goto c_printer; |
959 | ||
0426ad51 | 960 | gdb_puts ("fn ", stream); |
c44af4eb | 961 | if (varstring != NULL) |
0426ad51 TT |
962 | gdb_puts (varstring, stream); |
963 | gdb_puts ("(", stream); | |
1f704f76 | 964 | for (int i = 0; i < type->num_fields (); ++i) |
c44af4eb TT |
965 | { |
966 | QUIT; | |
967 | if (i > 0) | |
0426ad51 | 968 | gdb_puts (", ", stream); |
940da03e | 969 | rust_internal_print_type (type->field (i).type (), "", stream, |
a33ccfc7 | 970 | -1, 0, flags, false, podata); |
c44af4eb | 971 | } |
0426ad51 | 972 | gdb_puts (")", stream); |
921d8f54 | 973 | /* If it returns unit, we can omit the return type. */ |
27710edb | 974 | if (type->target_type ()->code () != TYPE_CODE_VOID) |
dda83cd7 | 975 | { |
0426ad51 | 976 | gdb_puts (" -> ", stream); |
27710edb | 977 | rust_internal_print_type (type->target_type (), "", stream, |
a33ccfc7 | 978 | -1, 0, flags, false, podata); |
dda83cd7 | 979 | } |
c44af4eb TT |
980 | break; |
981 | ||
982 | case TYPE_CODE_ARRAY: | |
983 | { | |
984 | LONGEST low_bound, high_bound; | |
985 | ||
0426ad51 | 986 | gdb_puts ("[", stream); |
27710edb | 987 | rust_internal_print_type (type->target_type (), NULL, |
a33ccfc7 TT |
988 | stream, show - 1, level, flags, false, |
989 | podata); | |
c44af4eb | 990 | |
cf88be68 SM |
991 | if (type->bounds ()->high.kind () == PROP_LOCEXPR |
992 | || type->bounds ()->high.kind () == PROP_LOCLIST) | |
6cb06a8c | 993 | gdb_printf (stream, "; variable length"); |
c44af4eb | 994 | else if (get_array_bounds (type, &low_bound, &high_bound)) |
6cb06a8c TT |
995 | gdb_printf (stream, "; %s", |
996 | plongest (high_bound - low_bound + 1)); | |
0426ad51 | 997 | gdb_puts ("]", stream); |
c44af4eb TT |
998 | } |
999 | break; | |
1000 | ||
c9317f21 | 1001 | case TYPE_CODE_UNION: |
c44af4eb | 1002 | case TYPE_CODE_STRUCT: |
c9317f21 | 1003 | rust_print_struct_def (type, varstring, stream, show, level, flags, |
a33ccfc7 | 1004 | for_rust_enum, podata); |
c44af4eb TT |
1005 | break; |
1006 | ||
1007 | case TYPE_CODE_ENUM: | |
1008 | { | |
b926417a | 1009 | int len = 0; |
c44af4eb | 1010 | |
0426ad51 | 1011 | gdb_puts ("enum ", stream); |
7d93a1e0 | 1012 | if (type->name () != NULL) |
c44af4eb | 1013 | { |
0426ad51 TT |
1014 | gdb_puts (type->name (), stream); |
1015 | gdb_puts (" ", stream); | |
7d93a1e0 | 1016 | len = strlen (type->name ()); |
c44af4eb | 1017 | } |
0426ad51 | 1018 | gdb_puts ("{\n", stream); |
c44af4eb | 1019 | |
1f704f76 | 1020 | for (int i = 0; i < type->num_fields (); ++i) |
c44af4eb | 1021 | { |
33d16dd9 | 1022 | const char *name = type->field (i).name (); |
c44af4eb TT |
1023 | |
1024 | QUIT; | |
1025 | ||
1026 | if (len > 0 | |
7d93a1e0 | 1027 | && strncmp (name, type->name (), len) == 0 |
c44af4eb TT |
1028 | && name[len] == ':' |
1029 | && name[len + 1] == ':') | |
1030 | name += len + 2; | |
6cb06a8c TT |
1031 | gdb_printf (stream, "%*s%ps,\n", |
1032 | level + 2, "", | |
1033 | styled_string (variable_name_style.style (), | |
1034 | name)); | |
c44af4eb TT |
1035 | } |
1036 | ||
0426ad51 | 1037 | gdb_puts ("}", stream); |
c44af4eb | 1038 | } |
73fc52c4 TT |
1039 | break; |
1040 | ||
1041 | case TYPE_CODE_PTR: | |
1042 | { | |
7d93a1e0 | 1043 | if (type->name () != nullptr) |
0426ad51 | 1044 | gdb_puts (type->name (), stream); |
73fc52c4 TT |
1045 | else |
1046 | { | |
1047 | /* We currently can't distinguish between pointers and | |
1048 | references. */ | |
0426ad51 | 1049 | gdb_puts ("*mut ", stream); |
27710edb | 1050 | type_print (type->target_type (), "", stream, 0); |
73fc52c4 TT |
1051 | } |
1052 | } | |
c44af4eb TT |
1053 | break; |
1054 | ||
c44af4eb TT |
1055 | default: |
1056 | c_printer: | |
1c6fbf42 PA |
1057 | c_print_type (type, varstring, stream, show, level, language_rust, |
1058 | flags); | |
c44af4eb TT |
1059 | } |
1060 | } | |
1061 | ||
1062 | \f | |
1063 | ||
c44af4eb TT |
1064 | /* Like arch_composite_type, but uses TYPE to decide how to allocate |
1065 | -- either on an obstack or on a gdbarch. */ | |
1066 | ||
1067 | static struct type * | |
1068 | rust_composite_type (struct type *original, | |
1069 | const char *name, | |
1070 | const char *field1, struct type *type1, | |
1071 | const char *field2, struct type *type2) | |
1072 | { | |
9fa83a7a | 1073 | struct type *result = type_allocator (original).new_type (); |
c44af4eb TT |
1074 | int i, nfields, bitpos; |
1075 | ||
1076 | nfields = 0; | |
1077 | if (field1 != NULL) | |
1078 | ++nfields; | |
1079 | if (field2 != NULL) | |
1080 | ++nfields; | |
1081 | ||
67607e24 | 1082 | result->set_code (TYPE_CODE_STRUCT); |
d0e39ea2 | 1083 | result->set_name (name); |
c44af4eb | 1084 | |
2774f2da | 1085 | result->alloc_fields (nfields); |
c44af4eb TT |
1086 | |
1087 | i = 0; | |
1088 | bitpos = 0; | |
1089 | if (field1 != NULL) | |
1090 | { | |
ceacbf6e | 1091 | struct field *field = &result->field (i); |
c44af4eb | 1092 | |
cd3f655c | 1093 | field->set_loc_bitpos (bitpos); |
df86565b | 1094 | bitpos += type1->length () * TARGET_CHAR_BIT; |
c44af4eb | 1095 | |
d3fd12df | 1096 | field->set_name (field1); |
5d14b6e5 | 1097 | field->set_type (type1); |
c44af4eb TT |
1098 | ++i; |
1099 | } | |
1100 | if (field2 != NULL) | |
1101 | { | |
ceacbf6e | 1102 | struct field *field = &result->field (i); |
2fff16dd | 1103 | unsigned align = type_align (type2); |
c44af4eb TT |
1104 | |
1105 | if (align != 0) | |
1106 | { | |
1107 | int delta; | |
1108 | ||
1109 | align *= TARGET_CHAR_BIT; | |
1110 | delta = bitpos % align; | |
1111 | if (delta != 0) | |
1112 | bitpos += align - delta; | |
1113 | } | |
cd3f655c | 1114 | field->set_loc_bitpos (bitpos); |
c44af4eb | 1115 | |
d3fd12df | 1116 | field->set_name (field2); |
5d14b6e5 | 1117 | field->set_type (type2); |
c44af4eb TT |
1118 | ++i; |
1119 | } | |
1120 | ||
1121 | if (i > 0) | |
b6cdbc9a | 1122 | result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT |
df86565b | 1123 | + result->field (i - 1).type ()->length ()); |
c44af4eb TT |
1124 | return result; |
1125 | } | |
1126 | ||
1127 | /* See rust-lang.h. */ | |
1128 | ||
1129 | struct type * | |
1130 | rust_slice_type (const char *name, struct type *elt_type, | |
1131 | struct type *usize_type) | |
1132 | { | |
1133 | struct type *type; | |
1134 | ||
1135 | elt_type = lookup_pointer_type (elt_type); | |
1136 | type = rust_composite_type (elt_type, name, | |
1137 | "data_ptr", elt_type, | |
1138 | "length", usize_type); | |
1139 | ||
1140 | return type; | |
1141 | } | |
1142 | ||
c44af4eb TT |
1143 | \f |
1144 | ||
9fe87c3c TT |
1145 | namespace expr |
1146 | { | |
c44af4eb | 1147 | |
9db6b6dd | 1148 | struct value * |
9fe87c3c TT |
1149 | rust_range_operation::evaluate (struct type *expect_type, |
1150 | struct expression *exp, | |
1151 | enum noside noside) | |
c44af4eb | 1152 | { |
9fe87c3c TT |
1153 | auto kind = std::get<0> (m_storage); |
1154 | value *low = nullptr; | |
1155 | if (std::get<1> (m_storage) != nullptr) | |
1156 | low = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); | |
1157 | value *high = nullptr; | |
1158 | if (std::get<2> (m_storage) != nullptr) | |
1159 | high = std::get<2> (m_storage)->evaluate (nullptr, exp, noside); | |
1160 | ||
c44af4eb TT |
1161 | struct value *addrval, *result; |
1162 | CORE_ADDR addr; | |
1163 | struct type *range_type; | |
1164 | struct type *index_type; | |
1165 | struct type *temp_type; | |
1166 | const char *name; | |
1167 | ||
2f1b18db | 1168 | bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE); |
c44af4eb | 1169 | |
c44af4eb TT |
1170 | if (low == NULL) |
1171 | { | |
1172 | if (high == NULL) | |
1173 | { | |
1174 | index_type = NULL; | |
1175 | name = "std::ops::RangeFull"; | |
1176 | } | |
1177 | else | |
1178 | { | |
d0c97917 | 1179 | index_type = high->type (); |
6873858b TT |
1180 | name = (inclusive |
1181 | ? "std::ops::RangeToInclusive" : "std::ops::RangeTo"); | |
c44af4eb TT |
1182 | } |
1183 | } | |
1184 | else | |
1185 | { | |
1186 | if (high == NULL) | |
1187 | { | |
d0c97917 | 1188 | index_type = low->type (); |
c44af4eb TT |
1189 | name = "std::ops::RangeFrom"; |
1190 | } | |
1191 | else | |
1192 | { | |
d0c97917 | 1193 | if (!types_equal (low->type (), high->type ())) |
c44af4eb | 1194 | error (_("Range expression with different types")); |
d0c97917 | 1195 | index_type = low->type (); |
6873858b | 1196 | name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range"; |
c44af4eb TT |
1197 | } |
1198 | } | |
1199 | ||
1200 | /* If we don't have an index type, just allocate this on the | |
1201 | arch. Here any type will do. */ | |
1202 | temp_type = (index_type == NULL | |
1203 | ? language_bool_type (exp->language_defn, exp->gdbarch) | |
1204 | : index_type); | |
1205 | /* It would be nicer to cache the range type. */ | |
1206 | range_type = rust_composite_type (temp_type, name, | |
1207 | low == NULL ? NULL : "start", index_type, | |
1208 | high == NULL ? NULL : "end", index_type); | |
1209 | ||
1210 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
ee7bb294 | 1211 | return value::zero (range_type, lval_memory); |
c44af4eb | 1212 | |
df86565b | 1213 | addrval = value_allocate_space_in_inferior (range_type->length ()); |
c44af4eb TT |
1214 | addr = value_as_long (addrval); |
1215 | result = value_at_lazy (range_type, addr); | |
1216 | ||
1217 | if (low != NULL) | |
1218 | { | |
158cc4fe | 1219 | struct value *start = value_struct_elt (&result, {}, "start", NULL, |
c44af4eb TT |
1220 | "range"); |
1221 | ||
1222 | value_assign (start, low); | |
1223 | } | |
1224 | ||
1225 | if (high != NULL) | |
1226 | { | |
158cc4fe | 1227 | struct value *end = value_struct_elt (&result, {}, "end", NULL, |
c44af4eb TT |
1228 | "range"); |
1229 | ||
1230 | value_assign (end, high); | |
1231 | } | |
1232 | ||
1233 | result = value_at_lazy (range_type, addr); | |
1234 | return result; | |
1235 | } | |
1236 | ||
9fe87c3c TT |
1237 | } /* namespace expr */ |
1238 | ||
c44af4eb TT |
1239 | /* A helper function to compute the range and kind given a range |
1240 | value. TYPE is the type of the range value. RANGE is the range | |
1241 | value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH | |
1242 | parameters might be filled in, or might not be, depending on the | |
1243 | kind of range this is. KIND will always be set to the appropriate | |
1244 | value describing the kind of range, and this can be used to | |
1245 | determine whether LOW or HIGH are valid. */ | |
1246 | ||
1247 | static void | |
1248 | rust_compute_range (struct type *type, struct value *range, | |
1249 | LONGEST *low, LONGEST *high, | |
f2d8e4c5 | 1250 | range_flags *kind) |
c44af4eb TT |
1251 | { |
1252 | int i; | |
1253 | ||
1254 | *low = 0; | |
1255 | *high = 0; | |
2f1b18db | 1256 | *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT; |
c44af4eb | 1257 | |
1f704f76 | 1258 | if (type->num_fields () == 0) |
c44af4eb TT |
1259 | return; |
1260 | ||
1261 | i = 0; | |
33d16dd9 | 1262 | if (strcmp (type->field (0).name (), "start") == 0) |
c44af4eb | 1263 | { |
2f1b18db | 1264 | *kind = RANGE_HIGH_BOUND_DEFAULT; |
c44af4eb TT |
1265 | *low = value_as_long (value_field (range, 0)); |
1266 | ++i; | |
1267 | } | |
1f704f76 | 1268 | if (type->num_fields () > i |
33d16dd9 | 1269 | && strcmp (type->field (i).name (), "end") == 0) |
c44af4eb | 1270 | { |
2f1b18db AB |
1271 | *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT) |
1272 | ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD); | |
c44af4eb | 1273 | *high = value_as_long (value_field (range, i)); |
6873858b TT |
1274 | |
1275 | if (rust_inclusive_range_type_p (type)) | |
1276 | ++*high; | |
c44af4eb TT |
1277 | } |
1278 | } | |
1279 | ||
9fe87c3c TT |
1280 | namespace expr |
1281 | { | |
c44af4eb | 1282 | |
6ce1ad67 | 1283 | struct value * |
9fe87c3c TT |
1284 | rust_subscript_operation::subscript (struct expression *exp, |
1285 | enum noside noside, bool for_addr) | |
c44af4eb | 1286 | { |
9fe87c3c TT |
1287 | value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); |
1288 | value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); | |
1289 | ||
984af2cb | 1290 | struct value *result; |
c44af4eb | 1291 | struct type *rhstype; |
45f4ed92 | 1292 | LONGEST low, high_bound; |
c44af4eb | 1293 | /* Initialized to appease the compiler. */ |
f2d8e4c5 | 1294 | range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT; |
45f4ed92 | 1295 | LONGEST high = 0; |
db7e13a9 | 1296 | bool want_slice = false; |
c44af4eb | 1297 | |
d0c97917 | 1298 | rhstype = check_typedef (rhs->type ()); |
c44af4eb TT |
1299 | if (rust_range_type_p (rhstype)) |
1300 | { | |
1301 | if (!for_addr) | |
1302 | error (_("Can't take slice of array without '&'")); | |
1303 | rust_compute_range (rhstype, rhs, &low, &high, &kind); | |
db7e13a9 | 1304 | want_slice = true; |
c44af4eb TT |
1305 | } |
1306 | else | |
1307 | low = value_as_long (rhs); | |
1308 | ||
d0c97917 | 1309 | struct type *type = check_typedef (lhs->type ()); |
b0dd661f | 1310 | struct type *orig_type = type; |
c44af4eb TT |
1311 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1312 | { | |
b3e3859b | 1313 | struct type *base_type = nullptr; |
78134374 | 1314 | if (type->code () == TYPE_CODE_ARRAY) |
27710edb | 1315 | base_type = type->target_type (); |
b3e3859b TT |
1316 | else if (rust_slice_type_p (type)) |
1317 | { | |
b0dd661f | 1318 | base_type = rust_array_like_element_type (type); |
b3e3859b | 1319 | if (base_type == nullptr) |
b0dd661f | 1320 | error (_("Cannot subscript non-array-like slice")); |
b3e3859b | 1321 | } |
78134374 | 1322 | else if (type->code () == TYPE_CODE_PTR) |
27710edb | 1323 | base_type = type->target_type (); |
b3e3859b TT |
1324 | else |
1325 | error (_("Cannot subscript non-array type")); | |
1326 | ||
1327 | struct type *new_type; | |
1328 | if (want_slice) | |
1329 | { | |
1330 | if (rust_slice_type_p (type)) | |
1331 | new_type = type; | |
1332 | else | |
1333 | { | |
1334 | struct type *usize | |
1335 | = language_lookup_primitive_type (exp->language_defn, | |
1336 | exp->gdbarch, | |
1337 | "usize"); | |
1338 | new_type = rust_slice_type ("&[*gdb*]", base_type, usize); | |
1339 | } | |
1340 | } | |
1341 | else | |
1342 | new_type = base_type; | |
c44af4eb | 1343 | |
736355f2 | 1344 | return value::zero (new_type, lhs->lval ()); |
c44af4eb TT |
1345 | } |
1346 | else | |
1347 | { | |
1348 | LONGEST low_bound; | |
1349 | struct value *base; | |
c44af4eb | 1350 | |
b0dd661f TT |
1351 | if (rust_slice_type_p (type)) |
1352 | { | |
1353 | lhs = convert_slice (lhs); | |
1354 | type = check_typedef (lhs->type ()); | |
1355 | } | |
1356 | ||
78134374 | 1357 | if (type->code () == TYPE_CODE_ARRAY) |
c44af4eb TT |
1358 | { |
1359 | base = lhs; | |
1360 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
1361 | error (_("Can't compute array bounds")); | |
1362 | if (low_bound != 0) | |
1363 | error (_("Found array with non-zero lower bound")); | |
1364 | ++high_bound; | |
1365 | } | |
78134374 | 1366 | else if (type->code () == TYPE_CODE_PTR) |
42d94011 MG |
1367 | { |
1368 | base = lhs; | |
1369 | low_bound = 0; | |
1370 | high_bound = LONGEST_MAX; | |
1371 | } | |
c44af4eb TT |
1372 | else |
1373 | error (_("Cannot subscript non-array type")); | |
1374 | ||
2f1b18db | 1375 | if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT)) |
c44af4eb TT |
1376 | low = low_bound; |
1377 | if (low < 0) | |
1378 | error (_("Index less than zero")); | |
1379 | if (low > high_bound) | |
1380 | error (_("Index greater than length")); | |
1381 | ||
1382 | result = value_subscript (base, low); | |
1383 | } | |
1384 | ||
1385 | if (for_addr) | |
1386 | { | |
1387 | if (want_slice) | |
1388 | { | |
1389 | struct type *usize, *slice; | |
1390 | CORE_ADDR addr; | |
1391 | struct value *addrval, *tem; | |
1392 | ||
2f1b18db | 1393 | if (kind & RANGE_HIGH_BOUND_DEFAULT) |
c44af4eb TT |
1394 | high = high_bound; |
1395 | if (high < 0) | |
1396 | error (_("High index less than zero")); | |
1397 | if (low > high) | |
1398 | error (_("Low index greater than high index")); | |
1399 | if (high > high_bound) | |
1400 | error (_("High index greater than length")); | |
1401 | ||
1402 | usize = language_lookup_primitive_type (exp->language_defn, | |
1403 | exp->gdbarch, | |
1404 | "usize"); | |
b0dd661f TT |
1405 | /* Preserve the name for slice-of-slice; this lets |
1406 | string-printing work a bit more nicely. */ | |
1407 | const char *new_name = ((orig_type != nullptr | |
1408 | && rust_slice_type_p (orig_type)) | |
1409 | ? orig_type->name () : "&[*gdb*]"); | |
45320ffa | 1410 | |
d0c97917 | 1411 | slice = rust_slice_type (new_name, result->type (), usize); |
c44af4eb | 1412 | |
df86565b | 1413 | addrval = value_allocate_space_in_inferior (slice->length ()); |
c44af4eb TT |
1414 | addr = value_as_long (addrval); |
1415 | tem = value_at_lazy (slice, addr); | |
1416 | ||
1417 | value_assign (value_field (tem, 0), value_addr (result)); | |
1418 | value_assign (value_field (tem, 1), | |
1419 | value_from_longest (usize, high - low)); | |
1420 | ||
1421 | result = value_at_lazy (slice, addr); | |
1422 | } | |
1423 | else | |
1424 | result = value_addr (result); | |
1425 | } | |
1426 | ||
1427 | return result; | |
1428 | } | |
1429 | ||
11dd3dce | 1430 | struct value * |
f10522c0 TT |
1431 | rust_unop_ind_operation::evaluate (struct type *expect_type, |
1432 | struct expression *exp, | |
1433 | enum noside noside) | |
d123f9e4 | 1434 | { |
f10522c0 TT |
1435 | if (noside != EVAL_NORMAL) |
1436 | return unop_ind_operation::evaluate (expect_type, exp, noside); | |
1437 | ||
1438 | struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
1439 | noside); | |
d123f9e4 TT |
1440 | struct value *trait_ptr = rust_get_trait_object_pointer (value); |
1441 | if (trait_ptr != NULL) | |
1442 | value = trait_ptr; | |
1443 | ||
1444 | return value_ind (value); | |
1445 | } | |
1446 | ||
f10522c0 TT |
1447 | } /* namespace expr */ |
1448 | ||
6fa9831f TT |
1449 | /* A helper function for UNOP_COMPLEMENT. */ |
1450 | ||
6fab4359 | 1451 | struct value * |
6fa9831f TT |
1452 | eval_op_rust_complement (struct type *expect_type, struct expression *exp, |
1453 | enum noside noside, | |
6fab4359 | 1454 | enum exp_opcode opcode, |
6fa9831f TT |
1455 | struct value *value) |
1456 | { | |
d0c97917 TT |
1457 | if (value->type ()->code () == TYPE_CODE_BOOL) |
1458 | return value_from_longest (value->type (), value_logical_not (value)); | |
6fa9831f TT |
1459 | return value_complement (value); |
1460 | } | |
1461 | ||
05104233 TT |
1462 | /* A helper function for OP_ARRAY. */ |
1463 | ||
6fab4359 | 1464 | struct value * |
05104233 TT |
1465 | eval_op_rust_array (struct type *expect_type, struct expression *exp, |
1466 | enum noside noside, | |
6fab4359 | 1467 | enum exp_opcode opcode, |
05104233 TT |
1468 | struct value *elt, struct value *ncopies) |
1469 | { | |
1470 | int copies = value_as_long (ncopies); | |
1471 | if (copies < 0) | |
1472 | error (_("Array with negative number of elements")); | |
1473 | ||
a4f974e9 | 1474 | if (noside == EVAL_NORMAL && copies > 0) |
7c651c5f | 1475 | return value_array (0, std::vector<value *> (copies, elt)); |
05104233 TT |
1476 | else |
1477 | { | |
1478 | struct type *arraytype | |
d0c97917 | 1479 | = lookup_array_range_type (elt->type (), 0, copies - 1); |
317c3ed9 | 1480 | return value::allocate (arraytype); |
05104233 TT |
1481 | } |
1482 | } | |
1483 | ||
f10522c0 TT |
1484 | namespace expr |
1485 | { | |
575cae23 | 1486 | |
e4407a20 | 1487 | struct value * |
f10522c0 TT |
1488 | rust_struct_anon::evaluate (struct type *expect_type, |
1489 | struct expression *exp, | |
1490 | enum noside noside) | |
575cae23 | 1491 | { |
f10522c0 TT |
1492 | value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); |
1493 | int field_number = std::get<0> (m_storage); | |
1494 | ||
254809b2 | 1495 | struct type *type = check_typedef (lhs->type ()); |
575cae23 TT |
1496 | |
1497 | if (type->code () == TYPE_CODE_STRUCT) | |
1498 | { | |
1499 | struct type *outer_type = NULL; | |
1500 | ||
1501 | if (rust_enum_p (type)) | |
1502 | { | |
efaf1ae0 | 1503 | type = resolve_dynamic_type (type, lhs->contents (), |
9feb2d07 | 1504 | lhs->address ()); |
575cae23 TT |
1505 | |
1506 | if (rust_empty_enum_p (type)) | |
1507 | error (_("Cannot access field %d of empty enum %s"), | |
1508 | field_number, type->name ()); | |
1509 | ||
1510 | int fieldno = rust_enum_variant (type); | |
6c49729e | 1511 | lhs = lhs->primitive_field (0, fieldno, type); |
575cae23 | 1512 | outer_type = type; |
d0c97917 | 1513 | type = lhs->type (); |
575cae23 TT |
1514 | } |
1515 | ||
1516 | /* Tuples and tuple structs */ | |
1517 | int nfields = type->num_fields (); | |
1518 | ||
1519 | if (field_number >= nfields || field_number < 0) | |
1520 | { | |
1521 | if (outer_type != NULL) | |
1522 | error(_("Cannot access field %d of variant %s::%s, " | |
1523 | "there are only %d fields"), | |
1524 | field_number, outer_type->name (), | |
1525 | rust_last_path_segment (type->name ()), | |
1526 | nfields); | |
1527 | else | |
1528 | error(_("Cannot access field %d of %s, " | |
1529 | "there are only %d fields"), | |
1530 | field_number, type->name (), nfields); | |
1531 | } | |
1532 | ||
1533 | /* Tuples are tuple structs too. */ | |
1534 | if (!rust_tuple_struct_type_p (type)) | |
1535 | { | |
1536 | if (outer_type != NULL) | |
1537 | error(_("Variant %s::%s is not a tuple variant"), | |
1538 | outer_type->name (), | |
1539 | rust_last_path_segment (type->name ())); | |
1540 | else | |
1541 | error(_("Attempting to access anonymous field %d " | |
1542 | "of %s, which is not a tuple, tuple struct, or " | |
1543 | "tuple-like variant"), | |
1544 | field_number, type->name ()); | |
1545 | } | |
1546 | ||
6c49729e | 1547 | return lhs->primitive_field (0, field_number, type); |
575cae23 TT |
1548 | } |
1549 | else | |
1550 | error(_("Anonymous field access is only allowed on tuples, \ | |
1551 | tuple structs, and tuple-like enum variants")); | |
1552 | } | |
1553 | ||
e4407a20 | 1554 | struct value * |
f10522c0 TT |
1555 | rust_structop::evaluate (struct type *expect_type, |
1556 | struct expression *exp, | |
1557 | enum noside noside) | |
1fa41fc7 | 1558 | { |
f10522c0 TT |
1559 | value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); |
1560 | const char *field_name = std::get<1> (m_storage).c_str (); | |
1561 | ||
1fa41fc7 | 1562 | struct value *result; |
d0c97917 | 1563 | struct type *type = lhs->type (); |
1fa41fc7 TT |
1564 | if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type)) |
1565 | { | |
efaf1ae0 | 1566 | type = resolve_dynamic_type (type, lhs->contents (), |
9feb2d07 | 1567 | lhs->address ()); |
1fa41fc7 TT |
1568 | |
1569 | if (rust_empty_enum_p (type)) | |
1570 | error (_("Cannot access field %s of empty enum %s"), | |
1571 | field_name, type->name ()); | |
1572 | ||
1573 | int fieldno = rust_enum_variant (type); | |
6c49729e | 1574 | lhs = lhs->primitive_field (0, fieldno, type); |
1fa41fc7 TT |
1575 | |
1576 | struct type *outer_type = type; | |
d0c97917 | 1577 | type = lhs->type (); |
1fa41fc7 TT |
1578 | if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type)) |
1579 | error (_("Attempting to access named field %s of tuple " | |
1580 | "variant %s::%s, which has only anonymous fields"), | |
1581 | field_name, outer_type->name (), | |
1582 | rust_last_path_segment (type->name ())); | |
1583 | ||
1584 | try | |
1585 | { | |
158cc4fe | 1586 | result = value_struct_elt (&lhs, {}, field_name, |
1fa41fc7 TT |
1587 | NULL, "structure"); |
1588 | } | |
1589 | catch (const gdb_exception_error &except) | |
1590 | { | |
1591 | error (_("Could not find field %s of struct variant %s::%s"), | |
1592 | field_name, outer_type->name (), | |
1593 | rust_last_path_segment (type->name ())); | |
1594 | } | |
1595 | } | |
1596 | else | |
b0dd661f TT |
1597 | { |
1598 | if (rust_slice_type_p (type)) | |
1599 | lhs = convert_slice (lhs); | |
1600 | result = value_struct_elt (&lhs, {}, field_name, NULL, "structure"); | |
1601 | } | |
1fa41fc7 | 1602 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
736355f2 | 1603 | result = value::zero (result->type (), result->lval ()); |
1fa41fc7 TT |
1604 | return result; |
1605 | } | |
1606 | ||
5947d337 TT |
1607 | value * |
1608 | rust_aggregate_operation::evaluate (struct type *expect_type, | |
1609 | struct expression *exp, | |
1610 | enum noside noside) | |
1611 | { | |
1612 | struct type *type = std::get<0> (m_storage); | |
1613 | CORE_ADDR addr = 0; | |
1614 | struct value *addrval = NULL; | |
1615 | value *result; | |
1616 | ||
1617 | if (noside == EVAL_NORMAL) | |
1618 | { | |
df86565b | 1619 | addrval = value_allocate_space_in_inferior (type->length ()); |
5947d337 TT |
1620 | addr = value_as_long (addrval); |
1621 | result = value_at_lazy (type, addr); | |
1622 | } | |
1623 | ||
1624 | if (std::get<1> (m_storage) != nullptr) | |
1625 | { | |
1626 | struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp, | |
1627 | noside); | |
1628 | ||
1629 | if (noside == EVAL_NORMAL) | |
1630 | { | |
1631 | /* This isn't quite right but will do for the time | |
1632 | being, seeing that we can't implement the Copy | |
1633 | trait anyway. */ | |
1634 | value_assign (result, init); | |
1635 | } | |
1636 | } | |
1637 | ||
1638 | for (const auto &item : std::get<2> (m_storage)) | |
1639 | { | |
1640 | value *val = item.second->evaluate (nullptr, exp, noside); | |
1641 | if (noside == EVAL_NORMAL) | |
1642 | { | |
1643 | const char *fieldname = item.first.c_str (); | |
158cc4fe | 1644 | value *field = value_struct_elt (&result, {}, fieldname, |
5947d337 TT |
1645 | nullptr, "structure"); |
1646 | value_assign (field, val); | |
1647 | } | |
1648 | } | |
1649 | ||
aa1da9ed | 1650 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
317c3ed9 | 1651 | result = value::allocate (type); |
5947d337 TT |
1652 | else |
1653 | result = value_at_lazy (type, addr); | |
1654 | ||
1655 | return result; | |
1656 | } | |
1657 | ||
638fd74a TT |
1658 | value * |
1659 | rust_structop::evaluate_funcall (struct type *expect_type, | |
1660 | struct expression *exp, | |
1661 | enum noside noside, | |
1662 | const std::vector<operation_up> &ops) | |
1663 | { | |
1664 | std::vector<struct value *> args (ops.size () + 1); | |
1665 | ||
1666 | /* Evaluate the argument to STRUCTOP_STRUCT, then find its | |
1667 | type in order to look up the method. */ | |
1668 | args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); | |
1669 | /* We don't yet implement real Deref semantics. */ | |
d0c97917 | 1670 | while (args[0]->type ()->code () == TYPE_CODE_PTR) |
638fd74a TT |
1671 | args[0] = value_ind (args[0]); |
1672 | ||
d0c97917 | 1673 | struct type *type = args[0]->type (); |
638fd74a TT |
1674 | if ((type->code () != TYPE_CODE_STRUCT |
1675 | && type->code () != TYPE_CODE_UNION | |
1676 | && type->code () != TYPE_CODE_ENUM) | |
1677 | || rust_tuple_type_p (type)) | |
1678 | error (_("Method calls only supported on struct or enum types")); | |
1679 | if (type->name () == NULL) | |
1680 | error (_("Method call on nameless type")); | |
1681 | ||
1682 | std::string name = (std::string (type->name ()) + "::" | |
1683 | + std::get<1> (m_storage)); | |
1684 | ||
1685 | const struct block *block = get_selected_block (0); | |
1686 | struct block_symbol sym = lookup_symbol (name.c_str (), block, | |
6cd92f3b | 1687 | SEARCH_FUNCTION_DOMAIN, |
ccf41c24 | 1688 | nullptr); |
638fd74a TT |
1689 | if (sym.symbol == NULL) |
1690 | error (_("Could not find function named '%s'"), name.c_str ()); | |
1691 | ||
5f9c5a63 | 1692 | struct type *fn_type = sym.symbol->type (); |
638fd74a TT |
1693 | if (fn_type->num_fields () == 0) |
1694 | error (_("Function '%s' takes no arguments"), name.c_str ()); | |
1695 | ||
1696 | if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR) | |
1697 | args[0] = value_addr (args[0]); | |
1698 | ||
1699 | value *function = address_of_variable (sym.symbol, block); | |
1700 | ||
1701 | for (int i = 0; i < ops.size (); ++i) | |
1702 | args[i + 1] = ops[i]->evaluate (nullptr, exp, noside); | |
1703 | ||
1704 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
ee7bb294 | 1705 | return value::zero (fn_type->target_type (), not_lval); |
638fd74a TT |
1706 | return call_function_by_hand (function, NULL, args); |
1707 | } | |
1708 | ||
5947d337 TT |
1709 | } |
1710 | ||
c44af4eb TT |
1711 | \f |
1712 | ||
1c485265 | 1713 | /* See language.h. */ |
0874fd07 | 1714 | |
1c485265 AB |
1715 | void |
1716 | rust_language::language_arch_info (struct gdbarch *gdbarch, | |
1717 | struct language_arch_info *lai) const | |
0874fd07 | 1718 | { |
1c485265 | 1719 | const struct builtin_type *builtin = builtin_type (gdbarch); |
87afa652 | 1720 | |
1c485265 AB |
1721 | /* Helper function to allow shorter lines below. */ |
1722 | auto add = [&] (struct type * t) -> struct type * | |
87afa652 | 1723 | { |
1c485265 AB |
1724 | lai->add_primitive_type (t); |
1725 | return t; | |
1726 | }; | |
1727 | ||
2d39ccd3 | 1728 | type_allocator alloc (gdbarch); |
1c485265 | 1729 | struct type *bool_type |
46c04ea3 | 1730 | = add (init_boolean_type (alloc, 8, 1, "bool")); |
f50b437c | 1731 | add (init_character_type (alloc, 32, 1, "char")); |
2d39ccd3 | 1732 | add (init_integer_type (alloc, 8, 0, "i8")); |
1c485265 | 1733 | struct type *u8_type |
2d39ccd3 TT |
1734 | = add (init_integer_type (alloc, 8, 1, "u8")); |
1735 | add (init_integer_type (alloc, 16, 0, "i16")); | |
1736 | add (init_integer_type (alloc, 16, 1, "u16")); | |
1737 | add (init_integer_type (alloc, 32, 0, "i32")); | |
1738 | add (init_integer_type (alloc, 32, 1, "u32")); | |
1739 | add (init_integer_type (alloc, 64, 0, "i64")); | |
1740 | add (init_integer_type (alloc, 64, 1, "u64")); | |
d760ae22 TT |
1741 | add (init_integer_type (alloc, 128, 0, "i128")); |
1742 | add (init_integer_type (alloc, 128, 1, "u128")); | |
1c485265 | 1743 | |
df86565b | 1744 | unsigned int length = 8 * builtin->builtin_data_ptr->length (); |
2d39ccd3 | 1745 | add (init_integer_type (alloc, length, 0, "isize")); |
1c485265 | 1746 | struct type *usize_type |
2d39ccd3 | 1747 | = add (init_integer_type (alloc, length, 1, "usize")); |
1c485265 | 1748 | |
77c5f496 TT |
1749 | add (init_float_type (alloc, 32, "f32", floatformats_ieee_single)); |
1750 | add (init_float_type (alloc, 64, "f64", floatformats_ieee_double)); | |
2d39ccd3 | 1751 | add (init_integer_type (alloc, 0, 1, "()")); |
1c485265 AB |
1752 | |
1753 | struct type *tem = make_cv_type (1, 0, u8_type, NULL); | |
1754 | add (rust_slice_type ("&str", tem, usize_type)); | |
1755 | ||
1756 | lai->set_bool_type (bool_type); | |
1757 | lai->set_string_char_type (u8_type); | |
1758 | } | |
39e7ecca | 1759 | |
1c485265 | 1760 | /* See language.h. */ |
efdf6a73 | 1761 | |
1c485265 AB |
1762 | void |
1763 | rust_language::print_type (struct type *type, const char *varstring, | |
1764 | struct ui_file *stream, int show, int level, | |
1765 | const struct type_print_options *flags) const | |
1766 | { | |
fbb46296 | 1767 | print_offset_data podata (flags); |
1c485265 AB |
1768 | rust_internal_print_type (type, varstring, stream, show, level, |
1769 | flags, false, &podata); | |
1770 | } | |
efdf6a73 | 1771 | |
1c485265 | 1772 | /* See language.h. */ |
5aba6ebe | 1773 | |
1c485265 AB |
1774 | void |
1775 | rust_language::emitchar (int ch, struct type *chtype, | |
1776 | struct ui_file *stream, int quoter) const | |
1777 | { | |
1778 | if (!rust_chartype_p (chtype)) | |
1779 | generic_emit_char (ch, chtype, stream, quoter, | |
8ee511af | 1780 | target_charset (chtype->arch ())); |
1c485265 | 1781 | else if (ch == '\\' || ch == quoter) |
6cb06a8c | 1782 | gdb_printf (stream, "\\%c", ch); |
1c485265 | 1783 | else if (ch == '\n') |
0426ad51 | 1784 | gdb_puts ("\\n", stream); |
1c485265 | 1785 | else if (ch == '\r') |
0426ad51 | 1786 | gdb_puts ("\\r", stream); |
1c485265 | 1787 | else if (ch == '\t') |
0426ad51 | 1788 | gdb_puts ("\\t", stream); |
1c485265 | 1789 | else if (ch == '\0') |
0426ad51 | 1790 | gdb_puts ("\\0", stream); |
1c485265 | 1791 | else if (ch >= 32 && ch <= 127 && isprint (ch)) |
a11ac3b3 | 1792 | gdb_putc (ch, stream); |
1c485265 | 1793 | else if (ch <= 255) |
6cb06a8c | 1794 | gdb_printf (stream, "\\x%02x", ch); |
1c485265 | 1795 | else |
6cb06a8c | 1796 | gdb_printf (stream, "\\u{%06x}", ch); |
1c485265 | 1797 | } |
5aba6ebe | 1798 | |
1c485265 | 1799 | /* See language.h. */ |
b7c6e27d | 1800 | |
b0dd661f TT |
1801 | bool |
1802 | rust_language::is_array_like (struct type *type) const | |
1803 | { | |
1804 | if (!rust_slice_type_p (type)) | |
1805 | return false; | |
1806 | return rust_array_like_element_type (type) != nullptr; | |
1807 | } | |
1808 | ||
1809 | /* See language.h. */ | |
1810 | ||
1c485265 AB |
1811 | bool |
1812 | rust_language::is_string_type_p (struct type *type) const | |
1813 | { | |
1814 | LONGEST low_bound, high_bound; | |
b7c6e27d | 1815 | |
1c485265 AB |
1816 | type = check_typedef (type); |
1817 | return ((type->code () == TYPE_CODE_STRING) | |
1818 | || (type->code () == TYPE_CODE_PTR | |
27710edb SM |
1819 | && (type->target_type ()->code () == TYPE_CODE_ARRAY |
1820 | && rust_u8_type_p (type->target_type ()->target_type ()) | |
1821 | && get_array_bounds (type->target_type (), &low_bound, | |
1c485265 AB |
1822 | &high_bound))) |
1823 | || (type->code () == TYPE_CODE_STRUCT | |
1824 | && !rust_enum_p (type) | |
1825 | && rust_slice_type_p (type) | |
1826 | && strcmp (type->name (), "&str") == 0)); | |
1827 | } | |
0874fd07 | 1828 | |
e1a482ad TT |
1829 | /* See language.h. */ |
1830 | ||
1831 | struct block_symbol | |
1832 | rust_language::lookup_symbol_nonlocal | |
1833 | (const char *name, const struct block *block, | |
ccf41c24 | 1834 | const domain_search_flags domain) const |
e1a482ad TT |
1835 | { |
1836 | struct block_symbol result = {}; | |
1837 | ||
1838 | const char *scope = block == nullptr ? "" : block->scope (); | |
1839 | symbol_lookup_debug_printf | |
1840 | ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)", | |
1841 | name, host_address_to_string (block), scope, | |
ccf41c24 | 1842 | domain_name (domain).c_str ()); |
e1a482ad TT |
1843 | |
1844 | /* Look up bare names in the block's scope. */ | |
1845 | std::string scopedname; | |
1846 | if (name[cp_find_first_component (name)] == '\0') | |
1847 | { | |
1848 | if (scope[0] != '\0') | |
1849 | { | |
1850 | scopedname = std::string (scope) + "::" + name; | |
1851 | name = scopedname.c_str (); | |
1852 | } | |
1853 | else | |
1854 | name = NULL; | |
1855 | } | |
1856 | ||
1857 | if (name != NULL) | |
1858 | { | |
1859 | result = lookup_symbol_in_static_block (name, block, domain); | |
1860 | if (result.symbol == NULL) | |
1861 | result = lookup_global_symbol (name, block, domain); | |
1862 | } | |
1863 | return result; | |
1864 | } | |
1865 | ||
0874fd07 AB |
1866 | /* Single instance of the Rust language class. */ |
1867 | ||
1868 | static rust_language rust_language_defn; |