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