]>
Commit | Line | Data |
---|---|---|
c44af4eb TT |
1 | /* Rust language support routines for GDB, the GNU debugger. |
2 | ||
213516ef | 3 | Copyright (C) 2016-2023 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" |
6fab4359 | 42 | #include "rust-exp.h" |
c44af4eb | 43 | |
c9317f21 | 44 | /* See rust-lang.h. */ |
c44af4eb | 45 | |
c9317f21 TT |
46 | const char * |
47 | rust_last_path_segment (const char *path) | |
c44af4eb TT |
48 | { |
49 | const char *result = strrchr (path, ':'); | |
50 | ||
51 | if (result == NULL) | |
c9317f21 | 52 | return path; |
c44af4eb TT |
53 | return result + 1; |
54 | } | |
55 | ||
03c85b11 | 56 | /* See rust-lang.h. */ |
c44af4eb | 57 | |
03c85b11 | 58 | std::string |
c44af4eb TT |
59 | rust_crate_for_block (const struct block *block) |
60 | { | |
61 | const char *scope = block_scope (block); | |
62 | ||
63 | if (scope[0] == '\0') | |
03c85b11 | 64 | return std::string (); |
c44af4eb | 65 | |
03c85b11 | 66 | return std::string (scope, cp_find_first_component (scope)); |
c44af4eb TT |
67 | } |
68 | ||
c9317f21 TT |
69 | /* Return true if TYPE, which must be a struct type, represents a Rust |
70 | enum. */ | |
c44af4eb | 71 | |
b96645f1 | 72 | static bool |
9c6a1327 | 73 | rust_enum_p (struct type *type) |
b96645f1 | 74 | { |
9c6a1327 TT |
75 | /* is_dynamic_type will return true if any field has a dynamic |
76 | attribute -- but we only want to check the top level. */ | |
77 | return TYPE_HAS_VARIANT_PARTS (type); | |
b96645f1 MG |
78 | } |
79 | ||
9c6a1327 TT |
80 | /* Return true if TYPE, which must be an already-resolved enum type, |
81 | has no variants. */ | |
098b2108 TT |
82 | |
83 | static bool | |
84 | rust_empty_enum_p (const struct type *type) | |
85 | { | |
1f704f76 | 86 | return type->num_fields () == 0; |
098b2108 TT |
87 | } |
88 | ||
9c6a1327 TT |
89 | /* Given an already-resolved enum type and contents, find which |
90 | variant is active. */ | |
c44af4eb | 91 | |
9c6a1327 TT |
92 | static int |
93 | rust_enum_variant (struct type *type) | |
c44af4eb | 94 | { |
9c6a1327 | 95 | /* The active variant is simply the first non-artificial field. */ |
1f704f76 | 96 | for (int i = 0; i < type->num_fields (); ++i) |
9c6a1327 TT |
97 | if (!TYPE_FIELD_ARTIFICIAL (type, i)) |
98 | return i; | |
c44af4eb | 99 | |
9c6a1327 TT |
100 | /* Perhaps we could get here by trying to print an Ada variant |
101 | record in Rust mode. Unlikely, but an error is safer than an | |
102 | assert. */ | |
103 | error (_("Could not find active enum variant")); | |
c44af4eb TT |
104 | } |
105 | ||
106 | /* See rust-lang.h. */ | |
107 | ||
65c40c95 | 108 | bool |
c44af4eb TT |
109 | rust_tuple_type_p (struct type *type) |
110 | { | |
111 | /* The current implementation is a bit of a hack, but there's | |
112 | nothing else in the debuginfo to distinguish a tuple from a | |
113 | struct. */ | |
78134374 | 114 | return (type->code () == TYPE_CODE_STRUCT |
7d93a1e0 SM |
115 | && type->name () != NULL |
116 | && type->name ()[0] == '('); | |
c44af4eb TT |
117 | } |
118 | ||
c44af4eb | 119 | /* Return true if all non-static fields of a structlike type are in a |
c9317f21 | 120 | sequence like __0, __1, __2. */ |
c44af4eb | 121 | |
65c40c95 | 122 | static bool |
c9317f21 | 123 | rust_underscore_fields (struct type *type) |
c44af4eb TT |
124 | { |
125 | int i, field_number; | |
126 | ||
127 | field_number = 0; | |
128 | ||
78134374 | 129 | if (type->code () != TYPE_CODE_STRUCT) |
65c40c95 | 130 | return false; |
1f704f76 | 131 | for (i = 0; i < type->num_fields (); ++i) |
c44af4eb | 132 | { |
ceacbf6e | 133 | if (!field_is_static (&type->field (i))) |
c44af4eb | 134 | { |
c9317f21 | 135 | char buf[20]; |
c44af4eb | 136 | |
c9317f21 | 137 | xsnprintf (buf, sizeof (buf), "__%d", field_number); |
33d16dd9 | 138 | if (strcmp (buf, type->field (i).name ()) != 0) |
c9317f21 TT |
139 | return false; |
140 | field_number++; | |
c44af4eb TT |
141 | } |
142 | } | |
65c40c95 | 143 | return true; |
c44af4eb TT |
144 | } |
145 | ||
146 | /* See rust-lang.h. */ | |
147 | ||
65c40c95 | 148 | bool |
c44af4eb TT |
149 | rust_tuple_struct_type_p (struct type *type) |
150 | { | |
12df5c00 TT |
151 | /* This is just an approximation until DWARF can represent Rust more |
152 | precisely. We exclude zero-length structs because they may not | |
153 | be tuple structs, and there's no way to tell. */ | |
1f704f76 | 154 | return type->num_fields () > 0 && rust_underscore_fields (type); |
c44af4eb TT |
155 | } |
156 | ||
157 | /* Return true if TYPE is a slice type, otherwise false. */ | |
158 | ||
65c40c95 | 159 | static bool |
c44af4eb TT |
160 | rust_slice_type_p (struct type *type) |
161 | { | |
506ec52e TT |
162 | if (type->code () == TYPE_CODE_STRUCT |
163 | && type->name () != NULL | |
164 | && type->num_fields () == 2) | |
165 | { | |
166 | /* The order of fields doesn't matter. While it would be nice | |
167 | to check for artificiality here, the Rust compiler doesn't | |
168 | emit this information. */ | |
169 | const char *n1 = type->field (0).name (); | |
170 | const char *n2 = type->field (1).name (); | |
171 | return ((streq (n1, "data_ptr") && streq (n2, "length")) | |
172 | || (streq (n2, "data_ptr") && streq (n1, "length"))); | |
173 | } | |
174 | return false; | |
c44af4eb TT |
175 | } |
176 | ||
177 | /* Return true if TYPE is a range type, otherwise false. */ | |
178 | ||
65c40c95 | 179 | static bool |
c44af4eb TT |
180 | rust_range_type_p (struct type *type) |
181 | { | |
182 | int i; | |
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 TT |
192 | |
193 | i = 0; | |
33d16dd9 | 194 | if (strcmp (type->field (0).name (), "start") == 0) |
c44af4eb | 195 | { |
1f704f76 | 196 | if (type->num_fields () == 1) |
65c40c95 | 197 | return true; |
c44af4eb TT |
198 | i = 1; |
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 | ||
33d16dd9 | 206 | return strcmp (type->field (i).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 | ||
c44af4eb TT |
273 | \f |
274 | ||
1c485265 | 275 | /* See language.h. */ |
c44af4eb | 276 | |
1c485265 AB |
277 | void |
278 | rust_language::printstr (struct ui_file *stream, struct type *type, | |
279 | const gdb_byte *string, unsigned int length, | |
280 | const char *user_encoding, int force_ellipses, | |
281 | const struct value_print_options *options) const | |
c44af4eb TT |
282 | { |
283 | /* Rust always uses UTF-8, but let the caller override this if need | |
284 | be. */ | |
285 | const char *encoding = user_encoding; | |
286 | if (user_encoding == NULL || !*user_encoding) | |
287 | { | |
288 | /* In Rust strings, characters are "u8". */ | |
289 | if (rust_u8_type_p (type)) | |
290 | encoding = "UTF-8"; | |
291 | else | |
292 | { | |
293 | /* This is probably some C string, so let's let C deal with | |
294 | it. */ | |
3a3bb6eb TT |
295 | language_defn::printstr (stream, type, string, length, |
296 | user_encoding, force_ellipses, | |
297 | options); | |
c44af4eb TT |
298 | return; |
299 | } | |
300 | } | |
301 | ||
302 | /* This is not ideal as it doesn't use our character printer. */ | |
303 | generic_printstr (stream, type, string, length, encoding, force_ellipses, | |
304 | '"', 0, options); | |
305 | } | |
306 | ||
307 | \f | |
308 | ||
506ec52e TT |
309 | static const struct generic_val_print_decorations rust_decorations = |
310 | { | |
311 | /* Complex isn't used in Rust, but we provide C-ish values just in | |
312 | case. */ | |
313 | "", | |
314 | " + ", | |
315 | " * I", | |
316 | "true", | |
317 | "false", | |
318 | "()", | |
319 | "[", | |
320 | "]" | |
321 | }; | |
322 | ||
323 | /* Helper function to print a slice. */ | |
45320ffa TT |
324 | |
325 | static void | |
506ec52e TT |
326 | rust_val_print_slice (struct value *val, struct ui_file *stream, int recurse, |
327 | const struct value_print_options *options) | |
45320ffa | 328 | { |
158cc4fe | 329 | struct value *base = value_struct_elt (&val, {}, "data_ptr", NULL, |
45320ffa | 330 | "slice"); |
158cc4fe | 331 | struct value *len = value_struct_elt (&val, {}, "length", NULL, "slice"); |
45320ffa | 332 | |
d0c97917 | 333 | struct type *type = check_typedef (val->type ()); |
506ec52e | 334 | if (strcmp (type->name (), "&str") == 0) |
d0c97917 | 335 | val_print_string (base->type ()->target_type (), "UTF-8", |
506ec52e TT |
336 | value_as_address (base), value_as_long (len), stream, |
337 | options); | |
338 | else | |
339 | { | |
340 | LONGEST llen = value_as_long (len); | |
341 | ||
d0c97917 | 342 | type_print (val->type (), "", stream, -1); |
506ec52e TT |
343 | gdb_printf (stream, " "); |
344 | ||
345 | if (llen == 0) | |
346 | gdb_printf (stream, "[]"); | |
347 | else | |
348 | { | |
d0c97917 | 349 | struct type *elt_type = base->type ()->target_type (); |
506ec52e TT |
350 | struct type *array_type = lookup_array_range_type (elt_type, 0, |
351 | llen - 1); | |
cbe793af | 352 | struct value *array = value::allocate_lazy (array_type); |
6f9c9d71 | 353 | array->set_lval (lval_memory); |
9feb2d07 | 354 | array->set_address (value_as_address (base)); |
78259c36 | 355 | array->fetch_lazy (); |
506ec52e TT |
356 | generic_value_print (array, stream, recurse, options, |
357 | &rust_decorations); | |
358 | } | |
359 | } | |
45320ffa TT |
360 | } |
361 | ||
1c485265 | 362 | /* See rust-lang.h. */ |
b96645f1 | 363 | |
1c485265 AB |
364 | void |
365 | rust_language::val_print_struct | |
366 | (struct value *val, struct ui_file *stream, int recurse, | |
367 | const struct value_print_options *options) const | |
b96645f1 MG |
368 | { |
369 | int i; | |
370 | int first_field; | |
d0c97917 | 371 | struct type *type = check_typedef (val->type ()); |
45320ffa | 372 | |
506ec52e | 373 | if (rust_slice_type_p (type)) |
45320ffa | 374 | { |
506ec52e | 375 | rust_val_print_slice (val, stream, recurse, options); |
45320ffa TT |
376 | return; |
377 | } | |
378 | ||
65c40c95 TT |
379 | bool is_tuple = rust_tuple_type_p (type); |
380 | bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type); | |
b96645f1 MG |
381 | struct value_print_options opts; |
382 | ||
383 | if (!is_tuple) | |
384 | { | |
7d93a1e0 | 385 | if (type->name () != NULL) |
6cb06a8c | 386 | gdb_printf (stream, "%s", type->name ()); |
b96645f1 | 387 | |
1f704f76 | 388 | if (type->num_fields () == 0) |
dda83cd7 | 389 | return; |
b96645f1 | 390 | |
7d93a1e0 | 391 | if (type->name () != NULL) |
0426ad51 | 392 | gdb_puts (" ", stream); |
b96645f1 MG |
393 | } |
394 | ||
395 | if (is_tuple || is_tuple_struct) | |
0426ad51 | 396 | gdb_puts ("(", stream); |
b96645f1 | 397 | else |
0426ad51 | 398 | gdb_puts ("{", stream); |
b96645f1 MG |
399 | |
400 | opts = *options; | |
dad6b350 | 401 | opts.deref_ref = false; |
b96645f1 MG |
402 | |
403 | first_field = 1; | |
1f704f76 | 404 | for (i = 0; i < type->num_fields (); ++i) |
b96645f1 | 405 | { |
ceacbf6e | 406 | if (field_is_static (&type->field (i))) |
dda83cd7 | 407 | continue; |
b96645f1 MG |
408 | |
409 | if (!first_field) | |
0426ad51 | 410 | gdb_puts (",", stream); |
b96645f1 MG |
411 | |
412 | if (options->prettyformat) | |
dda83cd7 | 413 | { |
0426ad51 | 414 | gdb_puts ("\n", stream); |
d0b1020b | 415 | print_spaces (2 + 2 * recurse, stream); |
dda83cd7 | 416 | } |
b96645f1 | 417 | else if (!first_field) |
0426ad51 | 418 | gdb_puts (" ", stream); |
b96645f1 MG |
419 | |
420 | first_field = 0; | |
421 | ||
422 | if (!is_tuple && !is_tuple_struct) | |
dda83cd7 | 423 | { |
33d16dd9 | 424 | fputs_styled (type->field (i).name (), |
3f0cbb04 | 425 | variable_name_style.style (), stream); |
0426ad51 | 426 | gdb_puts (": ", stream); |
dda83cd7 | 427 | } |
b96645f1 | 428 | |
887e7158 TT |
429 | common_val_print (value_field (val, i), stream, recurse + 1, &opts, |
430 | this); | |
b96645f1 MG |
431 | } |
432 | ||
433 | if (options->prettyformat) | |
434 | { | |
0426ad51 | 435 | gdb_puts ("\n", stream); |
d0b1020b | 436 | print_spaces (2 * recurse, stream); |
b96645f1 MG |
437 | } |
438 | ||
439 | if (is_tuple || is_tuple_struct) | |
0426ad51 | 440 | gdb_puts (")", stream); |
b96645f1 | 441 | else |
0426ad51 | 442 | gdb_puts ("}", stream); |
b96645f1 MG |
443 | } |
444 | ||
1c485265 | 445 | /* See rust-lang.h. */ |
c9317f21 | 446 | |
1c485265 AB |
447 | void |
448 | rust_language::print_enum (struct value *val, struct ui_file *stream, | |
449 | int recurse, | |
450 | const struct value_print_options *options) const | |
c9317f21 TT |
451 | { |
452 | struct value_print_options opts = *options; | |
d0c97917 | 453 | struct type *type = check_typedef (val->type ()); |
c9317f21 | 454 | |
dad6b350 | 455 | opts.deref_ref = false; |
c9317f21 | 456 | |
9c6a1327 | 457 | gdb_assert (rust_enum_p (type)); |
50888e42 | 458 | gdb::array_view<const gdb_byte> view |
efaf1ae0 | 459 | (val->contents_for_printing ().data (), |
d0c97917 | 460 | val->type ()->length ()); |
9feb2d07 | 461 | type = resolve_dynamic_type (type, view, val->address ()); |
9c6a1327 | 462 | |
098b2108 TT |
463 | if (rust_empty_enum_p (type)) |
464 | { | |
465 | /* Print the enum type name here to be more clear. */ | |
6cb06a8c TT |
466 | gdb_printf (stream, _("%s {%p[<No data fields>%p]}"), |
467 | type->name (), | |
468 | metadata_style.style ().ptr (), nullptr); | |
098b2108 TT |
469 | return; |
470 | } | |
471 | ||
9c6a1327 TT |
472 | int variant_fieldno = rust_enum_variant (type); |
473 | val = value_field (val, variant_fieldno); | |
940da03e | 474 | struct type *variant_type = type->field (variant_fieldno).type (); |
c9317f21 | 475 | |
1f704f76 | 476 | int nfields = variant_type->num_fields (); |
c9317f21 TT |
477 | |
478 | bool is_tuple = rust_tuple_struct_type_p (variant_type); | |
479 | ||
6cb06a8c | 480 | gdb_printf (stream, "%s", variant_type->name ()); |
c9317f21 TT |
481 | if (nfields == 0) |
482 | { | |
483 | /* In case of a nullary variant like 'None', just output | |
484 | the name. */ | |
485 | return; | |
486 | } | |
487 | ||
488 | /* In case of a non-nullary variant, we output 'Foo(x,y,z)'. */ | |
489 | if (is_tuple) | |
6cb06a8c | 490 | gdb_printf (stream, "("); |
c9317f21 TT |
491 | else |
492 | { | |
493 | /* struct variant. */ | |
6cb06a8c | 494 | gdb_printf (stream, "{"); |
c9317f21 TT |
495 | } |
496 | ||
497 | bool first_field = true; | |
8ac460b7 | 498 | for (int j = 0; j < nfields; j++) |
c9317f21 TT |
499 | { |
500 | if (!first_field) | |
0426ad51 | 501 | gdb_puts (", ", stream); |
c9317f21 TT |
502 | first_field = false; |
503 | ||
504 | if (!is_tuple) | |
6cb06a8c TT |
505 | gdb_printf (stream, "%ps: ", |
506 | styled_string (variable_name_style.style (), | |
507 | variant_type->field (j).name ())); | |
c9317f21 | 508 | |
887e7158 TT |
509 | common_val_print (value_field (val, j), stream, recurse + 1, &opts, |
510 | this); | |
c9317f21 TT |
511 | } |
512 | ||
513 | if (is_tuple) | |
0426ad51 | 514 | gdb_puts (")", stream); |
c9317f21 | 515 | else |
0426ad51 | 516 | gdb_puts ("}", stream); |
c9317f21 TT |
517 | } |
518 | ||
1c485265 AB |
519 | /* See language.h. */ |
520 | ||
521 | void | |
522 | rust_language::value_print_inner | |
523 | (struct value *val, struct ui_file *stream, int recurse, | |
524 | const struct value_print_options *options) const | |
5f56f7cb TT |
525 | { |
526 | struct value_print_options opts = *options; | |
dad6b350 | 527 | opts.deref_ref = true; |
5f56f7cb TT |
528 | |
529 | if (opts.prettyformat == Val_prettyformat_default) | |
530 | opts.prettyformat = (opts.prettyformat_structs | |
531 | ? Val_prettyformat : Val_no_prettyformat); | |
532 | ||
d0c97917 | 533 | struct type *type = check_typedef (val->type ()); |
78134374 | 534 | switch (type->code ()) |
c44af4eb TT |
535 | { |
536 | case TYPE_CODE_PTR: | |
537 | { | |
538 | LONGEST low_bound, high_bound; | |
539 | ||
27710edb SM |
540 | if (type->target_type ()->code () == TYPE_CODE_ARRAY |
541 | && rust_u8_type_p (type->target_type ()->target_type ()) | |
542 | && get_array_bounds (type->target_type (), &low_bound, | |
5f56f7cb TT |
543 | &high_bound)) |
544 | { | |
545 | /* We have a pointer to a byte string, so just print | |
546 | that. */ | |
27710edb | 547 | struct type *elttype = check_typedef (type->target_type ()); |
5f56f7cb | 548 | CORE_ADDR addr = value_as_address (val); |
8ee511af | 549 | struct gdbarch *arch = type->arch (); |
c44af4eb | 550 | |
5f56f7cb TT |
551 | if (opts.addressprint) |
552 | { | |
0426ad51 TT |
553 | gdb_puts (paddress (arch, addr), stream); |
554 | gdb_puts (" ", stream); | |
5f56f7cb | 555 | } |
c44af4eb | 556 | |
0426ad51 | 557 | gdb_puts ("b", stream); |
27710edb | 558 | val_print_string (elttype->target_type (), "ASCII", addr, |
5f56f7cb TT |
559 | high_bound - low_bound + 1, stream, |
560 | &opts); | |
561 | break; | |
562 | } | |
c44af4eb | 563 | } |
5f56f7cb | 564 | goto generic_print; |
c44af4eb | 565 | |
c44af4eb TT |
566 | case TYPE_CODE_INT: |
567 | /* Recognize the unit type. */ | |
df86565b | 568 | if (type->is_unsigned () && type->length () == 0 |
7d93a1e0 | 569 | && type->name () != NULL && strcmp (type->name (), "()") == 0) |
c44af4eb | 570 | { |
0426ad51 | 571 | gdb_puts ("()", stream); |
c44af4eb TT |
572 | break; |
573 | } | |
574 | goto generic_print; | |
575 | ||
576 | case TYPE_CODE_STRING: | |
577 | { | |
c44af4eb TT |
578 | LONGEST low_bound, high_bound; |
579 | ||
580 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
581 | error (_("Could not determine the array bounds")); | |
582 | ||
583 | /* If we see a plain TYPE_CODE_STRING, then we're printing a | |
584 | byte string, hence the choice of "ASCII" as the | |
585 | encoding. */ | |
0426ad51 | 586 | gdb_puts ("b", stream); |
27710edb | 587 | printstr (stream, type->target_type (), |
efaf1ae0 | 588 | val->contents_for_printing ().data (), |
1c485265 | 589 | high_bound - low_bound + 1, "ASCII", 0, &opts); |
c44af4eb TT |
590 | } |
591 | break; | |
592 | ||
593 | case TYPE_CODE_ARRAY: | |
594 | { | |
595 | LONGEST low_bound, high_bound; | |
596 | ||
597 | if (get_array_bounds (type, &low_bound, &high_bound) | |
598 | && high_bound - low_bound + 1 == 0) | |
0426ad51 | 599 | gdb_puts ("[]", stream); |
c44af4eb TT |
600 | else |
601 | goto generic_print; | |
602 | } | |
603 | break; | |
604 | ||
605 | case TYPE_CODE_UNION: | |
c9317f21 TT |
606 | /* Untagged unions are printed as if they are structs. Since |
607 | the field bit positions overlap in the debuginfo, the code | |
608 | for printing a union is same as that for a struct, the only | |
609 | difference is that the input type will have overlapping | |
610 | fields. */ | |
5f56f7cb | 611 | val_print_struct (val, stream, recurse, &opts); |
c44af4eb TT |
612 | break; |
613 | ||
614 | case TYPE_CODE_STRUCT: | |
c9317f21 | 615 | if (rust_enum_p (type)) |
1c485265 | 616 | print_enum (val, stream, recurse, &opts); |
c9317f21 | 617 | else |
5f56f7cb | 618 | val_print_struct (val, stream, recurse, &opts); |
b96645f1 | 619 | break; |
c44af4eb | 620 | |
b96645f1 MG |
621 | default: |
622 | generic_print: | |
623 | /* Nothing special yet. */ | |
5f56f7cb | 624 | generic_value_print (val, stream, recurse, &opts, &rust_decorations); |
b96645f1 MG |
625 | } |
626 | } | |
c44af4eb | 627 | |
1c9cc052 TT |
628 | /* See language.h. */ |
629 | ||
630 | void | |
631 | rust_language::value_print | |
632 | (struct value *val, struct ui_file *stream, | |
633 | const struct value_print_options *options) const | |
634 | { | |
635 | value_print_options opts = *options; | |
636 | opts.deref_ref = true; | |
637 | ||
d0c97917 | 638 | struct type *type = check_typedef (val->type ()); |
1c9cc052 TT |
639 | if (type->is_pointer_or_reference ()) |
640 | { | |
641 | gdb_printf (stream, "("); | |
d0c97917 | 642 | type_print (val->type (), "", stream, -1); |
1c9cc052 TT |
643 | gdb_printf (stream, ") "); |
644 | } | |
645 | ||
646 | return common_val_print (val, stream, 0, &opts, this); | |
647 | } | |
648 | ||
b96645f1 | 649 | \f |
c44af4eb | 650 | |
b96645f1 | 651 | static void |
c9317f21 TT |
652 | rust_internal_print_type (struct type *type, const char *varstring, |
653 | struct ui_file *stream, int show, int level, | |
654 | const struct type_print_options *flags, | |
a33ccfc7 | 655 | bool for_rust_enum, print_offset_data *podata); |
b96645f1 MG |
656 | |
657 | /* Print a struct or union typedef. */ | |
658 | static void | |
659 | rust_print_struct_def (struct type *type, const char *varstring, | |
b50f188d | 660 | struct ui_file *stream, int show, int level, |
c9317f21 | 661 | const struct type_print_options *flags, |
a33ccfc7 | 662 | bool for_rust_enum, print_offset_data *podata) |
b96645f1 | 663 | { |
b50f188d TT |
664 | /* Print a tuple type simply. */ |
665 | if (rust_tuple_type_p (type)) | |
666 | { | |
0426ad51 | 667 | gdb_puts (type->name (), stream); |
b50f188d TT |
668 | return; |
669 | } | |
c44af4eb | 670 | |
b50f188d TT |
671 | /* If we see a base class, delegate to C. */ |
672 | if (TYPE_N_BASECLASSES (type) > 0) | |
1c6fbf42 | 673 | c_print_type (type, varstring, stream, show, level, language_rust, flags); |
c44af4eb | 674 | |
a33ccfc7 TT |
675 | if (flags->print_offsets) |
676 | { | |
677 | /* Temporarily bump the level so that the output lines up | |
678 | correctly. */ | |
679 | level += 2; | |
680 | } | |
681 | ||
c9317f21 TT |
682 | /* Compute properties of TYPE here because, in the enum case, the |
683 | rest of the code ends up looking only at the variant part. */ | |
7d93a1e0 | 684 | const char *tagname = type->name (); |
c9317f21 TT |
685 | bool is_tuple_struct = rust_tuple_struct_type_p (type); |
686 | bool is_tuple = rust_tuple_type_p (type); | |
687 | bool is_enum = rust_enum_p (type); | |
b96645f1 | 688 | |
c9317f21 TT |
689 | if (for_rust_enum) |
690 | { | |
691 | /* Already printing an outer enum, so nothing to print here. */ | |
692 | } | |
693 | else | |
694 | { | |
695 | /* This code path is also used by unions and enums. */ | |
696 | if (is_enum) | |
697 | { | |
0426ad51 | 698 | gdb_puts ("enum ", stream); |
24e99c6c | 699 | dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS); |
8c2e4e06 SM |
700 | if (prop != nullptr && prop->kind () == PROP_TYPE) |
701 | type = prop->original_type (); | |
c9317f21 | 702 | } |
78134374 | 703 | else if (type->code () == TYPE_CODE_STRUCT) |
0426ad51 | 704 | gdb_puts ("struct ", stream); |
c9317f21 | 705 | else |
0426ad51 | 706 | gdb_puts ("union ", stream); |
b96645f1 | 707 | |
c9317f21 | 708 | if (tagname != NULL) |
0426ad51 | 709 | gdb_puts (tagname, stream); |
c9317f21 | 710 | } |
b96645f1 | 711 | |
1f704f76 | 712 | if (type->num_fields () == 0 && !is_tuple) |
b50f188d | 713 | return; |
a33ccfc7 | 714 | if (for_rust_enum && !flags->print_offsets) |
0426ad51 | 715 | gdb_puts (is_tuple_struct ? "(" : "{", stream); |
c9317f21 | 716 | else |
0426ad51 | 717 | gdb_puts (is_tuple_struct ? " (\n" : " {\n", stream); |
c44af4eb | 718 | |
a33ccfc7 TT |
719 | /* When printing offsets, we rearrange the fields into storage |
720 | order. This lets us show holes more clearly. We work using | |
721 | field indices here because it simplifies calls to | |
722 | print_offset_data::update below. */ | |
723 | std::vector<int> fields; | |
1f704f76 | 724 | for (int i = 0; i < type->num_fields (); ++i) |
b50f188d | 725 | { |
ceacbf6e | 726 | if (field_is_static (&type->field (i))) |
b50f188d | 727 | continue; |
9c6a1327 | 728 | if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)) |
a33ccfc7 TT |
729 | continue; |
730 | fields.push_back (i); | |
731 | } | |
732 | if (flags->print_offsets) | |
733 | std::sort (fields.begin (), fields.end (), | |
734 | [&] (int a, int b) | |
735 | { | |
b610c045 SM |
736 | return (type->field (a).loc_bitpos () |
737 | < type->field (b).loc_bitpos ()); | |
a33ccfc7 TT |
738 | }); |
739 | ||
740 | for (int i : fields) | |
741 | { | |
742 | QUIT; | |
743 | ||
ceacbf6e | 744 | gdb_assert (!field_is_static (&type->field (i))); |
9c6a1327 | 745 | gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))); |
a33ccfc7 TT |
746 | |
747 | if (flags->print_offsets) | |
748 | podata->update (type, i, stream); | |
b50f188d TT |
749 | |
750 | /* We'd like to print "pub" here as needed, but rustc | |
751 | doesn't emit the debuginfo, and our types don't have | |
752 | cplus_struct_type attached. */ | |
753 | ||
754 | /* For a tuple struct we print the type but nothing | |
755 | else. */ | |
a33ccfc7 | 756 | if (!for_rust_enum || flags->print_offsets) |
d0b1020b | 757 | print_spaces (level + 2, stream); |
c9317f21 | 758 | if (is_enum) |
33d16dd9 | 759 | fputs_styled (type->field (i).name (), variable_name_style.style (), |
3f0cbb04 | 760 | stream); |
c9317f21 | 761 | else if (!is_tuple_struct) |
6cb06a8c TT |
762 | gdb_printf (stream, "%ps: ", |
763 | styled_string (variable_name_style.style (), | |
764 | type->field (i).name ())); | |
b50f188d | 765 | |
940da03e | 766 | rust_internal_print_type (type->field (i).type (), NULL, |
53d7df28 | 767 | stream, (is_enum ? show : show - 1), |
a33ccfc7 TT |
768 | level + 2, flags, is_enum, podata); |
769 | if (!for_rust_enum || flags->print_offsets) | |
0426ad51 | 770 | gdb_puts (",\n", stream); |
a33ccfc7 TT |
771 | /* Note that this check of "I" is ok because we only sorted the |
772 | fields by offset when print_offsets was set, so we won't take | |
773 | this branch in that case. */ | |
1f704f76 | 774 | else if (i + 1 < type->num_fields ()) |
0426ad51 | 775 | gdb_puts (", ", stream); |
b50f188d | 776 | } |
c44af4eb | 777 | |
a33ccfc7 TT |
778 | if (flags->print_offsets) |
779 | { | |
780 | /* Undo the temporary level increase we did above. */ | |
781 | level -= 2; | |
782 | podata->finish (type, level, stream); | |
d0b1020b | 783 | print_spaces (print_offset_data::indentation, stream); |
a33ccfc7 | 784 | if (level == 0) |
d0b1020b | 785 | print_spaces (2, stream); |
a33ccfc7 TT |
786 | } |
787 | if (!for_rust_enum || flags->print_offsets) | |
d0b1020b | 788 | print_spaces (level, stream); |
0426ad51 | 789 | gdb_puts (is_tuple_struct ? ")" : "}", stream); |
c44af4eb TT |
790 | } |
791 | ||
c44af4eb TT |
792 | /* la_print_type implementation for Rust. */ |
793 | ||
794 | static void | |
c9317f21 TT |
795 | rust_internal_print_type (struct type *type, const char *varstring, |
796 | struct ui_file *stream, int show, int level, | |
797 | const struct type_print_options *flags, | |
a33ccfc7 | 798 | bool for_rust_enum, print_offset_data *podata) |
c44af4eb | 799 | { |
c44af4eb TT |
800 | QUIT; |
801 | if (show <= 0 | |
7d93a1e0 | 802 | && type->name () != NULL) |
c44af4eb | 803 | { |
921d8f54 | 804 | /* Rust calls the unit type "void" in its debuginfo, |
dda83cd7 | 805 | but we don't want to print it as that. */ |
78134374 | 806 | if (type->code () == TYPE_CODE_VOID) |
0426ad51 | 807 | gdb_puts ("()", stream); |
921d8f54 | 808 | else |
0426ad51 | 809 | gdb_puts (type->name (), stream); |
c44af4eb TT |
810 | return; |
811 | } | |
812 | ||
813 | type = check_typedef (type); | |
78134374 | 814 | switch (type->code ()) |
c44af4eb | 815 | { |
921d8f54 | 816 | case TYPE_CODE_VOID: |
c9317f21 TT |
817 | /* If we have an enum, we've already printed the type's |
818 | unqualified name, and there is nothing else to print | |
819 | here. */ | |
820 | if (!for_rust_enum) | |
0426ad51 | 821 | gdb_puts ("()", stream); |
921d8f54 MG |
822 | break; |
823 | ||
c44af4eb TT |
824 | case TYPE_CODE_FUNC: |
825 | /* Delegate varargs to the C printer. */ | |
a409645d | 826 | if (type->has_varargs ()) |
c44af4eb TT |
827 | goto c_printer; |
828 | ||
0426ad51 | 829 | gdb_puts ("fn ", stream); |
c44af4eb | 830 | if (varstring != NULL) |
0426ad51 TT |
831 | gdb_puts (varstring, stream); |
832 | gdb_puts ("(", stream); | |
1f704f76 | 833 | for (int i = 0; i < type->num_fields (); ++i) |
c44af4eb TT |
834 | { |
835 | QUIT; | |
836 | if (i > 0) | |
0426ad51 | 837 | gdb_puts (", ", stream); |
940da03e | 838 | rust_internal_print_type (type->field (i).type (), "", stream, |
a33ccfc7 | 839 | -1, 0, flags, false, podata); |
c44af4eb | 840 | } |
0426ad51 | 841 | gdb_puts (")", stream); |
921d8f54 | 842 | /* If it returns unit, we can omit the return type. */ |
27710edb | 843 | if (type->target_type ()->code () != TYPE_CODE_VOID) |
dda83cd7 | 844 | { |
0426ad51 | 845 | gdb_puts (" -> ", stream); |
27710edb | 846 | rust_internal_print_type (type->target_type (), "", stream, |
a33ccfc7 | 847 | -1, 0, flags, false, podata); |
dda83cd7 | 848 | } |
c44af4eb TT |
849 | break; |
850 | ||
851 | case TYPE_CODE_ARRAY: | |
852 | { | |
853 | LONGEST low_bound, high_bound; | |
854 | ||
0426ad51 | 855 | gdb_puts ("[", stream); |
27710edb | 856 | rust_internal_print_type (type->target_type (), NULL, |
a33ccfc7 TT |
857 | stream, show - 1, level, flags, false, |
858 | podata); | |
c44af4eb | 859 | |
cf88be68 SM |
860 | if (type->bounds ()->high.kind () == PROP_LOCEXPR |
861 | || type->bounds ()->high.kind () == PROP_LOCLIST) | |
6cb06a8c | 862 | gdb_printf (stream, "; variable length"); |
c44af4eb | 863 | else if (get_array_bounds (type, &low_bound, &high_bound)) |
6cb06a8c TT |
864 | gdb_printf (stream, "; %s", |
865 | plongest (high_bound - low_bound + 1)); | |
0426ad51 | 866 | gdb_puts ("]", stream); |
c44af4eb TT |
867 | } |
868 | break; | |
869 | ||
c9317f21 | 870 | case TYPE_CODE_UNION: |
c44af4eb | 871 | case TYPE_CODE_STRUCT: |
c9317f21 | 872 | rust_print_struct_def (type, varstring, stream, show, level, flags, |
a33ccfc7 | 873 | for_rust_enum, podata); |
c44af4eb TT |
874 | break; |
875 | ||
876 | case TYPE_CODE_ENUM: | |
877 | { | |
b926417a | 878 | int len = 0; |
c44af4eb | 879 | |
0426ad51 | 880 | gdb_puts ("enum ", stream); |
7d93a1e0 | 881 | if (type->name () != NULL) |
c44af4eb | 882 | { |
0426ad51 TT |
883 | gdb_puts (type->name (), stream); |
884 | gdb_puts (" ", stream); | |
7d93a1e0 | 885 | len = strlen (type->name ()); |
c44af4eb | 886 | } |
0426ad51 | 887 | gdb_puts ("{\n", stream); |
c44af4eb | 888 | |
1f704f76 | 889 | for (int i = 0; i < type->num_fields (); ++i) |
c44af4eb | 890 | { |
33d16dd9 | 891 | const char *name = type->field (i).name (); |
c44af4eb TT |
892 | |
893 | QUIT; | |
894 | ||
895 | if (len > 0 | |
7d93a1e0 | 896 | && strncmp (name, type->name (), len) == 0 |
c44af4eb TT |
897 | && name[len] == ':' |
898 | && name[len + 1] == ':') | |
899 | name += len + 2; | |
6cb06a8c TT |
900 | gdb_printf (stream, "%*s%ps,\n", |
901 | level + 2, "", | |
902 | styled_string (variable_name_style.style (), | |
903 | name)); | |
c44af4eb TT |
904 | } |
905 | ||
0426ad51 | 906 | gdb_puts ("}", stream); |
c44af4eb | 907 | } |
73fc52c4 TT |
908 | break; |
909 | ||
910 | case TYPE_CODE_PTR: | |
911 | { | |
7d93a1e0 | 912 | if (type->name () != nullptr) |
0426ad51 | 913 | gdb_puts (type->name (), stream); |
73fc52c4 TT |
914 | else |
915 | { | |
916 | /* We currently can't distinguish between pointers and | |
917 | references. */ | |
0426ad51 | 918 | gdb_puts ("*mut ", stream); |
27710edb | 919 | type_print (type->target_type (), "", stream, 0); |
73fc52c4 TT |
920 | } |
921 | } | |
c44af4eb TT |
922 | break; |
923 | ||
c44af4eb TT |
924 | default: |
925 | c_printer: | |
1c6fbf42 PA |
926 | c_print_type (type, varstring, stream, show, level, language_rust, |
927 | flags); | |
c44af4eb TT |
928 | } |
929 | } | |
930 | ||
931 | \f | |
932 | ||
c44af4eb TT |
933 | /* Like arch_composite_type, but uses TYPE to decide how to allocate |
934 | -- either on an obstack or on a gdbarch. */ | |
935 | ||
936 | static struct type * | |
937 | rust_composite_type (struct type *original, | |
938 | const char *name, | |
939 | const char *field1, struct type *type1, | |
940 | const char *field2, struct type *type2) | |
941 | { | |
942 | struct type *result = alloc_type_copy (original); | |
943 | int i, nfields, bitpos; | |
944 | ||
945 | nfields = 0; | |
946 | if (field1 != NULL) | |
947 | ++nfields; | |
948 | if (field2 != NULL) | |
949 | ++nfields; | |
950 | ||
67607e24 | 951 | result->set_code (TYPE_CODE_STRUCT); |
d0e39ea2 | 952 | result->set_name (name); |
c44af4eb | 953 | |
5e33d5f4 | 954 | result->set_num_fields (nfields); |
3cabb6b0 SM |
955 | result->set_fields |
956 | ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field))); | |
c44af4eb TT |
957 | |
958 | i = 0; | |
959 | bitpos = 0; | |
960 | if (field1 != NULL) | |
961 | { | |
ceacbf6e | 962 | struct field *field = &result->field (i); |
c44af4eb | 963 | |
cd3f655c | 964 | field->set_loc_bitpos (bitpos); |
df86565b | 965 | bitpos += type1->length () * TARGET_CHAR_BIT; |
c44af4eb | 966 | |
d3fd12df | 967 | field->set_name (field1); |
5d14b6e5 | 968 | field->set_type (type1); |
c44af4eb TT |
969 | ++i; |
970 | } | |
971 | if (field2 != NULL) | |
972 | { | |
ceacbf6e | 973 | struct field *field = &result->field (i); |
2fff16dd | 974 | unsigned align = type_align (type2); |
c44af4eb TT |
975 | |
976 | if (align != 0) | |
977 | { | |
978 | int delta; | |
979 | ||
980 | align *= TARGET_CHAR_BIT; | |
981 | delta = bitpos % align; | |
982 | if (delta != 0) | |
983 | bitpos += align - delta; | |
984 | } | |
cd3f655c | 985 | field->set_loc_bitpos (bitpos); |
c44af4eb | 986 | |
d3fd12df | 987 | field->set_name (field2); |
5d14b6e5 | 988 | field->set_type (type2); |
c44af4eb TT |
989 | ++i; |
990 | } | |
991 | ||
992 | if (i > 0) | |
b6cdbc9a | 993 | result->set_length (result->field (i - 1).loc_bitpos () / TARGET_CHAR_BIT |
df86565b | 994 | + result->field (i - 1).type ()->length ()); |
c44af4eb TT |
995 | return result; |
996 | } | |
997 | ||
998 | /* See rust-lang.h. */ | |
999 | ||
1000 | struct type * | |
1001 | rust_slice_type (const char *name, struct type *elt_type, | |
1002 | struct type *usize_type) | |
1003 | { | |
1004 | struct type *type; | |
1005 | ||
1006 | elt_type = lookup_pointer_type (elt_type); | |
1007 | type = rust_composite_type (elt_type, name, | |
1008 | "data_ptr", elt_type, | |
1009 | "length", usize_type); | |
1010 | ||
1011 | return type; | |
1012 | } | |
1013 | ||
c44af4eb TT |
1014 | \f |
1015 | ||
01739a3b | 1016 | /* A helper for rust_evaluate_subexp that handles OP_RANGE. */ |
c44af4eb | 1017 | |
9db6b6dd | 1018 | struct value * |
d148f803 TT |
1019 | rust_range (struct type *expect_type, struct expression *exp, |
1020 | enum noside noside, enum range_flag kind, | |
1021 | struct value *low, struct value *high) | |
c44af4eb | 1022 | { |
c44af4eb TT |
1023 | struct value *addrval, *result; |
1024 | CORE_ADDR addr; | |
1025 | struct type *range_type; | |
1026 | struct type *index_type; | |
1027 | struct type *temp_type; | |
1028 | const char *name; | |
1029 | ||
2f1b18db | 1030 | bool inclusive = !(kind & RANGE_HIGH_BOUND_EXCLUSIVE); |
c44af4eb | 1031 | |
c44af4eb TT |
1032 | if (low == NULL) |
1033 | { | |
1034 | if (high == NULL) | |
1035 | { | |
1036 | index_type = NULL; | |
1037 | name = "std::ops::RangeFull"; | |
1038 | } | |
1039 | else | |
1040 | { | |
d0c97917 | 1041 | index_type = high->type (); |
6873858b TT |
1042 | name = (inclusive |
1043 | ? "std::ops::RangeToInclusive" : "std::ops::RangeTo"); | |
c44af4eb TT |
1044 | } |
1045 | } | |
1046 | else | |
1047 | { | |
1048 | if (high == NULL) | |
1049 | { | |
d0c97917 | 1050 | index_type = low->type (); |
c44af4eb TT |
1051 | name = "std::ops::RangeFrom"; |
1052 | } | |
1053 | else | |
1054 | { | |
d0c97917 | 1055 | if (!types_equal (low->type (), high->type ())) |
c44af4eb | 1056 | error (_("Range expression with different types")); |
d0c97917 | 1057 | index_type = low->type (); |
6873858b | 1058 | name = inclusive ? "std::ops::RangeInclusive" : "std::ops::Range"; |
c44af4eb TT |
1059 | } |
1060 | } | |
1061 | ||
1062 | /* If we don't have an index type, just allocate this on the | |
1063 | arch. Here any type will do. */ | |
1064 | temp_type = (index_type == NULL | |
1065 | ? language_bool_type (exp->language_defn, exp->gdbarch) | |
1066 | : index_type); | |
1067 | /* It would be nicer to cache the range type. */ | |
1068 | range_type = rust_composite_type (temp_type, name, | |
1069 | low == NULL ? NULL : "start", index_type, | |
1070 | high == NULL ? NULL : "end", index_type); | |
1071 | ||
1072 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
ee7bb294 | 1073 | return value::zero (range_type, lval_memory); |
c44af4eb | 1074 | |
df86565b | 1075 | addrval = value_allocate_space_in_inferior (range_type->length ()); |
c44af4eb TT |
1076 | addr = value_as_long (addrval); |
1077 | result = value_at_lazy (range_type, addr); | |
1078 | ||
1079 | if (low != NULL) | |
1080 | { | |
158cc4fe | 1081 | struct value *start = value_struct_elt (&result, {}, "start", NULL, |
c44af4eb TT |
1082 | "range"); |
1083 | ||
1084 | value_assign (start, low); | |
1085 | } | |
1086 | ||
1087 | if (high != NULL) | |
1088 | { | |
158cc4fe | 1089 | struct value *end = value_struct_elt (&result, {}, "end", NULL, |
c44af4eb TT |
1090 | "range"); |
1091 | ||
1092 | value_assign (end, high); | |
1093 | } | |
1094 | ||
1095 | result = value_at_lazy (range_type, addr); | |
1096 | return result; | |
1097 | } | |
1098 | ||
1099 | /* A helper function to compute the range and kind given a range | |
1100 | value. TYPE is the type of the range value. RANGE is the range | |
1101 | value. LOW, HIGH, and KIND are out parameters. The LOW and HIGH | |
1102 | parameters might be filled in, or might not be, depending on the | |
1103 | kind of range this is. KIND will always be set to the appropriate | |
1104 | value describing the kind of range, and this can be used to | |
1105 | determine whether LOW or HIGH are valid. */ | |
1106 | ||
1107 | static void | |
1108 | rust_compute_range (struct type *type, struct value *range, | |
1109 | LONGEST *low, LONGEST *high, | |
f2d8e4c5 | 1110 | range_flags *kind) |
c44af4eb TT |
1111 | { |
1112 | int i; | |
1113 | ||
1114 | *low = 0; | |
1115 | *high = 0; | |
2f1b18db | 1116 | *kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT; |
c44af4eb | 1117 | |
1f704f76 | 1118 | if (type->num_fields () == 0) |
c44af4eb TT |
1119 | return; |
1120 | ||
1121 | i = 0; | |
33d16dd9 | 1122 | if (strcmp (type->field (0).name (), "start") == 0) |
c44af4eb | 1123 | { |
2f1b18db | 1124 | *kind = RANGE_HIGH_BOUND_DEFAULT; |
c44af4eb TT |
1125 | *low = value_as_long (value_field (range, 0)); |
1126 | ++i; | |
1127 | } | |
1f704f76 | 1128 | if (type->num_fields () > i |
33d16dd9 | 1129 | && strcmp (type->field (i).name (), "end") == 0) |
c44af4eb | 1130 | { |
2f1b18db AB |
1131 | *kind = (*kind == (RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT) |
1132 | ? RANGE_LOW_BOUND_DEFAULT : RANGE_STANDARD); | |
c44af4eb | 1133 | *high = value_as_long (value_field (range, i)); |
6873858b TT |
1134 | |
1135 | if (rust_inclusive_range_type_p (type)) | |
1136 | ++*high; | |
c44af4eb TT |
1137 | } |
1138 | } | |
1139 | ||
1140 | /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT. */ | |
1141 | ||
6ce1ad67 | 1142 | struct value * |
984af2cb TT |
1143 | rust_subscript (struct type *expect_type, struct expression *exp, |
1144 | enum noside noside, bool for_addr, | |
1145 | struct value *lhs, struct value *rhs) | |
c44af4eb | 1146 | { |
984af2cb | 1147 | struct value *result; |
c44af4eb | 1148 | struct type *rhstype; |
45f4ed92 | 1149 | LONGEST low, high_bound; |
c44af4eb | 1150 | /* Initialized to appease the compiler. */ |
f2d8e4c5 | 1151 | range_flags kind = RANGE_LOW_BOUND_DEFAULT | RANGE_HIGH_BOUND_DEFAULT; |
45f4ed92 | 1152 | LONGEST high = 0; |
c44af4eb TT |
1153 | int want_slice = 0; |
1154 | ||
d0c97917 | 1155 | rhstype = check_typedef (rhs->type ()); |
c44af4eb TT |
1156 | if (rust_range_type_p (rhstype)) |
1157 | { | |
1158 | if (!for_addr) | |
1159 | error (_("Can't take slice of array without '&'")); | |
1160 | rust_compute_range (rhstype, rhs, &low, &high, &kind); | |
1161 | want_slice = 1; | |
1162 | } | |
1163 | else | |
1164 | low = value_as_long (rhs); | |
1165 | ||
d0c97917 | 1166 | struct type *type = check_typedef (lhs->type ()); |
c44af4eb TT |
1167 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1168 | { | |
b3e3859b | 1169 | struct type *base_type = nullptr; |
78134374 | 1170 | if (type->code () == TYPE_CODE_ARRAY) |
27710edb | 1171 | base_type = type->target_type (); |
b3e3859b TT |
1172 | else if (rust_slice_type_p (type)) |
1173 | { | |
1f704f76 | 1174 | for (int i = 0; i < type->num_fields (); ++i) |
b3e3859b | 1175 | { |
33d16dd9 | 1176 | if (strcmp (type->field (i).name (), "data_ptr") == 0) |
b3e3859b | 1177 | { |
27710edb | 1178 | base_type = type->field (i).type ()->target_type (); |
b3e3859b TT |
1179 | break; |
1180 | } | |
1181 | } | |
1182 | if (base_type == nullptr) | |
1183 | error (_("Could not find 'data_ptr' in slice type")); | |
1184 | } | |
78134374 | 1185 | else if (type->code () == TYPE_CODE_PTR) |
27710edb | 1186 | base_type = type->target_type (); |
b3e3859b TT |
1187 | else |
1188 | error (_("Cannot subscript non-array type")); | |
1189 | ||
1190 | struct type *new_type; | |
1191 | if (want_slice) | |
1192 | { | |
1193 | if (rust_slice_type_p (type)) | |
1194 | new_type = type; | |
1195 | else | |
1196 | { | |
1197 | struct type *usize | |
1198 | = language_lookup_primitive_type (exp->language_defn, | |
1199 | exp->gdbarch, | |
1200 | "usize"); | |
1201 | new_type = rust_slice_type ("&[*gdb*]", base_type, usize); | |
1202 | } | |
1203 | } | |
1204 | else | |
1205 | new_type = base_type; | |
c44af4eb | 1206 | |
ee7bb294 | 1207 | return value::zero (new_type, VALUE_LVAL (lhs)); |
c44af4eb TT |
1208 | } |
1209 | else | |
1210 | { | |
1211 | LONGEST low_bound; | |
1212 | struct value *base; | |
c44af4eb | 1213 | |
78134374 | 1214 | if (type->code () == TYPE_CODE_ARRAY) |
c44af4eb TT |
1215 | { |
1216 | base = lhs; | |
1217 | if (!get_array_bounds (type, &low_bound, &high_bound)) | |
1218 | error (_("Can't compute array bounds")); | |
1219 | if (low_bound != 0) | |
1220 | error (_("Found array with non-zero lower bound")); | |
1221 | ++high_bound; | |
1222 | } | |
1223 | else if (rust_slice_type_p (type)) | |
1224 | { | |
1225 | struct value *len; | |
1226 | ||
158cc4fe AB |
1227 | base = value_struct_elt (&lhs, {}, "data_ptr", NULL, "slice"); |
1228 | len = value_struct_elt (&lhs, {}, "length", NULL, "slice"); | |
c44af4eb TT |
1229 | low_bound = 0; |
1230 | high_bound = value_as_long (len); | |
1231 | } | |
78134374 | 1232 | else if (type->code () == TYPE_CODE_PTR) |
42d94011 MG |
1233 | { |
1234 | base = lhs; | |
1235 | low_bound = 0; | |
1236 | high_bound = LONGEST_MAX; | |
1237 | } | |
c44af4eb TT |
1238 | else |
1239 | error (_("Cannot subscript non-array type")); | |
1240 | ||
2f1b18db | 1241 | if (want_slice && (kind & RANGE_LOW_BOUND_DEFAULT)) |
c44af4eb TT |
1242 | low = low_bound; |
1243 | if (low < 0) | |
1244 | error (_("Index less than zero")); | |
1245 | if (low > high_bound) | |
1246 | error (_("Index greater than length")); | |
1247 | ||
1248 | result = value_subscript (base, low); | |
1249 | } | |
1250 | ||
1251 | if (for_addr) | |
1252 | { | |
1253 | if (want_slice) | |
1254 | { | |
1255 | struct type *usize, *slice; | |
1256 | CORE_ADDR addr; | |
1257 | struct value *addrval, *tem; | |
1258 | ||
2f1b18db | 1259 | if (kind & RANGE_HIGH_BOUND_DEFAULT) |
c44af4eb TT |
1260 | high = high_bound; |
1261 | if (high < 0) | |
1262 | error (_("High index less than zero")); | |
1263 | if (low > high) | |
1264 | error (_("Low index greater than high index")); | |
1265 | if (high > high_bound) | |
1266 | error (_("High index greater than length")); | |
1267 | ||
1268 | usize = language_lookup_primitive_type (exp->language_defn, | |
1269 | exp->gdbarch, | |
1270 | "usize"); | |
45320ffa TT |
1271 | const char *new_name = ((type != nullptr |
1272 | && rust_slice_type_p (type)) | |
7d93a1e0 | 1273 | ? type->name () : "&[*gdb*]"); |
45320ffa | 1274 | |
d0c97917 | 1275 | slice = rust_slice_type (new_name, result->type (), usize); |
c44af4eb | 1276 | |
df86565b | 1277 | addrval = value_allocate_space_in_inferior (slice->length ()); |
c44af4eb TT |
1278 | addr = value_as_long (addrval); |
1279 | tem = value_at_lazy (slice, addr); | |
1280 | ||
1281 | value_assign (value_field (tem, 0), value_addr (result)); | |
1282 | value_assign (value_field (tem, 1), | |
1283 | value_from_longest (usize, high - low)); | |
1284 | ||
1285 | result = value_at_lazy (slice, addr); | |
1286 | } | |
1287 | else | |
1288 | result = value_addr (result); | |
1289 | } | |
1290 | ||
1291 | return result; | |
1292 | } | |
1293 | ||
f10522c0 TT |
1294 | namespace expr |
1295 | { | |
d123f9e4 | 1296 | |
11dd3dce | 1297 | struct value * |
f10522c0 TT |
1298 | rust_unop_ind_operation::evaluate (struct type *expect_type, |
1299 | struct expression *exp, | |
1300 | enum noside noside) | |
d123f9e4 | 1301 | { |
f10522c0 TT |
1302 | if (noside != EVAL_NORMAL) |
1303 | return unop_ind_operation::evaluate (expect_type, exp, noside); | |
1304 | ||
1305 | struct value *value = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
1306 | noside); | |
d123f9e4 TT |
1307 | struct value *trait_ptr = rust_get_trait_object_pointer (value); |
1308 | if (trait_ptr != NULL) | |
1309 | value = trait_ptr; | |
1310 | ||
1311 | return value_ind (value); | |
1312 | } | |
1313 | ||
f10522c0 TT |
1314 | } /* namespace expr */ |
1315 | ||
6fa9831f TT |
1316 | /* A helper function for UNOP_COMPLEMENT. */ |
1317 | ||
6fab4359 | 1318 | struct value * |
6fa9831f TT |
1319 | eval_op_rust_complement (struct type *expect_type, struct expression *exp, |
1320 | enum noside noside, | |
6fab4359 | 1321 | enum exp_opcode opcode, |
6fa9831f TT |
1322 | struct value *value) |
1323 | { | |
d0c97917 TT |
1324 | if (value->type ()->code () == TYPE_CODE_BOOL) |
1325 | return value_from_longest (value->type (), value_logical_not (value)); | |
6fa9831f TT |
1326 | return value_complement (value); |
1327 | } | |
1328 | ||
05104233 TT |
1329 | /* A helper function for OP_ARRAY. */ |
1330 | ||
6fab4359 | 1331 | struct value * |
05104233 TT |
1332 | eval_op_rust_array (struct type *expect_type, struct expression *exp, |
1333 | enum noside noside, | |
6fab4359 | 1334 | enum exp_opcode opcode, |
05104233 TT |
1335 | struct value *elt, struct value *ncopies) |
1336 | { | |
1337 | int copies = value_as_long (ncopies); | |
1338 | if (copies < 0) | |
1339 | error (_("Array with negative number of elements")); | |
1340 | ||
1341 | if (noside == EVAL_NORMAL) | |
1342 | { | |
1343 | int i; | |
1344 | std::vector<struct value *> eltvec (copies); | |
1345 | ||
1346 | for (i = 0; i < copies; ++i) | |
1347 | eltvec[i] = elt; | |
1348 | return value_array (0, copies - 1, eltvec.data ()); | |
1349 | } | |
1350 | else | |
1351 | { | |
1352 | struct type *arraytype | |
d0c97917 | 1353 | = lookup_array_range_type (elt->type (), 0, copies - 1); |
317c3ed9 | 1354 | return value::allocate (arraytype); |
05104233 TT |
1355 | } |
1356 | } | |
1357 | ||
f10522c0 TT |
1358 | namespace expr |
1359 | { | |
575cae23 | 1360 | |
e4407a20 | 1361 | struct value * |
f10522c0 TT |
1362 | rust_struct_anon::evaluate (struct type *expect_type, |
1363 | struct expression *exp, | |
1364 | enum noside noside) | |
575cae23 | 1365 | { |
f10522c0 TT |
1366 | value *lhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside); |
1367 | int field_number = std::get<0> (m_storage); | |
1368 | ||
d0c97917 | 1369 | struct type *type = lhs->type (); |
575cae23 TT |
1370 | |
1371 | if (type->code () == TYPE_CODE_STRUCT) | |
1372 | { | |
1373 | struct type *outer_type = NULL; | |
1374 | ||
1375 | if (rust_enum_p (type)) | |
1376 | { | |
efaf1ae0 | 1377 | type = resolve_dynamic_type (type, lhs->contents (), |
9feb2d07 | 1378 | lhs->address ()); |
575cae23 TT |
1379 | |
1380 | if (rust_empty_enum_p (type)) | |
1381 | error (_("Cannot access field %d of empty enum %s"), | |
1382 | field_number, type->name ()); | |
1383 | ||
1384 | int fieldno = rust_enum_variant (type); | |
6c49729e | 1385 | lhs = lhs->primitive_field (0, fieldno, type); |
575cae23 | 1386 | outer_type = type; |
d0c97917 | 1387 | type = lhs->type (); |
575cae23 TT |
1388 | } |
1389 | ||
1390 | /* Tuples and tuple structs */ | |
1391 | int nfields = type->num_fields (); | |
1392 | ||
1393 | if (field_number >= nfields || field_number < 0) | |
1394 | { | |
1395 | if (outer_type != NULL) | |
1396 | error(_("Cannot access field %d of variant %s::%s, " | |
1397 | "there are only %d fields"), | |
1398 | field_number, outer_type->name (), | |
1399 | rust_last_path_segment (type->name ()), | |
1400 | nfields); | |
1401 | else | |
1402 | error(_("Cannot access field %d of %s, " | |
1403 | "there are only %d fields"), | |
1404 | field_number, type->name (), nfields); | |
1405 | } | |
1406 | ||
1407 | /* Tuples are tuple structs too. */ | |
1408 | if (!rust_tuple_struct_type_p (type)) | |
1409 | { | |
1410 | if (outer_type != NULL) | |
1411 | error(_("Variant %s::%s is not a tuple variant"), | |
1412 | outer_type->name (), | |
1413 | rust_last_path_segment (type->name ())); | |
1414 | else | |
1415 | error(_("Attempting to access anonymous field %d " | |
1416 | "of %s, which is not a tuple, tuple struct, or " | |
1417 | "tuple-like variant"), | |
1418 | field_number, type->name ()); | |
1419 | } | |
1420 | ||
6c49729e | 1421 | return lhs->primitive_field (0, field_number, type); |
575cae23 TT |
1422 | } |
1423 | else | |
1424 | error(_("Anonymous field access is only allowed on tuples, \ | |
1425 | tuple structs, and tuple-like enum variants")); | |
1426 | } | |
1427 | ||
e4407a20 | 1428 | struct value * |
f10522c0 TT |
1429 | rust_structop::evaluate (struct type *expect_type, |
1430 | struct expression *exp, | |
1431 | enum noside noside) | |
1fa41fc7 | 1432 | { |
f10522c0 TT |
1433 | value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); |
1434 | const char *field_name = std::get<1> (m_storage).c_str (); | |
1435 | ||
1fa41fc7 | 1436 | struct value *result; |
d0c97917 | 1437 | struct type *type = lhs->type (); |
1fa41fc7 TT |
1438 | if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type)) |
1439 | { | |
efaf1ae0 | 1440 | type = resolve_dynamic_type (type, lhs->contents (), |
9feb2d07 | 1441 | lhs->address ()); |
1fa41fc7 TT |
1442 | |
1443 | if (rust_empty_enum_p (type)) | |
1444 | error (_("Cannot access field %s of empty enum %s"), | |
1445 | field_name, type->name ()); | |
1446 | ||
1447 | int fieldno = rust_enum_variant (type); | |
6c49729e | 1448 | lhs = lhs->primitive_field (0, fieldno, type); |
1fa41fc7 TT |
1449 | |
1450 | struct type *outer_type = type; | |
d0c97917 | 1451 | type = lhs->type (); |
1fa41fc7 TT |
1452 | if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type)) |
1453 | error (_("Attempting to access named field %s of tuple " | |
1454 | "variant %s::%s, which has only anonymous fields"), | |
1455 | field_name, outer_type->name (), | |
1456 | rust_last_path_segment (type->name ())); | |
1457 | ||
1458 | try | |
1459 | { | |
158cc4fe | 1460 | result = value_struct_elt (&lhs, {}, field_name, |
1fa41fc7 TT |
1461 | NULL, "structure"); |
1462 | } | |
1463 | catch (const gdb_exception_error &except) | |
1464 | { | |
1465 | error (_("Could not find field %s of struct variant %s::%s"), | |
1466 | field_name, outer_type->name (), | |
1467 | rust_last_path_segment (type->name ())); | |
1468 | } | |
1469 | } | |
1470 | else | |
158cc4fe | 1471 | result = value_struct_elt (&lhs, {}, field_name, NULL, "structure"); |
1fa41fc7 | 1472 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
ee7bb294 | 1473 | result = value::zero (result->type (), VALUE_LVAL (result)); |
1fa41fc7 TT |
1474 | return result; |
1475 | } | |
1476 | ||
5947d337 TT |
1477 | value * |
1478 | rust_aggregate_operation::evaluate (struct type *expect_type, | |
1479 | struct expression *exp, | |
1480 | enum noside noside) | |
1481 | { | |
1482 | struct type *type = std::get<0> (m_storage); | |
1483 | CORE_ADDR addr = 0; | |
1484 | struct value *addrval = NULL; | |
1485 | value *result; | |
1486 | ||
1487 | if (noside == EVAL_NORMAL) | |
1488 | { | |
df86565b | 1489 | addrval = value_allocate_space_in_inferior (type->length ()); |
5947d337 TT |
1490 | addr = value_as_long (addrval); |
1491 | result = value_at_lazy (type, addr); | |
1492 | } | |
1493 | ||
1494 | if (std::get<1> (m_storage) != nullptr) | |
1495 | { | |
1496 | struct value *init = std::get<1> (m_storage)->evaluate (nullptr, exp, | |
1497 | noside); | |
1498 | ||
1499 | if (noside == EVAL_NORMAL) | |
1500 | { | |
1501 | /* This isn't quite right but will do for the time | |
1502 | being, seeing that we can't implement the Copy | |
1503 | trait anyway. */ | |
1504 | value_assign (result, init); | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | for (const auto &item : std::get<2> (m_storage)) | |
1509 | { | |
1510 | value *val = item.second->evaluate (nullptr, exp, noside); | |
1511 | if (noside == EVAL_NORMAL) | |
1512 | { | |
1513 | const char *fieldname = item.first.c_str (); | |
158cc4fe | 1514 | value *field = value_struct_elt (&result, {}, fieldname, |
5947d337 TT |
1515 | nullptr, "structure"); |
1516 | value_assign (field, val); | |
1517 | } | |
1518 | } | |
1519 | ||
aa1da9ed | 1520 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
317c3ed9 | 1521 | result = value::allocate (type); |
5947d337 TT |
1522 | else |
1523 | result = value_at_lazy (type, addr); | |
1524 | ||
1525 | return result; | |
1526 | } | |
1527 | ||
638fd74a TT |
1528 | value * |
1529 | rust_structop::evaluate_funcall (struct type *expect_type, | |
1530 | struct expression *exp, | |
1531 | enum noside noside, | |
1532 | const std::vector<operation_up> &ops) | |
1533 | { | |
1534 | std::vector<struct value *> args (ops.size () + 1); | |
1535 | ||
1536 | /* Evaluate the argument to STRUCTOP_STRUCT, then find its | |
1537 | type in order to look up the method. */ | |
1538 | args[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); | |
1539 | /* We don't yet implement real Deref semantics. */ | |
d0c97917 | 1540 | while (args[0]->type ()->code () == TYPE_CODE_PTR) |
638fd74a TT |
1541 | args[0] = value_ind (args[0]); |
1542 | ||
d0c97917 | 1543 | struct type *type = args[0]->type (); |
638fd74a TT |
1544 | if ((type->code () != TYPE_CODE_STRUCT |
1545 | && type->code () != TYPE_CODE_UNION | |
1546 | && type->code () != TYPE_CODE_ENUM) | |
1547 | || rust_tuple_type_p (type)) | |
1548 | error (_("Method calls only supported on struct or enum types")); | |
1549 | if (type->name () == NULL) | |
1550 | error (_("Method call on nameless type")); | |
1551 | ||
1552 | std::string name = (std::string (type->name ()) + "::" | |
1553 | + std::get<1> (m_storage)); | |
1554 | ||
1555 | const struct block *block = get_selected_block (0); | |
1556 | struct block_symbol sym = lookup_symbol (name.c_str (), block, | |
1557 | VAR_DOMAIN, NULL); | |
1558 | if (sym.symbol == NULL) | |
1559 | error (_("Could not find function named '%s'"), name.c_str ()); | |
1560 | ||
5f9c5a63 | 1561 | struct type *fn_type = sym.symbol->type (); |
638fd74a TT |
1562 | if (fn_type->num_fields () == 0) |
1563 | error (_("Function '%s' takes no arguments"), name.c_str ()); | |
1564 | ||
1565 | if (fn_type->field (0).type ()->code () == TYPE_CODE_PTR) | |
1566 | args[0] = value_addr (args[0]); | |
1567 | ||
1568 | value *function = address_of_variable (sym.symbol, block); | |
1569 | ||
1570 | for (int i = 0; i < ops.size (); ++i) | |
1571 | args[i + 1] = ops[i]->evaluate (nullptr, exp, noside); | |
1572 | ||
1573 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
ee7bb294 | 1574 | return value::zero (fn_type->target_type (), not_lval); |
638fd74a TT |
1575 | return call_function_by_hand (function, NULL, args); |
1576 | } | |
1577 | ||
5947d337 TT |
1578 | } |
1579 | ||
c44af4eb TT |
1580 | \f |
1581 | ||
1c485265 | 1582 | /* See language.h. */ |
0874fd07 | 1583 | |
1c485265 AB |
1584 | void |
1585 | rust_language::language_arch_info (struct gdbarch *gdbarch, | |
1586 | struct language_arch_info *lai) const | |
0874fd07 | 1587 | { |
1c485265 | 1588 | const struct builtin_type *builtin = builtin_type (gdbarch); |
87afa652 | 1589 | |
1c485265 AB |
1590 | /* Helper function to allow shorter lines below. */ |
1591 | auto add = [&] (struct type * t) -> struct type * | |
87afa652 | 1592 | { |
1c485265 AB |
1593 | lai->add_primitive_type (t); |
1594 | return t; | |
1595 | }; | |
1596 | ||
1597 | struct type *bool_type | |
1598 | = add (arch_boolean_type (gdbarch, 8, 1, "bool")); | |
1599 | add (arch_character_type (gdbarch, 32, 1, "char")); | |
1600 | add (arch_integer_type (gdbarch, 8, 0, "i8")); | |
1601 | struct type *u8_type | |
1602 | = add (arch_integer_type (gdbarch, 8, 1, "u8")); | |
1603 | add (arch_integer_type (gdbarch, 16, 0, "i16")); | |
1604 | add (arch_integer_type (gdbarch, 16, 1, "u16")); | |
1605 | add (arch_integer_type (gdbarch, 32, 0, "i32")); | |
1606 | add (arch_integer_type (gdbarch, 32, 1, "u32")); | |
1607 | add (arch_integer_type (gdbarch, 64, 0, "i64")); | |
1608 | add (arch_integer_type (gdbarch, 64, 1, "u64")); | |
1609 | ||
df86565b | 1610 | unsigned int length = 8 * builtin->builtin_data_ptr->length (); |
1c485265 AB |
1611 | add (arch_integer_type (gdbarch, length, 0, "isize")); |
1612 | struct type *usize_type | |
1613 | = add (arch_integer_type (gdbarch, length, 1, "usize")); | |
1614 | ||
1615 | add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single)); | |
1616 | add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double)); | |
1617 | add (arch_integer_type (gdbarch, 0, 1, "()")); | |
1618 | ||
1619 | struct type *tem = make_cv_type (1, 0, u8_type, NULL); | |
1620 | add (rust_slice_type ("&str", tem, usize_type)); | |
1621 | ||
1622 | lai->set_bool_type (bool_type); | |
1623 | lai->set_string_char_type (u8_type); | |
1624 | } | |
39e7ecca | 1625 | |
1c485265 | 1626 | /* See language.h. */ |
efdf6a73 | 1627 | |
1c485265 AB |
1628 | void |
1629 | rust_language::print_type (struct type *type, const char *varstring, | |
1630 | struct ui_file *stream, int show, int level, | |
1631 | const struct type_print_options *flags) const | |
1632 | { | |
fbb46296 | 1633 | print_offset_data podata (flags); |
1c485265 AB |
1634 | rust_internal_print_type (type, varstring, stream, show, level, |
1635 | flags, false, &podata); | |
1636 | } | |
efdf6a73 | 1637 | |
1c485265 | 1638 | /* See language.h. */ |
5aba6ebe | 1639 | |
1c485265 AB |
1640 | void |
1641 | rust_language::emitchar (int ch, struct type *chtype, | |
1642 | struct ui_file *stream, int quoter) const | |
1643 | { | |
1644 | if (!rust_chartype_p (chtype)) | |
1645 | generic_emit_char (ch, chtype, stream, quoter, | |
8ee511af | 1646 | target_charset (chtype->arch ())); |
1c485265 | 1647 | else if (ch == '\\' || ch == quoter) |
6cb06a8c | 1648 | gdb_printf (stream, "\\%c", ch); |
1c485265 | 1649 | else if (ch == '\n') |
0426ad51 | 1650 | gdb_puts ("\\n", stream); |
1c485265 | 1651 | else if (ch == '\r') |
0426ad51 | 1652 | gdb_puts ("\\r", stream); |
1c485265 | 1653 | else if (ch == '\t') |
0426ad51 | 1654 | gdb_puts ("\\t", stream); |
1c485265 | 1655 | else if (ch == '\0') |
0426ad51 | 1656 | gdb_puts ("\\0", stream); |
1c485265 | 1657 | else if (ch >= 32 && ch <= 127 && isprint (ch)) |
a11ac3b3 | 1658 | gdb_putc (ch, stream); |
1c485265 | 1659 | else if (ch <= 255) |
6cb06a8c | 1660 | gdb_printf (stream, "\\x%02x", ch); |
1c485265 | 1661 | else |
6cb06a8c | 1662 | gdb_printf (stream, "\\u{%06x}", ch); |
1c485265 | 1663 | } |
5aba6ebe | 1664 | |
1c485265 | 1665 | /* See language.h. */ |
b7c6e27d | 1666 | |
1c485265 AB |
1667 | bool |
1668 | rust_language::is_string_type_p (struct type *type) const | |
1669 | { | |
1670 | LONGEST low_bound, high_bound; | |
b7c6e27d | 1671 | |
1c485265 AB |
1672 | type = check_typedef (type); |
1673 | return ((type->code () == TYPE_CODE_STRING) | |
1674 | || (type->code () == TYPE_CODE_PTR | |
27710edb SM |
1675 | && (type->target_type ()->code () == TYPE_CODE_ARRAY |
1676 | && rust_u8_type_p (type->target_type ()->target_type ()) | |
1677 | && get_array_bounds (type->target_type (), &low_bound, | |
1c485265 AB |
1678 | &high_bound))) |
1679 | || (type->code () == TYPE_CODE_STRUCT | |
1680 | && !rust_enum_p (type) | |
1681 | && rust_slice_type_p (type) | |
1682 | && strcmp (type->name (), "&str") == 0)); | |
1683 | } | |
0874fd07 AB |
1684 | |
1685 | /* Single instance of the Rust language class. */ | |
1686 | ||
1687 | static rust_language rust_language_defn; |