| 1 | /* Bison parser for Rust expressions, for GDB. |
| 2 | Copyright (C) 2016-2018 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* Removing the last conflict seems difficult. */ |
| 20 | %expect 1 |
| 21 | |
| 22 | %{ |
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include "block.h" |
| 27 | #include "charset.h" |
| 28 | #include "cp-support.h" |
| 29 | #include "gdb_obstack.h" |
| 30 | #include "gdb_regex.h" |
| 31 | #include "rust-lang.h" |
| 32 | #include "parser-defs.h" |
| 33 | #include "selftest.h" |
| 34 | #include "value.h" |
| 35 | #include "vec.h" |
| 36 | |
| 37 | #define GDB_YY_REMAP_PREFIX rust |
| 38 | #include "yy-remap.h" |
| 39 | |
| 40 | #define RUSTSTYPE YYSTYPE |
| 41 | |
| 42 | struct rust_op; |
| 43 | typedef std::vector<const struct rust_op *> rust_op_vector; |
| 44 | |
| 45 | /* A typed integer constant. */ |
| 46 | |
| 47 | struct typed_val_int |
| 48 | { |
| 49 | LONGEST val; |
| 50 | struct type *type; |
| 51 | }; |
| 52 | |
| 53 | /* A typed floating point constant. */ |
| 54 | |
| 55 | struct typed_val_float |
| 56 | { |
| 57 | gdb_byte val[16]; |
| 58 | struct type *type; |
| 59 | }; |
| 60 | |
| 61 | /* An identifier and an expression. This is used to represent one |
| 62 | element of a struct initializer. */ |
| 63 | |
| 64 | struct set_field |
| 65 | { |
| 66 | struct stoken name; |
| 67 | const struct rust_op *init; |
| 68 | }; |
| 69 | |
| 70 | typedef std::vector<set_field> rust_set_vector; |
| 71 | |
| 72 | static int rustyylex (void); |
| 73 | static void rust_push_back (char c); |
| 74 | static const char *rust_copy_name (const char *, int); |
| 75 | static struct stoken rust_concat3 (const char *, const char *, const char *); |
| 76 | static struct stoken make_stoken (const char *); |
| 77 | static struct block_symbol rust_lookup_symbol (const char *name, |
| 78 | const struct block *block, |
| 79 | const domain_enum domain); |
| 80 | static struct type *rust_lookup_type (const char *name, |
| 81 | const struct block *block); |
| 82 | static struct type *rust_type (const char *name); |
| 83 | |
| 84 | static const struct rust_op *crate_name (const struct rust_op *name); |
| 85 | static const struct rust_op *super_name (const struct rust_op *name, |
| 86 | unsigned int n_supers); |
| 87 | |
| 88 | static const struct rust_op *ast_operation (enum exp_opcode opcode, |
| 89 | const struct rust_op *left, |
| 90 | const struct rust_op *right); |
| 91 | static const struct rust_op *ast_compound_assignment |
| 92 | (enum exp_opcode opcode, const struct rust_op *left, |
| 93 | const struct rust_op *rust_op); |
| 94 | static const struct rust_op *ast_literal (struct typed_val_int val); |
| 95 | static const struct rust_op *ast_dliteral (struct typed_val_float val); |
| 96 | static const struct rust_op *ast_structop (const struct rust_op *left, |
| 97 | const char *name, |
| 98 | int completing); |
| 99 | static const struct rust_op *ast_structop_anonymous |
| 100 | (const struct rust_op *left, struct typed_val_int number); |
| 101 | static const struct rust_op *ast_unary (enum exp_opcode opcode, |
| 102 | const struct rust_op *expr); |
| 103 | static const struct rust_op *ast_cast (const struct rust_op *expr, |
| 104 | const struct rust_op *type); |
| 105 | static const struct rust_op *ast_call_ish (enum exp_opcode opcode, |
| 106 | const struct rust_op *expr, |
| 107 | rust_op_vector *params); |
| 108 | static const struct rust_op *ast_path (struct stoken name, |
| 109 | rust_op_vector *params); |
| 110 | static const struct rust_op *ast_string (struct stoken str); |
| 111 | static const struct rust_op *ast_struct (const struct rust_op *name, |
| 112 | rust_set_vector *fields); |
| 113 | static const struct rust_op *ast_range (const struct rust_op *lhs, |
| 114 | const struct rust_op *rhs); |
| 115 | static const struct rust_op *ast_array_type (const struct rust_op *lhs, |
| 116 | struct typed_val_int val); |
| 117 | static const struct rust_op *ast_slice_type (const struct rust_op *type); |
| 118 | static const struct rust_op *ast_reference_type (const struct rust_op *type); |
| 119 | static const struct rust_op *ast_pointer_type (const struct rust_op *type, |
| 120 | int is_mut); |
| 121 | static const struct rust_op *ast_function_type (const struct rust_op *result, |
| 122 | rust_op_vector *params); |
| 123 | static const struct rust_op *ast_tuple_type (rust_op_vector *params); |
| 124 | |
| 125 | /* The current rust parser. */ |
| 126 | |
| 127 | struct rust_parser; |
| 128 | static rust_parser *current_parser; |
| 129 | |
| 130 | /* A regular expression for matching Rust numbers. This is split up |
| 131 | since it is very long and this gives us a way to comment the |
| 132 | sections. */ |
| 133 | |
| 134 | static const char *number_regex_text = |
| 135 | /* subexpression 1: allows use of alternation, otherwise uninteresting */ |
| 136 | "^(" |
| 137 | /* First comes floating point. */ |
| 138 | /* Recognize number after the decimal point, with optional |
| 139 | exponent and optional type suffix. |
| 140 | subexpression 2: allows "?", otherwise uninteresting |
| 141 | subexpression 3: if present, type suffix |
| 142 | */ |
| 143 | "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?" |
| 144 | #define FLOAT_TYPE1 3 |
| 145 | "|" |
| 146 | /* Recognize exponent without decimal point, with optional type |
| 147 | suffix. |
| 148 | subexpression 4: if present, type suffix |
| 149 | */ |
| 150 | #define FLOAT_TYPE2 4 |
| 151 | "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?" |
| 152 | "|" |
| 153 | /* "23." is a valid floating point number, but "23.e5" and |
| 154 | "23.f32" are not. So, handle the trailing-. case |
| 155 | separately. */ |
| 156 | "[0-9][0-9_]*\\." |
| 157 | "|" |
| 158 | /* Finally come integers. |
| 159 | subexpression 5: text of integer |
| 160 | subexpression 6: if present, type suffix |
| 161 | subexpression 7: allows use of alternation, otherwise uninteresting |
| 162 | */ |
| 163 | #define INT_TEXT 5 |
| 164 | #define INT_TYPE 6 |
| 165 | "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)" |
| 166 | "([iu](size|8|16|32|64))?" |
| 167 | ")"; |
| 168 | /* The number of subexpressions to allocate space for, including the |
| 169 | "0th" whole match subexpression. */ |
| 170 | #define NUM_SUBEXPRESSIONS 8 |
| 171 | |
| 172 | /* The compiled number-matching regex. */ |
| 173 | |
| 174 | static regex_t number_regex; |
| 175 | |
| 176 | /* Obstack for data temporarily allocated during parsing. Points to |
| 177 | the obstack in the rust_parser, or to a temporary obstack during |
| 178 | unit testing. */ |
| 179 | |
| 180 | static auto_obstack *work_obstack; |
| 181 | |
| 182 | /* An instance of this is created before parsing, and destroyed when |
| 183 | parsing is finished. */ |
| 184 | |
| 185 | struct rust_parser |
| 186 | { |
| 187 | rust_parser (struct parser_state *state) |
| 188 | : rust_ast (nullptr), |
| 189 | pstate (state) |
| 190 | { |
| 191 | gdb_assert (current_parser == nullptr); |
| 192 | current_parser = this; |
| 193 | work_obstack = &obstack; |
| 194 | } |
| 195 | |
| 196 | ~rust_parser () |
| 197 | { |
| 198 | /* Clean up the globals we set. */ |
| 199 | current_parser = nullptr; |
| 200 | work_obstack = nullptr; |
| 201 | } |
| 202 | |
| 203 | /* Create a new rust_set_vector. The storage for the new vector is |
| 204 | managed by this class. */ |
| 205 | rust_set_vector *new_set_vector () |
| 206 | { |
| 207 | rust_set_vector *result = new rust_set_vector; |
| 208 | set_vectors.push_back (std::unique_ptr<rust_set_vector> (result)); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | /* Create a new rust_ops_vector. The storage for the new vector is |
| 213 | managed by this class. */ |
| 214 | rust_op_vector *new_op_vector () |
| 215 | { |
| 216 | rust_op_vector *result = new rust_op_vector; |
| 217 | op_vectors.push_back (std::unique_ptr<rust_op_vector> (result)); |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | /* Return the parser's language. */ |
| 222 | const struct language_defn *language () const |
| 223 | { |
| 224 | return parse_language (pstate); |
| 225 | } |
| 226 | |
| 227 | /* Return the parser's gdbarch. */ |
| 228 | struct gdbarch *arch () const |
| 229 | { |
| 230 | return parse_gdbarch (pstate); |
| 231 | } |
| 232 | |
| 233 | /* A pointer to this is installed globally. */ |
| 234 | auto_obstack obstack; |
| 235 | |
| 236 | /* Result of parsing. Points into obstack. */ |
| 237 | const struct rust_op *rust_ast; |
| 238 | |
| 239 | /* This keeps track of the various vectors we allocate. */ |
| 240 | std::vector<std::unique_ptr<rust_set_vector>> set_vectors; |
| 241 | std::vector<std::unique_ptr<rust_op_vector>> op_vectors; |
| 242 | |
| 243 | /* The parser state gdb gave us. */ |
| 244 | struct parser_state *pstate; |
| 245 | }; |
| 246 | |
| 247 | %} |
| 248 | |
| 249 | %union |
| 250 | { |
| 251 | /* A typed integer constant. */ |
| 252 | struct typed_val_int typed_val_int; |
| 253 | |
| 254 | /* A typed floating point constant. */ |
| 255 | struct typed_val_float typed_val_float; |
| 256 | |
| 257 | /* An identifier or string. */ |
| 258 | struct stoken sval; |
| 259 | |
| 260 | /* A token representing an opcode, like "==". */ |
| 261 | enum exp_opcode opcode; |
| 262 | |
| 263 | /* A list of expressions; for example, the arguments to a function |
| 264 | call. */ |
| 265 | rust_op_vector *params; |
| 266 | |
| 267 | /* A list of field initializers. */ |
| 268 | rust_set_vector *field_inits; |
| 269 | |
| 270 | /* A single field initializer. */ |
| 271 | struct set_field one_field_init; |
| 272 | |
| 273 | /* An expression. */ |
| 274 | const struct rust_op *op; |
| 275 | |
| 276 | /* A plain integer, for example used to count the number of |
| 277 | "super::" prefixes on a path. */ |
| 278 | unsigned int depth; |
| 279 | } |
| 280 | |
| 281 | %{ |
| 282 | |
| 283 | /* Rust AST operations. We build a tree of these; then lower them |
| 284 | to gdb expressions when parsing has completed. */ |
| 285 | |
| 286 | struct rust_op |
| 287 | { |
| 288 | /* The opcode. */ |
| 289 | enum exp_opcode opcode; |
| 290 | /* If OPCODE is OP_TYPE, then this holds information about what type |
| 291 | is described by this node. */ |
| 292 | enum type_code typecode; |
| 293 | /* Indicates whether OPCODE actually represents a compound |
| 294 | assignment. For example, if OPCODE is GTGT and this is false, |
| 295 | then this rust_op represents an ordinary ">>"; but if this is |
| 296 | true, then this rust_op represents ">>=". Unused in other |
| 297 | cases. */ |
| 298 | unsigned int compound_assignment : 1; |
| 299 | /* Only used by a field expression; if set, indicates that the field |
| 300 | name occurred at the end of the expression and is eligible for |
| 301 | completion. */ |
| 302 | unsigned int completing : 1; |
| 303 | /* Operands of expression. Which one is used and how depends on the |
| 304 | particular opcode. */ |
| 305 | RUSTSTYPE left; |
| 306 | RUSTSTYPE right; |
| 307 | }; |
| 308 | |
| 309 | %} |
| 310 | |
| 311 | %token <sval> GDBVAR |
| 312 | %token <sval> IDENT |
| 313 | %token <sval> COMPLETE |
| 314 | %token <typed_val_int> INTEGER |
| 315 | %token <typed_val_int> DECIMAL_INTEGER |
| 316 | %token <sval> STRING |
| 317 | %token <sval> BYTESTRING |
| 318 | %token <typed_val_float> FLOAT |
| 319 | %token <opcode> COMPOUND_ASSIGN |
| 320 | |
| 321 | /* Keyword tokens. */ |
| 322 | %token <voidval> KW_AS |
| 323 | %token <voidval> KW_IF |
| 324 | %token <voidval> KW_TRUE |
| 325 | %token <voidval> KW_FALSE |
| 326 | %token <voidval> KW_SUPER |
| 327 | %token <voidval> KW_SELF |
| 328 | %token <voidval> KW_MUT |
| 329 | %token <voidval> KW_EXTERN |
| 330 | %token <voidval> KW_CONST |
| 331 | %token <voidval> KW_FN |
| 332 | %token <voidval> KW_SIZEOF |
| 333 | |
| 334 | /* Operator tokens. */ |
| 335 | %token <voidval> DOTDOT |
| 336 | %token <voidval> OROR |
| 337 | %token <voidval> ANDAND |
| 338 | %token <voidval> EQEQ |
| 339 | %token <voidval> NOTEQ |
| 340 | %token <voidval> LTEQ |
| 341 | %token <voidval> GTEQ |
| 342 | %token <voidval> LSH RSH |
| 343 | %token <voidval> COLONCOLON |
| 344 | %token <voidval> ARROW |
| 345 | |
| 346 | %type <op> type |
| 347 | %type <op> path_for_expr |
| 348 | %type <op> identifier_path_for_expr |
| 349 | %type <op> path_for_type |
| 350 | %type <op> identifier_path_for_type |
| 351 | %type <op> just_identifiers_for_type |
| 352 | |
| 353 | %type <params> maybe_type_list |
| 354 | %type <params> type_list |
| 355 | |
| 356 | %type <depth> super_path |
| 357 | |
| 358 | %type <op> literal |
| 359 | %type <op> expr |
| 360 | %type <op> field_expr |
| 361 | %type <op> idx_expr |
| 362 | %type <op> unop_expr |
| 363 | %type <op> binop_expr |
| 364 | %type <op> binop_expr_expr |
| 365 | %type <op> type_cast_expr |
| 366 | %type <op> assignment_expr |
| 367 | %type <op> compound_assignment_expr |
| 368 | %type <op> paren_expr |
| 369 | %type <op> call_expr |
| 370 | %type <op> path_expr |
| 371 | %type <op> tuple_expr |
| 372 | %type <op> unit_expr |
| 373 | %type <op> struct_expr |
| 374 | %type <op> array_expr |
| 375 | %type <op> range_expr |
| 376 | |
| 377 | %type <params> expr_list |
| 378 | %type <params> maybe_expr_list |
| 379 | %type <params> paren_expr_list |
| 380 | |
| 381 | %type <field_inits> struct_expr_list |
| 382 | %type <one_field_init> struct_expr_tail |
| 383 | |
| 384 | /* Precedence. */ |
| 385 | %nonassoc DOTDOT |
| 386 | %right '=' COMPOUND_ASSIGN |
| 387 | %left OROR |
| 388 | %left ANDAND |
| 389 | %nonassoc EQEQ NOTEQ '<' '>' LTEQ GTEQ |
| 390 | %left '|' |
| 391 | %left '^' |
| 392 | %left '&' |
| 393 | %left LSH RSH |
| 394 | %left '@' |
| 395 | %left '+' '-' |
| 396 | %left '*' '/' '%' |
| 397 | /* These could be %precedence in Bison, but that isn't a yacc |
| 398 | feature. */ |
| 399 | %left KW_AS |
| 400 | %left UNARY |
| 401 | %left '[' '.' '(' |
| 402 | |
| 403 | %% |
| 404 | |
| 405 | start: |
| 406 | expr |
| 407 | { |
| 408 | /* If we are completing and see a valid parse, |
| 409 | rust_ast will already have been set. */ |
| 410 | if (current_parser->rust_ast == NULL) |
| 411 | current_parser->rust_ast = $1; |
| 412 | } |
| 413 | ; |
| 414 | |
| 415 | /* Note that the Rust grammar includes a method_call_expr, but we |
| 416 | handle this differently, to avoid a shift/reduce conflict with |
| 417 | call_expr. */ |
| 418 | expr: |
| 419 | literal |
| 420 | | path_expr |
| 421 | | tuple_expr |
| 422 | | unit_expr |
| 423 | | struct_expr |
| 424 | | field_expr |
| 425 | | array_expr |
| 426 | | idx_expr |
| 427 | | range_expr |
| 428 | | unop_expr /* Must precede call_expr because of ambiguity with sizeof. */ |
| 429 | | binop_expr |
| 430 | | paren_expr |
| 431 | | call_expr |
| 432 | ; |
| 433 | |
| 434 | tuple_expr: |
| 435 | '(' expr ',' maybe_expr_list ')' |
| 436 | { |
| 437 | $4->push_back ($2); |
| 438 | error (_("Tuple expressions not supported yet")); |
| 439 | } |
| 440 | ; |
| 441 | |
| 442 | unit_expr: |
| 443 | '(' ')' |
| 444 | { |
| 445 | struct typed_val_int val; |
| 446 | |
| 447 | val.type |
| 448 | = language_lookup_primitive_type (current_parser->language (), |
| 449 | current_parser->arch (), |
| 450 | "()"); |
| 451 | val.val = 0; |
| 452 | $$ = ast_literal (val); |
| 453 | } |
| 454 | ; |
| 455 | |
| 456 | /* To avoid a shift/reduce conflict with call_expr, we don't handle |
| 457 | tuple struct expressions here, but instead when examining the |
| 458 | AST. */ |
| 459 | struct_expr: |
| 460 | path_for_expr '{' struct_expr_list '}' |
| 461 | { $$ = ast_struct ($1, $3); } |
| 462 | ; |
| 463 | |
| 464 | struct_expr_tail: |
| 465 | DOTDOT expr |
| 466 | { |
| 467 | struct set_field sf; |
| 468 | |
| 469 | sf.name.ptr = NULL; |
| 470 | sf.name.length = 0; |
| 471 | sf.init = $2; |
| 472 | |
| 473 | $$ = sf; |
| 474 | } |
| 475 | | IDENT ':' expr |
| 476 | { |
| 477 | struct set_field sf; |
| 478 | |
| 479 | sf.name = $1; |
| 480 | sf.init = $3; |
| 481 | $$ = sf; |
| 482 | } |
| 483 | ; |
| 484 | |
| 485 | struct_expr_list: |
| 486 | /* %empty */ |
| 487 | { |
| 488 | $$ = current_parser->new_set_vector (); |
| 489 | } |
| 490 | | struct_expr_tail |
| 491 | { |
| 492 | rust_set_vector *result = current_parser->new_set_vector (); |
| 493 | result->push_back ($1); |
| 494 | $$ = result; |
| 495 | } |
| 496 | | IDENT ':' expr ',' struct_expr_list |
| 497 | { |
| 498 | struct set_field sf; |
| 499 | |
| 500 | sf.name = $1; |
| 501 | sf.init = $3; |
| 502 | $5->push_back (sf); |
| 503 | $$ = $5; |
| 504 | } |
| 505 | ; |
| 506 | |
| 507 | array_expr: |
| 508 | '[' KW_MUT expr_list ']' |
| 509 | { $$ = ast_call_ish (OP_ARRAY, NULL, $3); } |
| 510 | | '[' expr_list ']' |
| 511 | { $$ = ast_call_ish (OP_ARRAY, NULL, $2); } |
| 512 | | '[' KW_MUT expr ';' expr ']' |
| 513 | { $$ = ast_operation (OP_RUST_ARRAY, $3, $5); } |
| 514 | | '[' expr ';' expr ']' |
| 515 | { $$ = ast_operation (OP_RUST_ARRAY, $2, $4); } |
| 516 | ; |
| 517 | |
| 518 | range_expr: |
| 519 | expr DOTDOT |
| 520 | { $$ = ast_range ($1, NULL); } |
| 521 | | expr DOTDOT expr |
| 522 | { $$ = ast_range ($1, $3); } |
| 523 | | DOTDOT expr |
| 524 | { $$ = ast_range (NULL, $2); } |
| 525 | | DOTDOT |
| 526 | { $$ = ast_range (NULL, NULL); } |
| 527 | ; |
| 528 | |
| 529 | literal: |
| 530 | INTEGER |
| 531 | { $$ = ast_literal ($1); } |
| 532 | | DECIMAL_INTEGER |
| 533 | { $$ = ast_literal ($1); } |
| 534 | | FLOAT |
| 535 | { $$ = ast_dliteral ($1); } |
| 536 | | STRING |
| 537 | { |
| 538 | const struct rust_op *str = ast_string ($1); |
| 539 | struct set_field field; |
| 540 | struct typed_val_int val; |
| 541 | struct stoken token; |
| 542 | |
| 543 | rust_set_vector *fields = current_parser->new_set_vector (); |
| 544 | |
| 545 | /* Wrap the raw string in the &str struct. */ |
| 546 | field.name.ptr = "data_ptr"; |
| 547 | field.name.length = strlen (field.name.ptr); |
| 548 | field.init = ast_unary (UNOP_ADDR, ast_string ($1)); |
| 549 | fields->push_back (field); |
| 550 | |
| 551 | val.type = rust_type ("usize"); |
| 552 | val.val = $1.length; |
| 553 | |
| 554 | field.name.ptr = "length"; |
| 555 | field.name.length = strlen (field.name.ptr); |
| 556 | field.init = ast_literal (val); |
| 557 | fields->push_back (field); |
| 558 | |
| 559 | token.ptr = "&str"; |
| 560 | token.length = strlen (token.ptr); |
| 561 | $$ = ast_struct (ast_path (token, NULL), fields); |
| 562 | } |
| 563 | | BYTESTRING |
| 564 | { $$ = ast_string ($1); } |
| 565 | | KW_TRUE |
| 566 | { |
| 567 | struct typed_val_int val; |
| 568 | |
| 569 | val.type = language_bool_type (current_parser->language (), |
| 570 | current_parser->arch ()); |
| 571 | val.val = 1; |
| 572 | $$ = ast_literal (val); |
| 573 | } |
| 574 | | KW_FALSE |
| 575 | { |
| 576 | struct typed_val_int val; |
| 577 | |
| 578 | val.type = language_bool_type (current_parser->language (), |
| 579 | current_parser->arch ()); |
| 580 | val.val = 0; |
| 581 | $$ = ast_literal (val); |
| 582 | } |
| 583 | ; |
| 584 | |
| 585 | field_expr: |
| 586 | expr '.' IDENT |
| 587 | { $$ = ast_structop ($1, $3.ptr, 0); } |
| 588 | | expr '.' COMPLETE |
| 589 | { |
| 590 | $$ = ast_structop ($1, $3.ptr, 1); |
| 591 | current_parser->rust_ast = $$; |
| 592 | } |
| 593 | | expr '.' DECIMAL_INTEGER |
| 594 | { $$ = ast_structop_anonymous ($1, $3); } |
| 595 | ; |
| 596 | |
| 597 | idx_expr: |
| 598 | expr '[' expr ']' |
| 599 | { $$ = ast_operation (BINOP_SUBSCRIPT, $1, $3); } |
| 600 | ; |
| 601 | |
| 602 | unop_expr: |
| 603 | '+' expr %prec UNARY |
| 604 | { $$ = ast_unary (UNOP_PLUS, $2); } |
| 605 | |
| 606 | | '-' expr %prec UNARY |
| 607 | { $$ = ast_unary (UNOP_NEG, $2); } |
| 608 | |
| 609 | | '!' expr %prec UNARY |
| 610 | { |
| 611 | /* Note that we provide a Rust-specific evaluator |
| 612 | override for UNOP_COMPLEMENT, so it can do the |
| 613 | right thing for both bool and integral |
| 614 | values. */ |
| 615 | $$ = ast_unary (UNOP_COMPLEMENT, $2); |
| 616 | } |
| 617 | |
| 618 | | '*' expr %prec UNARY |
| 619 | { $$ = ast_unary (UNOP_IND, $2); } |
| 620 | |
| 621 | | '&' expr %prec UNARY |
| 622 | { $$ = ast_unary (UNOP_ADDR, $2); } |
| 623 | |
| 624 | | '&' KW_MUT expr %prec UNARY |
| 625 | { $$ = ast_unary (UNOP_ADDR, $3); } |
| 626 | | KW_SIZEOF '(' expr ')' %prec UNARY |
| 627 | { $$ = ast_unary (UNOP_SIZEOF, $3); } |
| 628 | ; |
| 629 | |
| 630 | binop_expr: |
| 631 | binop_expr_expr |
| 632 | | type_cast_expr |
| 633 | | assignment_expr |
| 634 | | compound_assignment_expr |
| 635 | ; |
| 636 | |
| 637 | binop_expr_expr: |
| 638 | expr '*' expr |
| 639 | { $$ = ast_operation (BINOP_MUL, $1, $3); } |
| 640 | |
| 641 | | expr '@' expr |
| 642 | { $$ = ast_operation (BINOP_REPEAT, $1, $3); } |
| 643 | |
| 644 | | expr '/' expr |
| 645 | { $$ = ast_operation (BINOP_DIV, $1, $3); } |
| 646 | |
| 647 | | expr '%' expr |
| 648 | { $$ = ast_operation (BINOP_REM, $1, $3); } |
| 649 | |
| 650 | | expr '<' expr |
| 651 | { $$ = ast_operation (BINOP_LESS, $1, $3); } |
| 652 | |
| 653 | | expr '>' expr |
| 654 | { $$ = ast_operation (BINOP_GTR, $1, $3); } |
| 655 | |
| 656 | | expr '&' expr |
| 657 | { $$ = ast_operation (BINOP_BITWISE_AND, $1, $3); } |
| 658 | |
| 659 | | expr '|' expr |
| 660 | { $$ = ast_operation (BINOP_BITWISE_IOR, $1, $3); } |
| 661 | |
| 662 | | expr '^' expr |
| 663 | { $$ = ast_operation (BINOP_BITWISE_XOR, $1, $3); } |
| 664 | |
| 665 | | expr '+' expr |
| 666 | { $$ = ast_operation (BINOP_ADD, $1, $3); } |
| 667 | |
| 668 | | expr '-' expr |
| 669 | { $$ = ast_operation (BINOP_SUB, $1, $3); } |
| 670 | |
| 671 | | expr OROR expr |
| 672 | { $$ = ast_operation (BINOP_LOGICAL_OR, $1, $3); } |
| 673 | |
| 674 | | expr ANDAND expr |
| 675 | { $$ = ast_operation (BINOP_LOGICAL_AND, $1, $3); } |
| 676 | |
| 677 | | expr EQEQ expr |
| 678 | { $$ = ast_operation (BINOP_EQUAL, $1, $3); } |
| 679 | |
| 680 | | expr NOTEQ expr |
| 681 | { $$ = ast_operation (BINOP_NOTEQUAL, $1, $3); } |
| 682 | |
| 683 | | expr LTEQ expr |
| 684 | { $$ = ast_operation (BINOP_LEQ, $1, $3); } |
| 685 | |
| 686 | | expr GTEQ expr |
| 687 | { $$ = ast_operation (BINOP_GEQ, $1, $3); } |
| 688 | |
| 689 | | expr LSH expr |
| 690 | { $$ = ast_operation (BINOP_LSH, $1, $3); } |
| 691 | |
| 692 | | expr RSH expr |
| 693 | { $$ = ast_operation (BINOP_RSH, $1, $3); } |
| 694 | ; |
| 695 | |
| 696 | type_cast_expr: |
| 697 | expr KW_AS type |
| 698 | { $$ = ast_cast ($1, $3); } |
| 699 | ; |
| 700 | |
| 701 | assignment_expr: |
| 702 | expr '=' expr |
| 703 | { $$ = ast_operation (BINOP_ASSIGN, $1, $3); } |
| 704 | ; |
| 705 | |
| 706 | compound_assignment_expr: |
| 707 | expr COMPOUND_ASSIGN expr |
| 708 | { $$ = ast_compound_assignment ($2, $1, $3); } |
| 709 | |
| 710 | ; |
| 711 | |
| 712 | paren_expr: |
| 713 | '(' expr ')' |
| 714 | { $$ = $2; } |
| 715 | ; |
| 716 | |
| 717 | expr_list: |
| 718 | expr |
| 719 | { |
| 720 | $$ = current_parser->new_op_vector (); |
| 721 | $$->push_back ($1); |
| 722 | } |
| 723 | | expr_list ',' expr |
| 724 | { |
| 725 | $1->push_back ($3); |
| 726 | $$ = $1; |
| 727 | } |
| 728 | ; |
| 729 | |
| 730 | maybe_expr_list: |
| 731 | /* %empty */ |
| 732 | { |
| 733 | /* The result can't be NULL. */ |
| 734 | $$ = current_parser->new_op_vector (); |
| 735 | } |
| 736 | | expr_list |
| 737 | { $$ = $1; } |
| 738 | ; |
| 739 | |
| 740 | paren_expr_list: |
| 741 | '(' |
| 742 | maybe_expr_list |
| 743 | ')' |
| 744 | { $$ = $2; } |
| 745 | ; |
| 746 | |
| 747 | call_expr: |
| 748 | expr paren_expr_list |
| 749 | { $$ = ast_call_ish (OP_FUNCALL, $1, $2); } |
| 750 | ; |
| 751 | |
| 752 | maybe_self_path: |
| 753 | /* %empty */ |
| 754 | | KW_SELF COLONCOLON |
| 755 | ; |
| 756 | |
| 757 | super_path: |
| 758 | KW_SUPER COLONCOLON |
| 759 | { $$ = 1; } |
| 760 | | super_path KW_SUPER COLONCOLON |
| 761 | { $$ = $1 + 1; } |
| 762 | ; |
| 763 | |
| 764 | path_expr: |
| 765 | path_for_expr |
| 766 | { $$ = $1; } |
| 767 | | GDBVAR |
| 768 | { $$ = ast_path ($1, NULL); } |
| 769 | | KW_SELF |
| 770 | { $$ = ast_path (make_stoken ("self"), NULL); } |
| 771 | ; |
| 772 | |
| 773 | path_for_expr: |
| 774 | identifier_path_for_expr |
| 775 | | KW_SELF COLONCOLON identifier_path_for_expr |
| 776 | { $$ = super_name ($3, 0); } |
| 777 | | maybe_self_path super_path identifier_path_for_expr |
| 778 | { $$ = super_name ($3, $2); } |
| 779 | | COLONCOLON identifier_path_for_expr |
| 780 | { $$ = crate_name ($2); } |
| 781 | | KW_EXTERN identifier_path_for_expr |
| 782 | { |
| 783 | /* This is a gdb extension to make it possible to |
| 784 | refer to items in other crates. It just bypasses |
| 785 | adding the current crate to the front of the |
| 786 | name. */ |
| 787 | $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL), |
| 788 | $2->right.params); |
| 789 | } |
| 790 | ; |
| 791 | |
| 792 | identifier_path_for_expr: |
| 793 | IDENT |
| 794 | { $$ = ast_path ($1, NULL); } |
| 795 | | identifier_path_for_expr COLONCOLON IDENT |
| 796 | { |
| 797 | $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::", |
| 798 | $3.ptr), |
| 799 | NULL); |
| 800 | } |
| 801 | | identifier_path_for_expr COLONCOLON '<' type_list '>' |
| 802 | { $$ = ast_path ($1->left.sval, $4); } |
| 803 | | identifier_path_for_expr COLONCOLON '<' type_list RSH |
| 804 | { |
| 805 | $$ = ast_path ($1->left.sval, $4); |
| 806 | rust_push_back ('>'); |
| 807 | } |
| 808 | ; |
| 809 | |
| 810 | path_for_type: |
| 811 | identifier_path_for_type |
| 812 | | KW_SELF COLONCOLON identifier_path_for_type |
| 813 | { $$ = super_name ($3, 0); } |
| 814 | | maybe_self_path super_path identifier_path_for_type |
| 815 | { $$ = super_name ($3, $2); } |
| 816 | | COLONCOLON identifier_path_for_type |
| 817 | { $$ = crate_name ($2); } |
| 818 | | KW_EXTERN identifier_path_for_type |
| 819 | { |
| 820 | /* This is a gdb extension to make it possible to |
| 821 | refer to items in other crates. It just bypasses |
| 822 | adding the current crate to the front of the |
| 823 | name. */ |
| 824 | $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL), |
| 825 | $2->right.params); |
| 826 | } |
| 827 | ; |
| 828 | |
| 829 | just_identifiers_for_type: |
| 830 | IDENT |
| 831 | { $$ = ast_path ($1, NULL); } |
| 832 | | just_identifiers_for_type COLONCOLON IDENT |
| 833 | { |
| 834 | $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::", |
| 835 | $3.ptr), |
| 836 | NULL); |
| 837 | } |
| 838 | ; |
| 839 | |
| 840 | identifier_path_for_type: |
| 841 | just_identifiers_for_type |
| 842 | | just_identifiers_for_type '<' type_list '>' |
| 843 | { $$ = ast_path ($1->left.sval, $3); } |
| 844 | | just_identifiers_for_type '<' type_list RSH |
| 845 | { |
| 846 | $$ = ast_path ($1->left.sval, $3); |
| 847 | rust_push_back ('>'); |
| 848 | } |
| 849 | ; |
| 850 | |
| 851 | type: |
| 852 | path_for_type |
| 853 | | '[' type ';' INTEGER ']' |
| 854 | { $$ = ast_array_type ($2, $4); } |
| 855 | | '[' type ';' DECIMAL_INTEGER ']' |
| 856 | { $$ = ast_array_type ($2, $4); } |
| 857 | | '&' '[' type ']' |
| 858 | { $$ = ast_slice_type ($3); } |
| 859 | | '&' type |
| 860 | { $$ = ast_reference_type ($2); } |
| 861 | | '*' KW_MUT type |
| 862 | { $$ = ast_pointer_type ($3, 1); } |
| 863 | | '*' KW_CONST type |
| 864 | { $$ = ast_pointer_type ($3, 0); } |
| 865 | | KW_FN '(' maybe_type_list ')' ARROW type |
| 866 | { $$ = ast_function_type ($6, $3); } |
| 867 | | '(' maybe_type_list ')' |
| 868 | { $$ = ast_tuple_type ($2); } |
| 869 | ; |
| 870 | |
| 871 | maybe_type_list: |
| 872 | /* %empty */ |
| 873 | { $$ = NULL; } |
| 874 | | type_list |
| 875 | { $$ = $1; } |
| 876 | ; |
| 877 | |
| 878 | type_list: |
| 879 | type |
| 880 | { |
| 881 | rust_op_vector *result = current_parser->new_op_vector (); |
| 882 | result->push_back ($1); |
| 883 | $$ = result; |
| 884 | } |
| 885 | | type_list ',' type |
| 886 | { |
| 887 | $1->push_back ($3); |
| 888 | $$ = $1; |
| 889 | } |
| 890 | ; |
| 891 | |
| 892 | %% |
| 893 | |
| 894 | /* A struct of this type is used to describe a token. */ |
| 895 | |
| 896 | struct token_info |
| 897 | { |
| 898 | const char *name; |
| 899 | int value; |
| 900 | enum exp_opcode opcode; |
| 901 | }; |
| 902 | |
| 903 | /* Identifier tokens. */ |
| 904 | |
| 905 | static const struct token_info identifier_tokens[] = |
| 906 | { |
| 907 | { "as", KW_AS, OP_NULL }, |
| 908 | { "false", KW_FALSE, OP_NULL }, |
| 909 | { "if", 0, OP_NULL }, |
| 910 | { "mut", KW_MUT, OP_NULL }, |
| 911 | { "const", KW_CONST, OP_NULL }, |
| 912 | { "self", KW_SELF, OP_NULL }, |
| 913 | { "super", KW_SUPER, OP_NULL }, |
| 914 | { "true", KW_TRUE, OP_NULL }, |
| 915 | { "extern", KW_EXTERN, OP_NULL }, |
| 916 | { "fn", KW_FN, OP_NULL }, |
| 917 | { "sizeof", KW_SIZEOF, OP_NULL }, |
| 918 | }; |
| 919 | |
| 920 | /* Operator tokens, sorted longest first. */ |
| 921 | |
| 922 | static const struct token_info operator_tokens[] = |
| 923 | { |
| 924 | { ">>=", COMPOUND_ASSIGN, BINOP_RSH }, |
| 925 | { "<<=", COMPOUND_ASSIGN, BINOP_LSH }, |
| 926 | |
| 927 | { "<<", LSH, OP_NULL }, |
| 928 | { ">>", RSH, OP_NULL }, |
| 929 | { "&&", ANDAND, OP_NULL }, |
| 930 | { "||", OROR, OP_NULL }, |
| 931 | { "==", EQEQ, OP_NULL }, |
| 932 | { "!=", NOTEQ, OP_NULL }, |
| 933 | { "<=", LTEQ, OP_NULL }, |
| 934 | { ">=", GTEQ, OP_NULL }, |
| 935 | { "+=", COMPOUND_ASSIGN, BINOP_ADD }, |
| 936 | { "-=", COMPOUND_ASSIGN, BINOP_SUB }, |
| 937 | { "*=", COMPOUND_ASSIGN, BINOP_MUL }, |
| 938 | { "/=", COMPOUND_ASSIGN, BINOP_DIV }, |
| 939 | { "%=", COMPOUND_ASSIGN, BINOP_REM }, |
| 940 | { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND }, |
| 941 | { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR }, |
| 942 | { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR }, |
| 943 | |
| 944 | { "::", COLONCOLON, OP_NULL }, |
| 945 | { "..", DOTDOT, OP_NULL }, |
| 946 | { "->", ARROW, OP_NULL } |
| 947 | }; |
| 948 | |
| 949 | /* Helper function to copy to the name obstack. */ |
| 950 | |
| 951 | static const char * |
| 952 | rust_copy_name (const char *name, int len) |
| 953 | { |
| 954 | return (const char *) obstack_copy0 (work_obstack, name, len); |
| 955 | } |
| 956 | |
| 957 | /* Helper function to make an stoken from a C string. */ |
| 958 | |
| 959 | static struct stoken |
| 960 | make_stoken (const char *p) |
| 961 | { |
| 962 | struct stoken result; |
| 963 | |
| 964 | result.ptr = p; |
| 965 | result.length = strlen (result.ptr); |
| 966 | return result; |
| 967 | } |
| 968 | |
| 969 | /* Helper function to concatenate three strings on the name |
| 970 | obstack. */ |
| 971 | |
| 972 | static struct stoken |
| 973 | rust_concat3 (const char *s1, const char *s2, const char *s3) |
| 974 | { |
| 975 | return make_stoken (obconcat (work_obstack, s1, s2, s3, (char *) NULL)); |
| 976 | } |
| 977 | |
| 978 | /* Return an AST node referring to NAME, but relative to the crate's |
| 979 | name. */ |
| 980 | |
| 981 | static const struct rust_op * |
| 982 | crate_name (const struct rust_op *name) |
| 983 | { |
| 984 | std::string crate = rust_crate_for_block (expression_context_block); |
| 985 | struct stoken result; |
| 986 | |
| 987 | gdb_assert (name->opcode == OP_VAR_VALUE); |
| 988 | |
| 989 | if (crate.empty ()) |
| 990 | error (_("Could not find crate for current location")); |
| 991 | result = make_stoken (obconcat (work_obstack, "::", crate.c_str (), "::", |
| 992 | name->left.sval.ptr, (char *) NULL)); |
| 993 | |
| 994 | return ast_path (result, name->right.params); |
| 995 | } |
| 996 | |
| 997 | /* Create an AST node referring to a "super::" qualified name. IDENT |
| 998 | is the base name and N_SUPERS is how many "super::"s were |
| 999 | provided. N_SUPERS can be zero. */ |
| 1000 | |
| 1001 | static const struct rust_op * |
| 1002 | super_name (const struct rust_op *ident, unsigned int n_supers) |
| 1003 | { |
| 1004 | const char *scope = block_scope (expression_context_block); |
| 1005 | int offset; |
| 1006 | |
| 1007 | gdb_assert (ident->opcode == OP_VAR_VALUE); |
| 1008 | |
| 1009 | if (scope[0] == '\0') |
| 1010 | error (_("Couldn't find namespace scope for self::")); |
| 1011 | |
| 1012 | if (n_supers > 0) |
| 1013 | { |
| 1014 | int len; |
| 1015 | std::vector<int> offsets; |
| 1016 | unsigned int current_len; |
| 1017 | |
| 1018 | current_len = cp_find_first_component (scope); |
| 1019 | while (scope[current_len] != '\0') |
| 1020 | { |
| 1021 | offsets.push_back (current_len); |
| 1022 | gdb_assert (scope[current_len] == ':'); |
| 1023 | /* The "::". */ |
| 1024 | current_len += 2; |
| 1025 | current_len += cp_find_first_component (scope |
| 1026 | + current_len); |
| 1027 | } |
| 1028 | |
| 1029 | len = offsets.size (); |
| 1030 | if (n_supers >= len) |
| 1031 | error (_("Too many super:: uses from '%s'"), scope); |
| 1032 | |
| 1033 | offset = offsets[len - n_supers]; |
| 1034 | } |
| 1035 | else |
| 1036 | offset = strlen (scope); |
| 1037 | |
| 1038 | obstack_grow (work_obstack, "::", 2); |
| 1039 | obstack_grow (work_obstack, scope, offset); |
| 1040 | obstack_grow (work_obstack, "::", 2); |
| 1041 | obstack_grow0 (work_obstack, ident->left.sval.ptr, ident->left.sval.length); |
| 1042 | |
| 1043 | return ast_path (make_stoken ((const char *) obstack_finish (work_obstack)), |
| 1044 | ident->right.params); |
| 1045 | } |
| 1046 | |
| 1047 | /* A helper that updates the innermost block as appropriate. */ |
| 1048 | |
| 1049 | static void |
| 1050 | update_innermost_block (struct block_symbol sym) |
| 1051 | { |
| 1052 | if (symbol_read_needs_frame (sym.symbol)) |
| 1053 | innermost_block.update (sym); |
| 1054 | } |
| 1055 | |
| 1056 | /* A helper to look up a Rust type, or fail. This only works for |
| 1057 | types defined by rust_language_arch_info. */ |
| 1058 | |
| 1059 | static struct type * |
| 1060 | rust_type (const char *name) |
| 1061 | { |
| 1062 | struct type *type; |
| 1063 | |
| 1064 | type = language_lookup_primitive_type (current_parser->language (), |
| 1065 | current_parser->arch (), |
| 1066 | name); |
| 1067 | if (type == NULL) |
| 1068 | error (_("Could not find Rust type %s"), name); |
| 1069 | return type; |
| 1070 | } |
| 1071 | |
| 1072 | /* Lex a hex number with at least MIN digits and at most MAX |
| 1073 | digits. */ |
| 1074 | |
| 1075 | static uint32_t |
| 1076 | lex_hex (int min, int max) |
| 1077 | { |
| 1078 | uint32_t result = 0; |
| 1079 | int len = 0; |
| 1080 | /* We only want to stop at MAX if we're lexing a byte escape. */ |
| 1081 | int check_max = min == max; |
| 1082 | |
| 1083 | while ((check_max ? len <= max : 1) |
| 1084 | && ((lexptr[0] >= 'a' && lexptr[0] <= 'f') |
| 1085 | || (lexptr[0] >= 'A' && lexptr[0] <= 'F') |
| 1086 | || (lexptr[0] >= '0' && lexptr[0] <= '9'))) |
| 1087 | { |
| 1088 | result *= 16; |
| 1089 | if (lexptr[0] >= 'a' && lexptr[0] <= 'f') |
| 1090 | result = result + 10 + lexptr[0] - 'a'; |
| 1091 | else if (lexptr[0] >= 'A' && lexptr[0] <= 'F') |
| 1092 | result = result + 10 + lexptr[0] - 'A'; |
| 1093 | else |
| 1094 | result = result + lexptr[0] - '0'; |
| 1095 | ++lexptr; |
| 1096 | ++len; |
| 1097 | } |
| 1098 | |
| 1099 | if (len < min) |
| 1100 | error (_("Not enough hex digits seen")); |
| 1101 | if (len > max) |
| 1102 | { |
| 1103 | gdb_assert (min != max); |
| 1104 | error (_("Overlong hex escape")); |
| 1105 | } |
| 1106 | |
| 1107 | return result; |
| 1108 | } |
| 1109 | |
| 1110 | /* Lex an escape. IS_BYTE is true if we're lexing a byte escape; |
| 1111 | otherwise we're lexing a character escape. */ |
| 1112 | |
| 1113 | static uint32_t |
| 1114 | lex_escape (int is_byte) |
| 1115 | { |
| 1116 | uint32_t result; |
| 1117 | |
| 1118 | gdb_assert (lexptr[0] == '\\'); |
| 1119 | ++lexptr; |
| 1120 | switch (lexptr[0]) |
| 1121 | { |
| 1122 | case 'x': |
| 1123 | ++lexptr; |
| 1124 | result = lex_hex (2, 2); |
| 1125 | break; |
| 1126 | |
| 1127 | case 'u': |
| 1128 | if (is_byte) |
| 1129 | error (_("Unicode escape in byte literal")); |
| 1130 | ++lexptr; |
| 1131 | if (lexptr[0] != '{') |
| 1132 | error (_("Missing '{' in Unicode escape")); |
| 1133 | ++lexptr; |
| 1134 | result = lex_hex (1, 6); |
| 1135 | /* Could do range checks here. */ |
| 1136 | if (lexptr[0] != '}') |
| 1137 | error (_("Missing '}' in Unicode escape")); |
| 1138 | ++lexptr; |
| 1139 | break; |
| 1140 | |
| 1141 | case 'n': |
| 1142 | result = '\n'; |
| 1143 | ++lexptr; |
| 1144 | break; |
| 1145 | case 'r': |
| 1146 | result = '\r'; |
| 1147 | ++lexptr; |
| 1148 | break; |
| 1149 | case 't': |
| 1150 | result = '\t'; |
| 1151 | ++lexptr; |
| 1152 | break; |
| 1153 | case '\\': |
| 1154 | result = '\\'; |
| 1155 | ++lexptr; |
| 1156 | break; |
| 1157 | case '0': |
| 1158 | result = '\0'; |
| 1159 | ++lexptr; |
| 1160 | break; |
| 1161 | case '\'': |
| 1162 | result = '\''; |
| 1163 | ++lexptr; |
| 1164 | break; |
| 1165 | case '"': |
| 1166 | result = '"'; |
| 1167 | ++lexptr; |
| 1168 | break; |
| 1169 | |
| 1170 | default: |
| 1171 | error (_("Invalid escape \\%c in literal"), lexptr[0]); |
| 1172 | } |
| 1173 | |
| 1174 | return result; |
| 1175 | } |
| 1176 | |
| 1177 | /* Lex a character constant. */ |
| 1178 | |
| 1179 | static int |
| 1180 | lex_character (void) |
| 1181 | { |
| 1182 | int is_byte = 0; |
| 1183 | uint32_t value; |
| 1184 | |
| 1185 | if (lexptr[0] == 'b') |
| 1186 | { |
| 1187 | is_byte = 1; |
| 1188 | ++lexptr; |
| 1189 | } |
| 1190 | gdb_assert (lexptr[0] == '\''); |
| 1191 | ++lexptr; |
| 1192 | /* This should handle UTF-8 here. */ |
| 1193 | if (lexptr[0] == '\\') |
| 1194 | value = lex_escape (is_byte); |
| 1195 | else |
| 1196 | { |
| 1197 | value = lexptr[0] & 0xff; |
| 1198 | ++lexptr; |
| 1199 | } |
| 1200 | |
| 1201 | if (lexptr[0] != '\'') |
| 1202 | error (_("Unterminated character literal")); |
| 1203 | ++lexptr; |
| 1204 | |
| 1205 | rustyylval.typed_val_int.val = value; |
| 1206 | rustyylval.typed_val_int.type = rust_type (is_byte ? "u8" : "char"); |
| 1207 | |
| 1208 | return INTEGER; |
| 1209 | } |
| 1210 | |
| 1211 | /* Return the offset of the double quote if STR looks like the start |
| 1212 | of a raw string, or 0 if STR does not start a raw string. */ |
| 1213 | |
| 1214 | static int |
| 1215 | starts_raw_string (const char *str) |
| 1216 | { |
| 1217 | const char *save = str; |
| 1218 | |
| 1219 | if (str[0] != 'r') |
| 1220 | return 0; |
| 1221 | ++str; |
| 1222 | while (str[0] == '#') |
| 1223 | ++str; |
| 1224 | if (str[0] == '"') |
| 1225 | return str - save; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* Return true if STR looks like the end of a raw string that had N |
| 1230 | hashes at the start. */ |
| 1231 | |
| 1232 | static bool |
| 1233 | ends_raw_string (const char *str, int n) |
| 1234 | { |
| 1235 | int i; |
| 1236 | |
| 1237 | gdb_assert (str[0] == '"'); |
| 1238 | for (i = 0; i < n; ++i) |
| 1239 | if (str[i + 1] != '#') |
| 1240 | return false; |
| 1241 | return true; |
| 1242 | } |
| 1243 | |
| 1244 | /* Lex a string constant. */ |
| 1245 | |
| 1246 | static int |
| 1247 | lex_string (void) |
| 1248 | { |
| 1249 | int is_byte = lexptr[0] == 'b'; |
| 1250 | int raw_length; |
| 1251 | |
| 1252 | if (is_byte) |
| 1253 | ++lexptr; |
| 1254 | raw_length = starts_raw_string (lexptr); |
| 1255 | lexptr += raw_length; |
| 1256 | gdb_assert (lexptr[0] == '"'); |
| 1257 | ++lexptr; |
| 1258 | |
| 1259 | while (1) |
| 1260 | { |
| 1261 | uint32_t value; |
| 1262 | |
| 1263 | if (raw_length > 0) |
| 1264 | { |
| 1265 | if (lexptr[0] == '"' && ends_raw_string (lexptr, raw_length - 1)) |
| 1266 | { |
| 1267 | /* Exit with lexptr pointing after the final "#". */ |
| 1268 | lexptr += raw_length; |
| 1269 | break; |
| 1270 | } |
| 1271 | else if (lexptr[0] == '\0') |
| 1272 | error (_("Unexpected EOF in string")); |
| 1273 | |
| 1274 | value = lexptr[0] & 0xff; |
| 1275 | if (is_byte && value > 127) |
| 1276 | error (_("Non-ASCII value in raw byte string")); |
| 1277 | obstack_1grow (work_obstack, value); |
| 1278 | |
| 1279 | ++lexptr; |
| 1280 | } |
| 1281 | else if (lexptr[0] == '"') |
| 1282 | { |
| 1283 | /* Make sure to skip the quote. */ |
| 1284 | ++lexptr; |
| 1285 | break; |
| 1286 | } |
| 1287 | else if (lexptr[0] == '\\') |
| 1288 | { |
| 1289 | value = lex_escape (is_byte); |
| 1290 | |
| 1291 | if (is_byte) |
| 1292 | obstack_1grow (work_obstack, value); |
| 1293 | else |
| 1294 | convert_between_encodings ("UTF-32", "UTF-8", (gdb_byte *) &value, |
| 1295 | sizeof (value), sizeof (value), |
| 1296 | work_obstack, translit_none); |
| 1297 | } |
| 1298 | else if (lexptr[0] == '\0') |
| 1299 | error (_("Unexpected EOF in string")); |
| 1300 | else |
| 1301 | { |
| 1302 | value = lexptr[0] & 0xff; |
| 1303 | if (is_byte && value > 127) |
| 1304 | error (_("Non-ASCII value in byte string")); |
| 1305 | obstack_1grow (work_obstack, value); |
| 1306 | ++lexptr; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | rustyylval.sval.length = obstack_object_size (work_obstack); |
| 1311 | rustyylval.sval.ptr = (const char *) obstack_finish (work_obstack); |
| 1312 | return is_byte ? BYTESTRING : STRING; |
| 1313 | } |
| 1314 | |
| 1315 | /* Return true if STRING starts with whitespace followed by a digit. */ |
| 1316 | |
| 1317 | static bool |
| 1318 | space_then_number (const char *string) |
| 1319 | { |
| 1320 | const char *p = string; |
| 1321 | |
| 1322 | while (p[0] == ' ' || p[0] == '\t') |
| 1323 | ++p; |
| 1324 | if (p == string) |
| 1325 | return false; |
| 1326 | |
| 1327 | return *p >= '0' && *p <= '9'; |
| 1328 | } |
| 1329 | |
| 1330 | /* Return true if C can start an identifier. */ |
| 1331 | |
| 1332 | static bool |
| 1333 | rust_identifier_start_p (char c) |
| 1334 | { |
| 1335 | return ((c >= 'a' && c <= 'z') |
| 1336 | || (c >= 'A' && c <= 'Z') |
| 1337 | || c == '_' |
| 1338 | || c == '$'); |
| 1339 | } |
| 1340 | |
| 1341 | /* Lex an identifier. */ |
| 1342 | |
| 1343 | static int |
| 1344 | lex_identifier (void) |
| 1345 | { |
| 1346 | const char *start = lexptr; |
| 1347 | unsigned int length; |
| 1348 | const struct token_info *token; |
| 1349 | int i; |
| 1350 | int is_gdb_var = lexptr[0] == '$'; |
| 1351 | |
| 1352 | gdb_assert (rust_identifier_start_p (lexptr[0])); |
| 1353 | |
| 1354 | ++lexptr; |
| 1355 | |
| 1356 | /* For the time being this doesn't handle Unicode rules. Non-ASCII |
| 1357 | identifiers are gated anyway. */ |
| 1358 | while ((lexptr[0] >= 'a' && lexptr[0] <= 'z') |
| 1359 | || (lexptr[0] >= 'A' && lexptr[0] <= 'Z') |
| 1360 | || lexptr[0] == '_' |
| 1361 | || (is_gdb_var && lexptr[0] == '$') |
| 1362 | || (lexptr[0] >= '0' && lexptr[0] <= '9')) |
| 1363 | ++lexptr; |
| 1364 | |
| 1365 | |
| 1366 | length = lexptr - start; |
| 1367 | token = NULL; |
| 1368 | for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i) |
| 1369 | { |
| 1370 | if (length == strlen (identifier_tokens[i].name) |
| 1371 | && strncmp (identifier_tokens[i].name, start, length) == 0) |
| 1372 | { |
| 1373 | token = &identifier_tokens[i]; |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | if (token != NULL) |
| 1379 | { |
| 1380 | if (token->value == 0) |
| 1381 | { |
| 1382 | /* Leave the terminating token alone. */ |
| 1383 | lexptr = start; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | } |
| 1387 | else if (token == NULL |
| 1388 | && (strncmp (start, "thread", length) == 0 |
| 1389 | || strncmp (start, "task", length) == 0) |
| 1390 | && space_then_number (lexptr)) |
| 1391 | { |
| 1392 | /* "task" or "thread" followed by a number terminates the |
| 1393 | parse, per gdb rules. */ |
| 1394 | lexptr = start; |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | if (token == NULL || (parse_completion && lexptr[0] == '\0')) |
| 1399 | rustyylval.sval = make_stoken (rust_copy_name (start, length)); |
| 1400 | |
| 1401 | if (parse_completion && lexptr[0] == '\0') |
| 1402 | { |
| 1403 | /* Prevent rustyylex from returning two COMPLETE tokens. */ |
| 1404 | prev_lexptr = lexptr; |
| 1405 | return COMPLETE; |
| 1406 | } |
| 1407 | |
| 1408 | if (token != NULL) |
| 1409 | return token->value; |
| 1410 | if (is_gdb_var) |
| 1411 | return GDBVAR; |
| 1412 | return IDENT; |
| 1413 | } |
| 1414 | |
| 1415 | /* Lex an operator. */ |
| 1416 | |
| 1417 | static int |
| 1418 | lex_operator (void) |
| 1419 | { |
| 1420 | const struct token_info *token = NULL; |
| 1421 | int i; |
| 1422 | |
| 1423 | for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i) |
| 1424 | { |
| 1425 | if (strncmp (operator_tokens[i].name, lexptr, |
| 1426 | strlen (operator_tokens[i].name)) == 0) |
| 1427 | { |
| 1428 | lexptr += strlen (operator_tokens[i].name); |
| 1429 | token = &operator_tokens[i]; |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (token != NULL) |
| 1435 | { |
| 1436 | rustyylval.opcode = token->opcode; |
| 1437 | return token->value; |
| 1438 | } |
| 1439 | |
| 1440 | return *lexptr++; |
| 1441 | } |
| 1442 | |
| 1443 | /* Lex a number. */ |
| 1444 | |
| 1445 | static int |
| 1446 | lex_number (void) |
| 1447 | { |
| 1448 | regmatch_t subexps[NUM_SUBEXPRESSIONS]; |
| 1449 | int match; |
| 1450 | int is_integer = 0; |
| 1451 | int could_be_decimal = 1; |
| 1452 | int implicit_i32 = 0; |
| 1453 | const char *type_name = NULL; |
| 1454 | struct type *type; |
| 1455 | int end_index; |
| 1456 | int type_index = -1; |
| 1457 | int i; |
| 1458 | |
| 1459 | match = regexec (&number_regex, lexptr, ARRAY_SIZE (subexps), subexps, 0); |
| 1460 | /* Failure means the regexp is broken. */ |
| 1461 | gdb_assert (match == 0); |
| 1462 | |
| 1463 | if (subexps[INT_TEXT].rm_so != -1) |
| 1464 | { |
| 1465 | /* Integer part matched. */ |
| 1466 | is_integer = 1; |
| 1467 | end_index = subexps[INT_TEXT].rm_eo; |
| 1468 | if (subexps[INT_TYPE].rm_so == -1) |
| 1469 | { |
| 1470 | type_name = "i32"; |
| 1471 | implicit_i32 = 1; |
| 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | type_index = INT_TYPE; |
| 1476 | could_be_decimal = 0; |
| 1477 | } |
| 1478 | } |
| 1479 | else if (subexps[FLOAT_TYPE1].rm_so != -1) |
| 1480 | { |
| 1481 | /* Found floating point type suffix. */ |
| 1482 | end_index = subexps[FLOAT_TYPE1].rm_so; |
| 1483 | type_index = FLOAT_TYPE1; |
| 1484 | } |
| 1485 | else if (subexps[FLOAT_TYPE2].rm_so != -1) |
| 1486 | { |
| 1487 | /* Found floating point type suffix. */ |
| 1488 | end_index = subexps[FLOAT_TYPE2].rm_so; |
| 1489 | type_index = FLOAT_TYPE2; |
| 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | /* Any other floating point match. */ |
| 1494 | end_index = subexps[0].rm_eo; |
| 1495 | type_name = "f64"; |
| 1496 | } |
| 1497 | |
| 1498 | /* We need a special case if the final character is ".". In this |
| 1499 | case we might need to parse an integer. For example, "23.f()" is |
| 1500 | a request for a trait method call, not a syntax error involving |
| 1501 | the floating point number "23.". */ |
| 1502 | gdb_assert (subexps[0].rm_eo > 0); |
| 1503 | if (lexptr[subexps[0].rm_eo - 1] == '.') |
| 1504 | { |
| 1505 | const char *next = skip_spaces (&lexptr[subexps[0].rm_eo]); |
| 1506 | |
| 1507 | if (rust_identifier_start_p (*next) || *next == '.') |
| 1508 | { |
| 1509 | --subexps[0].rm_eo; |
| 1510 | is_integer = 1; |
| 1511 | end_index = subexps[0].rm_eo; |
| 1512 | type_name = "i32"; |
| 1513 | could_be_decimal = 1; |
| 1514 | implicit_i32 = 1; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | /* Compute the type name if we haven't already. */ |
| 1519 | std::string type_name_holder; |
| 1520 | if (type_name == NULL) |
| 1521 | { |
| 1522 | gdb_assert (type_index != -1); |
| 1523 | type_name_holder = std::string (lexptr + subexps[type_index].rm_so, |
| 1524 | (subexps[type_index].rm_eo |
| 1525 | - subexps[type_index].rm_so)); |
| 1526 | type_name = type_name_holder.c_str (); |
| 1527 | } |
| 1528 | |
| 1529 | /* Look up the type. */ |
| 1530 | type = rust_type (type_name); |
| 1531 | |
| 1532 | /* Copy the text of the number and remove the "_"s. */ |
| 1533 | std::string number; |
| 1534 | for (i = 0; i < end_index && lexptr[i]; ++i) |
| 1535 | { |
| 1536 | if (lexptr[i] == '_') |
| 1537 | could_be_decimal = 0; |
| 1538 | else |
| 1539 | number.push_back (lexptr[i]); |
| 1540 | } |
| 1541 | |
| 1542 | /* Advance past the match. */ |
| 1543 | lexptr += subexps[0].rm_eo; |
| 1544 | |
| 1545 | /* Parse the number. */ |
| 1546 | if (is_integer) |
| 1547 | { |
| 1548 | uint64_t value; |
| 1549 | int radix = 10; |
| 1550 | int offset = 0; |
| 1551 | |
| 1552 | if (number[0] == '0') |
| 1553 | { |
| 1554 | if (number[1] == 'x') |
| 1555 | radix = 16; |
| 1556 | else if (number[1] == 'o') |
| 1557 | radix = 8; |
| 1558 | else if (number[1] == 'b') |
| 1559 | radix = 2; |
| 1560 | if (radix != 10) |
| 1561 | { |
| 1562 | offset = 2; |
| 1563 | could_be_decimal = 0; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | value = strtoul (number.c_str () + offset, NULL, radix); |
| 1568 | if (implicit_i32 && value >= ((uint64_t) 1) << 31) |
| 1569 | type = rust_type ("i64"); |
| 1570 | |
| 1571 | rustyylval.typed_val_int.val = value; |
| 1572 | rustyylval.typed_val_int.type = type; |
| 1573 | } |
| 1574 | else |
| 1575 | { |
| 1576 | rustyylval.typed_val_float.type = type; |
| 1577 | bool parsed = parse_float (number.c_str (), number.length (), |
| 1578 | rustyylval.typed_val_float.type, |
| 1579 | rustyylval.typed_val_float.val); |
| 1580 | gdb_assert (parsed); |
| 1581 | } |
| 1582 | |
| 1583 | return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT; |
| 1584 | } |
| 1585 | |
| 1586 | /* The lexer. */ |
| 1587 | |
| 1588 | static int |
| 1589 | rustyylex (void) |
| 1590 | { |
| 1591 | /* Skip all leading whitespace. */ |
| 1592 | while (lexptr[0] == ' ' || lexptr[0] == '\t' || lexptr[0] == '\r' |
| 1593 | || lexptr[0] == '\n') |
| 1594 | ++lexptr; |
| 1595 | |
| 1596 | /* If we hit EOF and we're completing, then return COMPLETE -- maybe |
| 1597 | we're completing an empty string at the end of a field_expr. |
| 1598 | But, we don't want to return two COMPLETE tokens in a row. */ |
| 1599 | if (lexptr[0] == '\0' && lexptr == prev_lexptr) |
| 1600 | return 0; |
| 1601 | prev_lexptr = lexptr; |
| 1602 | if (lexptr[0] == '\0') |
| 1603 | { |
| 1604 | if (parse_completion) |
| 1605 | { |
| 1606 | rustyylval.sval = make_stoken (""); |
| 1607 | return COMPLETE; |
| 1608 | } |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | if (lexptr[0] >= '0' && lexptr[0] <= '9') |
| 1613 | return lex_number (); |
| 1614 | else if (lexptr[0] == 'b' && lexptr[1] == '\'') |
| 1615 | return lex_character (); |
| 1616 | else if (lexptr[0] == 'b' && lexptr[1] == '"') |
| 1617 | return lex_string (); |
| 1618 | else if (lexptr[0] == 'b' && starts_raw_string (lexptr + 1)) |
| 1619 | return lex_string (); |
| 1620 | else if (starts_raw_string (lexptr)) |
| 1621 | return lex_string (); |
| 1622 | else if (rust_identifier_start_p (lexptr[0])) |
| 1623 | return lex_identifier (); |
| 1624 | else if (lexptr[0] == '"') |
| 1625 | return lex_string (); |
| 1626 | else if (lexptr[0] == '\'') |
| 1627 | return lex_character (); |
| 1628 | else if (lexptr[0] == '}' || lexptr[0] == ']') |
| 1629 | { |
| 1630 | /* Falls through to lex_operator. */ |
| 1631 | --paren_depth; |
| 1632 | } |
| 1633 | else if (lexptr[0] == '(' || lexptr[0] == '{') |
| 1634 | { |
| 1635 | /* Falls through to lex_operator. */ |
| 1636 | ++paren_depth; |
| 1637 | } |
| 1638 | else if (lexptr[0] == ',' && comma_terminates && paren_depth == 0) |
| 1639 | return 0; |
| 1640 | |
| 1641 | return lex_operator (); |
| 1642 | } |
| 1643 | |
| 1644 | /* Push back a single character to be re-lexed. */ |
| 1645 | |
| 1646 | static void |
| 1647 | rust_push_back (char c) |
| 1648 | { |
| 1649 | /* Can't be called before any lexing. */ |
| 1650 | gdb_assert (prev_lexptr != NULL); |
| 1651 | |
| 1652 | --lexptr; |
| 1653 | gdb_assert (*lexptr == c); |
| 1654 | } |
| 1655 | |
| 1656 | \f |
| 1657 | |
| 1658 | /* Make an arbitrary operation and fill in the fields. */ |
| 1659 | |
| 1660 | static const struct rust_op * |
| 1661 | ast_operation (enum exp_opcode opcode, const struct rust_op *left, |
| 1662 | const struct rust_op *right) |
| 1663 | { |
| 1664 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1665 | |
| 1666 | result->opcode = opcode; |
| 1667 | result->left.op = left; |
| 1668 | result->right.op = right; |
| 1669 | |
| 1670 | return result; |
| 1671 | } |
| 1672 | |
| 1673 | /* Make a compound assignment operation. */ |
| 1674 | |
| 1675 | static const struct rust_op * |
| 1676 | ast_compound_assignment (enum exp_opcode opcode, const struct rust_op *left, |
| 1677 | const struct rust_op *right) |
| 1678 | { |
| 1679 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1680 | |
| 1681 | result->opcode = opcode; |
| 1682 | result->compound_assignment = 1; |
| 1683 | result->left.op = left; |
| 1684 | result->right.op = right; |
| 1685 | |
| 1686 | return result; |
| 1687 | } |
| 1688 | |
| 1689 | /* Make a typed integer literal operation. */ |
| 1690 | |
| 1691 | static const struct rust_op * |
| 1692 | ast_literal (struct typed_val_int val) |
| 1693 | { |
| 1694 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1695 | |
| 1696 | result->opcode = OP_LONG; |
| 1697 | result->left.typed_val_int = val; |
| 1698 | |
| 1699 | return result; |
| 1700 | } |
| 1701 | |
| 1702 | /* Make a typed floating point literal operation. */ |
| 1703 | |
| 1704 | static const struct rust_op * |
| 1705 | ast_dliteral (struct typed_val_float val) |
| 1706 | { |
| 1707 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1708 | |
| 1709 | result->opcode = OP_FLOAT; |
| 1710 | result->left.typed_val_float = val; |
| 1711 | |
| 1712 | return result; |
| 1713 | } |
| 1714 | |
| 1715 | /* Make a unary operation. */ |
| 1716 | |
| 1717 | static const struct rust_op * |
| 1718 | ast_unary (enum exp_opcode opcode, const struct rust_op *expr) |
| 1719 | { |
| 1720 | return ast_operation (opcode, expr, NULL); |
| 1721 | } |
| 1722 | |
| 1723 | /* Make a cast operation. */ |
| 1724 | |
| 1725 | static const struct rust_op * |
| 1726 | ast_cast (const struct rust_op *expr, const struct rust_op *type) |
| 1727 | { |
| 1728 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1729 | |
| 1730 | result->opcode = UNOP_CAST; |
| 1731 | result->left.op = expr; |
| 1732 | result->right.op = type; |
| 1733 | |
| 1734 | return result; |
| 1735 | } |
| 1736 | |
| 1737 | /* Make a call-like operation. This is nominally a function call, but |
| 1738 | when lowering we may discover that it actually represents the |
| 1739 | creation of a tuple struct. */ |
| 1740 | |
| 1741 | static const struct rust_op * |
| 1742 | ast_call_ish (enum exp_opcode opcode, const struct rust_op *expr, |
| 1743 | rust_op_vector *params) |
| 1744 | { |
| 1745 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1746 | |
| 1747 | result->opcode = opcode; |
| 1748 | result->left.op = expr; |
| 1749 | result->right.params = params; |
| 1750 | |
| 1751 | return result; |
| 1752 | } |
| 1753 | |
| 1754 | /* Make a structure creation operation. */ |
| 1755 | |
| 1756 | static const struct rust_op * |
| 1757 | ast_struct (const struct rust_op *name, rust_set_vector *fields) |
| 1758 | { |
| 1759 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1760 | |
| 1761 | result->opcode = OP_AGGREGATE; |
| 1762 | result->left.op = name; |
| 1763 | result->right.field_inits = fields; |
| 1764 | |
| 1765 | return result; |
| 1766 | } |
| 1767 | |
| 1768 | /* Make an identifier path. */ |
| 1769 | |
| 1770 | static const struct rust_op * |
| 1771 | ast_path (struct stoken path, rust_op_vector *params) |
| 1772 | { |
| 1773 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1774 | |
| 1775 | result->opcode = OP_VAR_VALUE; |
| 1776 | result->left.sval = path; |
| 1777 | result->right.params = params; |
| 1778 | |
| 1779 | return result; |
| 1780 | } |
| 1781 | |
| 1782 | /* Make a string constant operation. */ |
| 1783 | |
| 1784 | static const struct rust_op * |
| 1785 | ast_string (struct stoken str) |
| 1786 | { |
| 1787 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1788 | |
| 1789 | result->opcode = OP_STRING; |
| 1790 | result->left.sval = str; |
| 1791 | |
| 1792 | return result; |
| 1793 | } |
| 1794 | |
| 1795 | /* Make a field expression. */ |
| 1796 | |
| 1797 | static const struct rust_op * |
| 1798 | ast_structop (const struct rust_op *left, const char *name, int completing) |
| 1799 | { |
| 1800 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1801 | |
| 1802 | result->opcode = STRUCTOP_STRUCT; |
| 1803 | result->completing = completing; |
| 1804 | result->left.op = left; |
| 1805 | result->right.sval = make_stoken (name); |
| 1806 | |
| 1807 | return result; |
| 1808 | } |
| 1809 | |
| 1810 | /* Make an anonymous struct operation, like 'x.0'. */ |
| 1811 | |
| 1812 | static const struct rust_op * |
| 1813 | ast_structop_anonymous (const struct rust_op *left, |
| 1814 | struct typed_val_int number) |
| 1815 | { |
| 1816 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1817 | |
| 1818 | result->opcode = STRUCTOP_ANONYMOUS; |
| 1819 | result->left.op = left; |
| 1820 | result->right.typed_val_int = number; |
| 1821 | |
| 1822 | return result; |
| 1823 | } |
| 1824 | |
| 1825 | /* Make a range operation. */ |
| 1826 | |
| 1827 | static const struct rust_op * |
| 1828 | ast_range (const struct rust_op *lhs, const struct rust_op *rhs) |
| 1829 | { |
| 1830 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1831 | |
| 1832 | result->opcode = OP_RANGE; |
| 1833 | result->left.op = lhs; |
| 1834 | result->right.op = rhs; |
| 1835 | |
| 1836 | return result; |
| 1837 | } |
| 1838 | |
| 1839 | /* A helper function to make a type-related AST node. */ |
| 1840 | |
| 1841 | static struct rust_op * |
| 1842 | ast_basic_type (enum type_code typecode) |
| 1843 | { |
| 1844 | struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op); |
| 1845 | |
| 1846 | result->opcode = OP_TYPE; |
| 1847 | result->typecode = typecode; |
| 1848 | return result; |
| 1849 | } |
| 1850 | |
| 1851 | /* Create an AST node describing an array type. */ |
| 1852 | |
| 1853 | static const struct rust_op * |
| 1854 | ast_array_type (const struct rust_op *lhs, struct typed_val_int val) |
| 1855 | { |
| 1856 | struct rust_op *result = ast_basic_type (TYPE_CODE_ARRAY); |
| 1857 | |
| 1858 | result->left.op = lhs; |
| 1859 | result->right.typed_val_int = val; |
| 1860 | return result; |
| 1861 | } |
| 1862 | |
| 1863 | /* Create an AST node describing a reference type. */ |
| 1864 | |
| 1865 | static const struct rust_op * |
| 1866 | ast_slice_type (const struct rust_op *type) |
| 1867 | { |
| 1868 | /* Use TYPE_CODE_COMPLEX just because it is handy. */ |
| 1869 | struct rust_op *result = ast_basic_type (TYPE_CODE_COMPLEX); |
| 1870 | |
| 1871 | result->left.op = type; |
| 1872 | return result; |
| 1873 | } |
| 1874 | |
| 1875 | /* Create an AST node describing a reference type. */ |
| 1876 | |
| 1877 | static const struct rust_op * |
| 1878 | ast_reference_type (const struct rust_op *type) |
| 1879 | { |
| 1880 | struct rust_op *result = ast_basic_type (TYPE_CODE_REF); |
| 1881 | |
| 1882 | result->left.op = type; |
| 1883 | return result; |
| 1884 | } |
| 1885 | |
| 1886 | /* Create an AST node describing a pointer type. */ |
| 1887 | |
| 1888 | static const struct rust_op * |
| 1889 | ast_pointer_type (const struct rust_op *type, int is_mut) |
| 1890 | { |
| 1891 | struct rust_op *result = ast_basic_type (TYPE_CODE_PTR); |
| 1892 | |
| 1893 | result->left.op = type; |
| 1894 | /* For the time being we ignore is_mut. */ |
| 1895 | return result; |
| 1896 | } |
| 1897 | |
| 1898 | /* Create an AST node describing a function type. */ |
| 1899 | |
| 1900 | static const struct rust_op * |
| 1901 | ast_function_type (const struct rust_op *rtype, rust_op_vector *params) |
| 1902 | { |
| 1903 | struct rust_op *result = ast_basic_type (TYPE_CODE_FUNC); |
| 1904 | |
| 1905 | result->left.op = rtype; |
| 1906 | result->right.params = params; |
| 1907 | return result; |
| 1908 | } |
| 1909 | |
| 1910 | /* Create an AST node describing a tuple type. */ |
| 1911 | |
| 1912 | static const struct rust_op * |
| 1913 | ast_tuple_type (rust_op_vector *params) |
| 1914 | { |
| 1915 | struct rust_op *result = ast_basic_type (TYPE_CODE_STRUCT); |
| 1916 | |
| 1917 | result->left.params = params; |
| 1918 | return result; |
| 1919 | } |
| 1920 | |
| 1921 | /* A helper to appropriately munge NAME and BLOCK depending on the |
| 1922 | presence of a leading "::". */ |
| 1923 | |
| 1924 | static void |
| 1925 | munge_name_and_block (const char **name, const struct block **block) |
| 1926 | { |
| 1927 | /* If it is a global reference, skip the current block in favor of |
| 1928 | the static block. */ |
| 1929 | if (strncmp (*name, "::", 2) == 0) |
| 1930 | { |
| 1931 | *name += 2; |
| 1932 | *block = block_static_block (*block); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /* Like lookup_symbol, but handles Rust namespace conventions, and |
| 1937 | doesn't require field_of_this_result. */ |
| 1938 | |
| 1939 | static struct block_symbol |
| 1940 | rust_lookup_symbol (const char *name, const struct block *block, |
| 1941 | const domain_enum domain) |
| 1942 | { |
| 1943 | struct block_symbol result; |
| 1944 | |
| 1945 | munge_name_and_block (&name, &block); |
| 1946 | |
| 1947 | result = lookup_symbol (name, block, domain, NULL); |
| 1948 | if (result.symbol != NULL) |
| 1949 | update_innermost_block (result); |
| 1950 | return result; |
| 1951 | } |
| 1952 | |
| 1953 | /* Look up a type, following Rust namespace conventions. */ |
| 1954 | |
| 1955 | static struct type * |
| 1956 | rust_lookup_type (const char *name, const struct block *block) |
| 1957 | { |
| 1958 | struct block_symbol result; |
| 1959 | struct type *type; |
| 1960 | |
| 1961 | munge_name_and_block (&name, &block); |
| 1962 | |
| 1963 | result = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); |
| 1964 | if (result.symbol != NULL) |
| 1965 | { |
| 1966 | update_innermost_block (result); |
| 1967 | return SYMBOL_TYPE (result.symbol); |
| 1968 | } |
| 1969 | |
| 1970 | type = lookup_typename (current_parser->language (), current_parser->arch (), |
| 1971 | name, NULL, 1); |
| 1972 | if (type != NULL) |
| 1973 | return type; |
| 1974 | |
| 1975 | /* Last chance, try a built-in type. */ |
| 1976 | return language_lookup_primitive_type (current_parser->language (), |
| 1977 | current_parser->arch (), |
| 1978 | name); |
| 1979 | } |
| 1980 | |
| 1981 | static struct type *convert_ast_to_type (struct parser_state *state, |
| 1982 | const struct rust_op *operation); |
| 1983 | static const char *convert_name (struct parser_state *state, |
| 1984 | const struct rust_op *operation); |
| 1985 | |
| 1986 | /* Convert a vector of rust_ops representing types to a vector of |
| 1987 | types. */ |
| 1988 | |
| 1989 | static std::vector<struct type *> |
| 1990 | convert_params_to_types (struct parser_state *state, rust_op_vector *params) |
| 1991 | { |
| 1992 | std::vector<struct type *> result; |
| 1993 | |
| 1994 | for (const rust_op *op : *params) |
| 1995 | result.push_back (convert_ast_to_type (state, op)); |
| 1996 | |
| 1997 | return result; |
| 1998 | } |
| 1999 | |
| 2000 | /* Convert a rust_op representing a type to a struct type *. */ |
| 2001 | |
| 2002 | static struct type * |
| 2003 | convert_ast_to_type (struct parser_state *state, |
| 2004 | const struct rust_op *operation) |
| 2005 | { |
| 2006 | struct type *type, *result = NULL; |
| 2007 | |
| 2008 | if (operation->opcode == OP_VAR_VALUE) |
| 2009 | { |
| 2010 | const char *varname = convert_name (state, operation); |
| 2011 | |
| 2012 | result = rust_lookup_type (varname, expression_context_block); |
| 2013 | if (result == NULL) |
| 2014 | error (_("No typed name '%s' in current context"), varname); |
| 2015 | return result; |
| 2016 | } |
| 2017 | |
| 2018 | gdb_assert (operation->opcode == OP_TYPE); |
| 2019 | |
| 2020 | switch (operation->typecode) |
| 2021 | { |
| 2022 | case TYPE_CODE_ARRAY: |
| 2023 | type = convert_ast_to_type (state, operation->left.op); |
| 2024 | if (operation->right.typed_val_int.val < 0) |
| 2025 | error (_("Negative array length")); |
| 2026 | result = lookup_array_range_type (type, 0, |
| 2027 | operation->right.typed_val_int.val - 1); |
| 2028 | break; |
| 2029 | |
| 2030 | case TYPE_CODE_COMPLEX: |
| 2031 | { |
| 2032 | struct type *usize = rust_type ("usize"); |
| 2033 | |
| 2034 | type = convert_ast_to_type (state, operation->left.op); |
| 2035 | result = rust_slice_type ("&[*gdb*]", type, usize); |
| 2036 | } |
| 2037 | break; |
| 2038 | |
| 2039 | case TYPE_CODE_REF: |
| 2040 | case TYPE_CODE_PTR: |
| 2041 | /* For now we treat &x and *x identically. */ |
| 2042 | type = convert_ast_to_type (state, operation->left.op); |
| 2043 | result = lookup_pointer_type (type); |
| 2044 | break; |
| 2045 | |
| 2046 | case TYPE_CODE_FUNC: |
| 2047 | { |
| 2048 | std::vector<struct type *> args |
| 2049 | (convert_params_to_types (state, operation->right.params)); |
| 2050 | struct type **argtypes = NULL; |
| 2051 | |
| 2052 | type = convert_ast_to_type (state, operation->left.op); |
| 2053 | if (!args.empty ()) |
| 2054 | argtypes = args.data (); |
| 2055 | |
| 2056 | result |
| 2057 | = lookup_function_type_with_arguments (type, args.size (), |
| 2058 | argtypes); |
| 2059 | result = lookup_pointer_type (result); |
| 2060 | } |
| 2061 | break; |
| 2062 | |
| 2063 | case TYPE_CODE_STRUCT: |
| 2064 | { |
| 2065 | std::vector<struct type *> args |
| 2066 | (convert_params_to_types (state, operation->left.params)); |
| 2067 | int i; |
| 2068 | const char *name; |
| 2069 | |
| 2070 | obstack_1grow (work_obstack, '('); |
| 2071 | for (i = 0; i < args.size (); ++i) |
| 2072 | { |
| 2073 | std::string type_name = type_to_string (args[i]); |
| 2074 | |
| 2075 | if (i > 0) |
| 2076 | obstack_1grow (work_obstack, ','); |
| 2077 | obstack_grow_str (work_obstack, type_name.c_str ()); |
| 2078 | } |
| 2079 | |
| 2080 | obstack_grow_str0 (work_obstack, ")"); |
| 2081 | name = (const char *) obstack_finish (work_obstack); |
| 2082 | |
| 2083 | /* We don't allow creating new tuple types (yet), but we do |
| 2084 | allow looking up existing tuple types. */ |
| 2085 | result = rust_lookup_type (name, expression_context_block); |
| 2086 | if (result == NULL) |
| 2087 | error (_("could not find tuple type '%s'"), name); |
| 2088 | } |
| 2089 | break; |
| 2090 | |
| 2091 | default: |
| 2092 | gdb_assert_not_reached ("unhandled opcode in convert_ast_to_type"); |
| 2093 | } |
| 2094 | |
| 2095 | gdb_assert (result != NULL); |
| 2096 | return result; |
| 2097 | } |
| 2098 | |
| 2099 | /* A helper function to turn a rust_op representing a name into a full |
| 2100 | name. This applies generic arguments as needed. The returned name |
| 2101 | is allocated on the work obstack. */ |
| 2102 | |
| 2103 | static const char * |
| 2104 | convert_name (struct parser_state *state, const struct rust_op *operation) |
| 2105 | { |
| 2106 | int i; |
| 2107 | |
| 2108 | gdb_assert (operation->opcode == OP_VAR_VALUE); |
| 2109 | |
| 2110 | if (operation->right.params == NULL) |
| 2111 | return operation->left.sval.ptr; |
| 2112 | |
| 2113 | std::vector<struct type *> types |
| 2114 | (convert_params_to_types (state, operation->right.params)); |
| 2115 | |
| 2116 | obstack_grow_str (work_obstack, operation->left.sval.ptr); |
| 2117 | obstack_1grow (work_obstack, '<'); |
| 2118 | for (i = 0; i < types.size (); ++i) |
| 2119 | { |
| 2120 | std::string type_name = type_to_string (types[i]); |
| 2121 | |
| 2122 | if (i > 0) |
| 2123 | obstack_1grow (work_obstack, ','); |
| 2124 | |
| 2125 | obstack_grow_str (work_obstack, type_name.c_str ()); |
| 2126 | } |
| 2127 | obstack_grow_str0 (work_obstack, ">"); |
| 2128 | |
| 2129 | return (const char *) obstack_finish (work_obstack); |
| 2130 | } |
| 2131 | |
| 2132 | static void convert_ast_to_expression (struct parser_state *state, |
| 2133 | const struct rust_op *operation, |
| 2134 | const struct rust_op *top, |
| 2135 | bool want_type = false); |
| 2136 | |
| 2137 | /* A helper function that converts a vec of rust_ops to a gdb |
| 2138 | expression. */ |
| 2139 | |
| 2140 | static void |
| 2141 | convert_params_to_expression (struct parser_state *state, |
| 2142 | rust_op_vector *params, |
| 2143 | const struct rust_op *top) |
| 2144 | { |
| 2145 | for (const rust_op *elem : *params) |
| 2146 | convert_ast_to_expression (state, elem, top); |
| 2147 | } |
| 2148 | |
| 2149 | /* Lower a rust_op to a gdb expression. STATE is the parser state. |
| 2150 | OPERATION is the operation to lower. TOP is a pointer to the |
| 2151 | top-most operation; it is used to handle the special case where the |
| 2152 | top-most expression is an identifier and can be optionally lowered |
| 2153 | to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression |
| 2154 | is the name of a type, then emit an OP_TYPE for it (rather than |
| 2155 | erroring). If WANT_TYPE is set, then the similar TOP handling is |
| 2156 | not done. */ |
| 2157 | |
| 2158 | static void |
| 2159 | convert_ast_to_expression (struct parser_state *state, |
| 2160 | const struct rust_op *operation, |
| 2161 | const struct rust_op *top, |
| 2162 | bool want_type) |
| 2163 | { |
| 2164 | switch (operation->opcode) |
| 2165 | { |
| 2166 | case OP_LONG: |
| 2167 | write_exp_elt_opcode (state, OP_LONG); |
| 2168 | write_exp_elt_type (state, operation->left.typed_val_int.type); |
| 2169 | write_exp_elt_longcst (state, operation->left.typed_val_int.val); |
| 2170 | write_exp_elt_opcode (state, OP_LONG); |
| 2171 | break; |
| 2172 | |
| 2173 | case OP_FLOAT: |
| 2174 | write_exp_elt_opcode (state, OP_FLOAT); |
| 2175 | write_exp_elt_type (state, operation->left.typed_val_float.type); |
| 2176 | write_exp_elt_floatcst (state, operation->left.typed_val_float.val); |
| 2177 | write_exp_elt_opcode (state, OP_FLOAT); |
| 2178 | break; |
| 2179 | |
| 2180 | case STRUCTOP_STRUCT: |
| 2181 | { |
| 2182 | convert_ast_to_expression (state, operation->left.op, top); |
| 2183 | |
| 2184 | if (operation->completing) |
| 2185 | mark_struct_expression (state); |
| 2186 | write_exp_elt_opcode (state, STRUCTOP_STRUCT); |
| 2187 | write_exp_string (state, operation->right.sval); |
| 2188 | write_exp_elt_opcode (state, STRUCTOP_STRUCT); |
| 2189 | } |
| 2190 | break; |
| 2191 | |
| 2192 | case STRUCTOP_ANONYMOUS: |
| 2193 | { |
| 2194 | convert_ast_to_expression (state, operation->left.op, top); |
| 2195 | |
| 2196 | write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS); |
| 2197 | write_exp_elt_longcst (state, operation->right.typed_val_int.val); |
| 2198 | write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS); |
| 2199 | } |
| 2200 | break; |
| 2201 | |
| 2202 | case UNOP_SIZEOF: |
| 2203 | convert_ast_to_expression (state, operation->left.op, top, true); |
| 2204 | write_exp_elt_opcode (state, UNOP_SIZEOF); |
| 2205 | break; |
| 2206 | |
| 2207 | case UNOP_PLUS: |
| 2208 | case UNOP_NEG: |
| 2209 | case UNOP_COMPLEMENT: |
| 2210 | case UNOP_IND: |
| 2211 | case UNOP_ADDR: |
| 2212 | convert_ast_to_expression (state, operation->left.op, top); |
| 2213 | write_exp_elt_opcode (state, operation->opcode); |
| 2214 | break; |
| 2215 | |
| 2216 | case BINOP_SUBSCRIPT: |
| 2217 | case BINOP_MUL: |
| 2218 | case BINOP_REPEAT: |
| 2219 | case BINOP_DIV: |
| 2220 | case BINOP_REM: |
| 2221 | case BINOP_LESS: |
| 2222 | case BINOP_GTR: |
| 2223 | case BINOP_BITWISE_AND: |
| 2224 | case BINOP_BITWISE_IOR: |
| 2225 | case BINOP_BITWISE_XOR: |
| 2226 | case BINOP_ADD: |
| 2227 | case BINOP_SUB: |
| 2228 | case BINOP_LOGICAL_OR: |
| 2229 | case BINOP_LOGICAL_AND: |
| 2230 | case BINOP_EQUAL: |
| 2231 | case BINOP_NOTEQUAL: |
| 2232 | case BINOP_LEQ: |
| 2233 | case BINOP_GEQ: |
| 2234 | case BINOP_LSH: |
| 2235 | case BINOP_RSH: |
| 2236 | case BINOP_ASSIGN: |
| 2237 | case OP_RUST_ARRAY: |
| 2238 | convert_ast_to_expression (state, operation->left.op, top); |
| 2239 | convert_ast_to_expression (state, operation->right.op, top); |
| 2240 | if (operation->compound_assignment) |
| 2241 | { |
| 2242 | write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY); |
| 2243 | write_exp_elt_opcode (state, operation->opcode); |
| 2244 | write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY); |
| 2245 | } |
| 2246 | else |
| 2247 | write_exp_elt_opcode (state, operation->opcode); |
| 2248 | |
| 2249 | if (operation->compound_assignment |
| 2250 | || operation->opcode == BINOP_ASSIGN) |
| 2251 | { |
| 2252 | struct type *type; |
| 2253 | |
| 2254 | type = language_lookup_primitive_type (parse_language (state), |
| 2255 | parse_gdbarch (state), |
| 2256 | "()"); |
| 2257 | |
| 2258 | write_exp_elt_opcode (state, OP_LONG); |
| 2259 | write_exp_elt_type (state, type); |
| 2260 | write_exp_elt_longcst (state, 0); |
| 2261 | write_exp_elt_opcode (state, OP_LONG); |
| 2262 | |
| 2263 | write_exp_elt_opcode (state, BINOP_COMMA); |
| 2264 | } |
| 2265 | break; |
| 2266 | |
| 2267 | case UNOP_CAST: |
| 2268 | { |
| 2269 | struct type *type = convert_ast_to_type (state, operation->right.op); |
| 2270 | |
| 2271 | convert_ast_to_expression (state, operation->left.op, top); |
| 2272 | write_exp_elt_opcode (state, UNOP_CAST); |
| 2273 | write_exp_elt_type (state, type); |
| 2274 | write_exp_elt_opcode (state, UNOP_CAST); |
| 2275 | } |
| 2276 | break; |
| 2277 | |
| 2278 | case OP_FUNCALL: |
| 2279 | { |
| 2280 | if (operation->left.op->opcode == OP_VAR_VALUE) |
| 2281 | { |
| 2282 | struct type *type; |
| 2283 | const char *varname = convert_name (state, operation->left.op); |
| 2284 | |
| 2285 | type = rust_lookup_type (varname, expression_context_block); |
| 2286 | if (type != NULL) |
| 2287 | { |
| 2288 | /* This is actually a tuple struct expression, not a |
| 2289 | call expression. */ |
| 2290 | rust_op_vector *params = operation->right.params; |
| 2291 | |
| 2292 | if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE) |
| 2293 | { |
| 2294 | if (!rust_tuple_struct_type_p (type)) |
| 2295 | error (_("Type %s is not a tuple struct"), varname); |
| 2296 | |
| 2297 | for (int i = 0; i < params->size (); ++i) |
| 2298 | { |
| 2299 | char *cell = get_print_cell (); |
| 2300 | |
| 2301 | xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i); |
| 2302 | write_exp_elt_opcode (state, OP_NAME); |
| 2303 | write_exp_string (state, make_stoken (cell)); |
| 2304 | write_exp_elt_opcode (state, OP_NAME); |
| 2305 | |
| 2306 | convert_ast_to_expression (state, (*params)[i], top); |
| 2307 | } |
| 2308 | |
| 2309 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2310 | write_exp_elt_type (state, type); |
| 2311 | write_exp_elt_longcst (state, 2 * params->size ()); |
| 2312 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2313 | break; |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | convert_ast_to_expression (state, operation->left.op, top); |
| 2318 | convert_params_to_expression (state, operation->right.params, top); |
| 2319 | write_exp_elt_opcode (state, OP_FUNCALL); |
| 2320 | write_exp_elt_longcst (state, operation->right.params->size ()); |
| 2321 | write_exp_elt_longcst (state, OP_FUNCALL); |
| 2322 | } |
| 2323 | break; |
| 2324 | |
| 2325 | case OP_ARRAY: |
| 2326 | gdb_assert (operation->left.op == NULL); |
| 2327 | convert_params_to_expression (state, operation->right.params, top); |
| 2328 | write_exp_elt_opcode (state, OP_ARRAY); |
| 2329 | write_exp_elt_longcst (state, 0); |
| 2330 | write_exp_elt_longcst (state, operation->right.params->size () - 1); |
| 2331 | write_exp_elt_longcst (state, OP_ARRAY); |
| 2332 | break; |
| 2333 | |
| 2334 | case OP_VAR_VALUE: |
| 2335 | { |
| 2336 | struct block_symbol sym; |
| 2337 | const char *varname; |
| 2338 | |
| 2339 | if (operation->left.sval.ptr[0] == '$') |
| 2340 | { |
| 2341 | write_dollar_variable (state, operation->left.sval); |
| 2342 | break; |
| 2343 | } |
| 2344 | |
| 2345 | varname = convert_name (state, operation); |
| 2346 | sym = rust_lookup_symbol (varname, expression_context_block, |
| 2347 | VAR_DOMAIN); |
| 2348 | if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF) |
| 2349 | { |
| 2350 | write_exp_elt_opcode (state, OP_VAR_VALUE); |
| 2351 | write_exp_elt_block (state, sym.block); |
| 2352 | write_exp_elt_sym (state, sym.symbol); |
| 2353 | write_exp_elt_opcode (state, OP_VAR_VALUE); |
| 2354 | } |
| 2355 | else |
| 2356 | { |
| 2357 | struct type *type = NULL; |
| 2358 | |
| 2359 | if (sym.symbol != NULL) |
| 2360 | { |
| 2361 | gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF); |
| 2362 | type = SYMBOL_TYPE (sym.symbol); |
| 2363 | } |
| 2364 | if (type == NULL) |
| 2365 | type = rust_lookup_type (varname, expression_context_block); |
| 2366 | if (type == NULL) |
| 2367 | error (_("No symbol '%s' in current context"), varname); |
| 2368 | |
| 2369 | if (!want_type |
| 2370 | && TYPE_CODE (type) == TYPE_CODE_STRUCT |
| 2371 | && TYPE_NFIELDS (type) == 0) |
| 2372 | { |
| 2373 | /* A unit-like struct. */ |
| 2374 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2375 | write_exp_elt_type (state, type); |
| 2376 | write_exp_elt_longcst (state, 0); |
| 2377 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2378 | } |
| 2379 | else if (want_type || operation == top) |
| 2380 | { |
| 2381 | write_exp_elt_opcode (state, OP_TYPE); |
| 2382 | write_exp_elt_type (state, type); |
| 2383 | write_exp_elt_opcode (state, OP_TYPE); |
| 2384 | } |
| 2385 | else |
| 2386 | error (_("Found type '%s', which can't be " |
| 2387 | "evaluated in this context"), |
| 2388 | varname); |
| 2389 | } |
| 2390 | } |
| 2391 | break; |
| 2392 | |
| 2393 | case OP_AGGREGATE: |
| 2394 | { |
| 2395 | int length; |
| 2396 | rust_set_vector *fields = operation->right.field_inits; |
| 2397 | struct type *type; |
| 2398 | const char *name; |
| 2399 | |
| 2400 | length = 0; |
| 2401 | for (const set_field &init : *fields) |
| 2402 | { |
| 2403 | if (init.name.ptr != NULL) |
| 2404 | { |
| 2405 | write_exp_elt_opcode (state, OP_NAME); |
| 2406 | write_exp_string (state, init.name); |
| 2407 | write_exp_elt_opcode (state, OP_NAME); |
| 2408 | ++length; |
| 2409 | } |
| 2410 | |
| 2411 | convert_ast_to_expression (state, init.init, top); |
| 2412 | ++length; |
| 2413 | |
| 2414 | if (init.name.ptr == NULL) |
| 2415 | { |
| 2416 | /* This is handled differently from Ada in our |
| 2417 | evaluator. */ |
| 2418 | write_exp_elt_opcode (state, OP_OTHERS); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | name = convert_name (state, operation->left.op); |
| 2423 | type = rust_lookup_type (name, expression_context_block); |
| 2424 | if (type == NULL) |
| 2425 | error (_("Could not find type '%s'"), operation->left.sval.ptr); |
| 2426 | |
| 2427 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
| 2428 | || rust_tuple_type_p (type) |
| 2429 | || rust_tuple_struct_type_p (type)) |
| 2430 | error (_("Struct expression applied to non-struct type")); |
| 2431 | |
| 2432 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2433 | write_exp_elt_type (state, type); |
| 2434 | write_exp_elt_longcst (state, length); |
| 2435 | write_exp_elt_opcode (state, OP_AGGREGATE); |
| 2436 | } |
| 2437 | break; |
| 2438 | |
| 2439 | case OP_STRING: |
| 2440 | { |
| 2441 | write_exp_elt_opcode (state, OP_STRING); |
| 2442 | write_exp_string (state, operation->left.sval); |
| 2443 | write_exp_elt_opcode (state, OP_STRING); |
| 2444 | } |
| 2445 | break; |
| 2446 | |
| 2447 | case OP_RANGE: |
| 2448 | { |
| 2449 | enum range_type kind = BOTH_BOUND_DEFAULT; |
| 2450 | |
| 2451 | if (operation->left.op != NULL) |
| 2452 | { |
| 2453 | convert_ast_to_expression (state, operation->left.op, top); |
| 2454 | kind = HIGH_BOUND_DEFAULT; |
| 2455 | } |
| 2456 | if (operation->right.op != NULL) |
| 2457 | { |
| 2458 | convert_ast_to_expression (state, operation->right.op, top); |
| 2459 | if (kind == BOTH_BOUND_DEFAULT) |
| 2460 | kind = LOW_BOUND_DEFAULT; |
| 2461 | else |
| 2462 | { |
| 2463 | gdb_assert (kind == HIGH_BOUND_DEFAULT); |
| 2464 | kind = NONE_BOUND_DEFAULT; |
| 2465 | } |
| 2466 | } |
| 2467 | write_exp_elt_opcode (state, OP_RANGE); |
| 2468 | write_exp_elt_longcst (state, kind); |
| 2469 | write_exp_elt_opcode (state, OP_RANGE); |
| 2470 | } |
| 2471 | break; |
| 2472 | |
| 2473 | default: |
| 2474 | gdb_assert_not_reached ("unhandled opcode in convert_ast_to_expression"); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | \f |
| 2479 | |
| 2480 | /* The parser as exposed to gdb. */ |
| 2481 | |
| 2482 | int |
| 2483 | rust_parse (struct parser_state *state) |
| 2484 | { |
| 2485 | int result; |
| 2486 | |
| 2487 | /* This sets various globals and also clears them on |
| 2488 | destruction. */ |
| 2489 | rust_parser parser (state); |
| 2490 | |
| 2491 | result = rustyyparse (); |
| 2492 | |
| 2493 | if (!result || (parse_completion && parser.rust_ast != NULL)) |
| 2494 | convert_ast_to_expression (state, parser.rust_ast, parser.rust_ast); |
| 2495 | |
| 2496 | return result; |
| 2497 | } |
| 2498 | |
| 2499 | /* The parser error handler. */ |
| 2500 | |
| 2501 | void |
| 2502 | rustyyerror (const char *msg) |
| 2503 | { |
| 2504 | const char *where = prev_lexptr ? prev_lexptr : lexptr; |
| 2505 | error (_("%s in expression, near `%s'."), (msg ? msg : "Error"), where); |
| 2506 | } |
| 2507 | |
| 2508 | \f |
| 2509 | |
| 2510 | #if GDB_SELF_TEST |
| 2511 | |
| 2512 | /* Initialize the lexer for testing. */ |
| 2513 | |
| 2514 | static void |
| 2515 | rust_lex_test_init (const char *input) |
| 2516 | { |
| 2517 | prev_lexptr = NULL; |
| 2518 | lexptr = input; |
| 2519 | paren_depth = 0; |
| 2520 | } |
| 2521 | |
| 2522 | /* A test helper that lexes a string, expecting a single token. It |
| 2523 | returns the lexer data for this token. */ |
| 2524 | |
| 2525 | static RUSTSTYPE |
| 2526 | rust_lex_test_one (const char *input, int expected) |
| 2527 | { |
| 2528 | int token; |
| 2529 | RUSTSTYPE result; |
| 2530 | |
| 2531 | rust_lex_test_init (input); |
| 2532 | |
| 2533 | token = rustyylex (); |
| 2534 | SELF_CHECK (token == expected); |
| 2535 | result = rustyylval; |
| 2536 | |
| 2537 | if (token) |
| 2538 | { |
| 2539 | token = rustyylex (); |
| 2540 | SELF_CHECK (token == 0); |
| 2541 | } |
| 2542 | |
| 2543 | return result; |
| 2544 | } |
| 2545 | |
| 2546 | /* Test that INPUT lexes as the integer VALUE. */ |
| 2547 | |
| 2548 | static void |
| 2549 | rust_lex_int_test (const char *input, int value, int kind) |
| 2550 | { |
| 2551 | RUSTSTYPE result = rust_lex_test_one (input, kind); |
| 2552 | SELF_CHECK (result.typed_val_int.val == value); |
| 2553 | } |
| 2554 | |
| 2555 | /* Test that INPUT throws an exception with text ERR. */ |
| 2556 | |
| 2557 | static void |
| 2558 | rust_lex_exception_test (const char *input, const char *err) |
| 2559 | { |
| 2560 | TRY |
| 2561 | { |
| 2562 | /* The "kind" doesn't matter. */ |
| 2563 | rust_lex_test_one (input, DECIMAL_INTEGER); |
| 2564 | SELF_CHECK (0); |
| 2565 | } |
| 2566 | CATCH (except, RETURN_MASK_ERROR) |
| 2567 | { |
| 2568 | SELF_CHECK (strcmp (except.message, err) == 0); |
| 2569 | } |
| 2570 | END_CATCH |
| 2571 | } |
| 2572 | |
| 2573 | /* Test that INPUT lexes as the identifier, string, or byte-string |
| 2574 | VALUE. KIND holds the expected token kind. */ |
| 2575 | |
| 2576 | static void |
| 2577 | rust_lex_stringish_test (const char *input, const char *value, int kind) |
| 2578 | { |
| 2579 | RUSTSTYPE result = rust_lex_test_one (input, kind); |
| 2580 | SELF_CHECK (result.sval.length == strlen (value)); |
| 2581 | SELF_CHECK (strncmp (result.sval.ptr, value, result.sval.length) == 0); |
| 2582 | } |
| 2583 | |
| 2584 | /* Helper to test that a string parses as a given token sequence. */ |
| 2585 | |
| 2586 | static void |
| 2587 | rust_lex_test_sequence (const char *input, int len, const int expected[]) |
| 2588 | { |
| 2589 | int i; |
| 2590 | |
| 2591 | lexptr = input; |
| 2592 | paren_depth = 0; |
| 2593 | |
| 2594 | for (i = 0; i < len; ++i) |
| 2595 | { |
| 2596 | int token = rustyylex (); |
| 2597 | |
| 2598 | SELF_CHECK (token == expected[i]); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /* Tests for an integer-parsing corner case. */ |
| 2603 | |
| 2604 | static void |
| 2605 | rust_lex_test_trailing_dot (void) |
| 2606 | { |
| 2607 | const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 }; |
| 2608 | const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 }; |
| 2609 | const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 }; |
| 2610 | const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 }; |
| 2611 | |
| 2612 | rust_lex_test_sequence ("23.g()", ARRAY_SIZE (expected1), expected1); |
| 2613 | rust_lex_test_sequence ("23_0.g()", ARRAY_SIZE (expected2), expected2); |
| 2614 | rust_lex_test_sequence ("23.==()", ARRAY_SIZE (expected3), expected3); |
| 2615 | rust_lex_test_sequence ("23..25", ARRAY_SIZE (expected4), expected4); |
| 2616 | } |
| 2617 | |
| 2618 | /* Tests of completion. */ |
| 2619 | |
| 2620 | static void |
| 2621 | rust_lex_test_completion (void) |
| 2622 | { |
| 2623 | const int expected[] = { IDENT, '.', COMPLETE, 0 }; |
| 2624 | |
| 2625 | parse_completion = 1; |
| 2626 | |
| 2627 | rust_lex_test_sequence ("something.wha", ARRAY_SIZE (expected), expected); |
| 2628 | rust_lex_test_sequence ("something.", ARRAY_SIZE (expected), expected); |
| 2629 | |
| 2630 | parse_completion = 0; |
| 2631 | } |
| 2632 | |
| 2633 | /* Test pushback. */ |
| 2634 | |
| 2635 | static void |
| 2636 | rust_lex_test_push_back (void) |
| 2637 | { |
| 2638 | int token; |
| 2639 | |
| 2640 | rust_lex_test_init (">>="); |
| 2641 | |
| 2642 | token = rustyylex (); |
| 2643 | SELF_CHECK (token == COMPOUND_ASSIGN); |
| 2644 | SELF_CHECK (rustyylval.opcode == BINOP_RSH); |
| 2645 | |
| 2646 | rust_push_back ('='); |
| 2647 | |
| 2648 | token = rustyylex (); |
| 2649 | SELF_CHECK (token == '='); |
| 2650 | |
| 2651 | token = rustyylex (); |
| 2652 | SELF_CHECK (token == 0); |
| 2653 | } |
| 2654 | |
| 2655 | /* Unit test the lexer. */ |
| 2656 | |
| 2657 | static void |
| 2658 | rust_lex_tests (void) |
| 2659 | { |
| 2660 | int i; |
| 2661 | |
| 2662 | auto_obstack test_obstack; |
| 2663 | scoped_restore obstack_holder = make_scoped_restore (&work_obstack, |
| 2664 | &test_obstack); |
| 2665 | |
| 2666 | // Set up dummy "parser", so that rust_type works. |
| 2667 | struct parser_state ps (0, &rust_language_defn, target_gdbarch ()); |
| 2668 | rust_parser parser (&ps); |
| 2669 | |
| 2670 | rust_lex_test_one ("", 0); |
| 2671 | rust_lex_test_one (" \t \n \r ", 0); |
| 2672 | rust_lex_test_one ("thread 23", 0); |
| 2673 | rust_lex_test_one ("task 23", 0); |
| 2674 | rust_lex_test_one ("th 104", 0); |
| 2675 | rust_lex_test_one ("ta 97", 0); |
| 2676 | |
| 2677 | rust_lex_int_test ("'z'", 'z', INTEGER); |
| 2678 | rust_lex_int_test ("'\\xff'", 0xff, INTEGER); |
| 2679 | rust_lex_int_test ("'\\u{1016f}'", 0x1016f, INTEGER); |
| 2680 | rust_lex_int_test ("b'z'", 'z', INTEGER); |
| 2681 | rust_lex_int_test ("b'\\xfe'", 0xfe, INTEGER); |
| 2682 | rust_lex_int_test ("b'\\xFE'", 0xfe, INTEGER); |
| 2683 | rust_lex_int_test ("b'\\xfE'", 0xfe, INTEGER); |
| 2684 | |
| 2685 | /* Test all escapes in both modes. */ |
| 2686 | rust_lex_int_test ("'\\n'", '\n', INTEGER); |
| 2687 | rust_lex_int_test ("'\\r'", '\r', INTEGER); |
| 2688 | rust_lex_int_test ("'\\t'", '\t', INTEGER); |
| 2689 | rust_lex_int_test ("'\\\\'", '\\', INTEGER); |
| 2690 | rust_lex_int_test ("'\\0'", '\0', INTEGER); |
| 2691 | rust_lex_int_test ("'\\''", '\'', INTEGER); |
| 2692 | rust_lex_int_test ("'\\\"'", '"', INTEGER); |
| 2693 | |
| 2694 | rust_lex_int_test ("b'\\n'", '\n', INTEGER); |
| 2695 | rust_lex_int_test ("b'\\r'", '\r', INTEGER); |
| 2696 | rust_lex_int_test ("b'\\t'", '\t', INTEGER); |
| 2697 | rust_lex_int_test ("b'\\\\'", '\\', INTEGER); |
| 2698 | rust_lex_int_test ("b'\\0'", '\0', INTEGER); |
| 2699 | rust_lex_int_test ("b'\\''", '\'', INTEGER); |
| 2700 | rust_lex_int_test ("b'\\\"'", '"', INTEGER); |
| 2701 | |
| 2702 | rust_lex_exception_test ("'z", "Unterminated character literal"); |
| 2703 | rust_lex_exception_test ("b'\\x0'", "Not enough hex digits seen"); |
| 2704 | rust_lex_exception_test ("b'\\u{0}'", "Unicode escape in byte literal"); |
| 2705 | rust_lex_exception_test ("'\\x0'", "Not enough hex digits seen"); |
| 2706 | rust_lex_exception_test ("'\\u0'", "Missing '{' in Unicode escape"); |
| 2707 | rust_lex_exception_test ("'\\u{0", "Missing '}' in Unicode escape"); |
| 2708 | rust_lex_exception_test ("'\\u{0000007}", "Overlong hex escape"); |
| 2709 | rust_lex_exception_test ("'\\u{}", "Not enough hex digits seen"); |
| 2710 | rust_lex_exception_test ("'\\Q'", "Invalid escape \\Q in literal"); |
| 2711 | rust_lex_exception_test ("b'\\Q'", "Invalid escape \\Q in literal"); |
| 2712 | |
| 2713 | rust_lex_int_test ("23", 23, DECIMAL_INTEGER); |
| 2714 | rust_lex_int_test ("2_344__29", 234429, INTEGER); |
| 2715 | rust_lex_int_test ("0x1f", 0x1f, INTEGER); |
| 2716 | rust_lex_int_test ("23usize", 23, INTEGER); |
| 2717 | rust_lex_int_test ("23i32", 23, INTEGER); |
| 2718 | rust_lex_int_test ("0x1_f", 0x1f, INTEGER); |
| 2719 | rust_lex_int_test ("0b1_101011__", 0x6b, INTEGER); |
| 2720 | rust_lex_int_test ("0o001177i64", 639, INTEGER); |
| 2721 | |
| 2722 | rust_lex_test_trailing_dot (); |
| 2723 | |
| 2724 | rust_lex_test_one ("23.", FLOAT); |
| 2725 | rust_lex_test_one ("23.99f32", FLOAT); |
| 2726 | rust_lex_test_one ("23e7", FLOAT); |
| 2727 | rust_lex_test_one ("23E-7", FLOAT); |
| 2728 | rust_lex_test_one ("23e+7", FLOAT); |
| 2729 | rust_lex_test_one ("23.99e+7f64", FLOAT); |
| 2730 | rust_lex_test_one ("23.82f32", FLOAT); |
| 2731 | |
| 2732 | rust_lex_stringish_test ("hibob", "hibob", IDENT); |
| 2733 | rust_lex_stringish_test ("hibob__93", "hibob__93", IDENT); |
| 2734 | rust_lex_stringish_test ("thread", "thread", IDENT); |
| 2735 | |
| 2736 | rust_lex_stringish_test ("\"string\"", "string", STRING); |
| 2737 | rust_lex_stringish_test ("\"str\\ting\"", "str\ting", STRING); |
| 2738 | rust_lex_stringish_test ("\"str\\\"ing\"", "str\"ing", STRING); |
| 2739 | rust_lex_stringish_test ("r\"str\\ing\"", "str\\ing", STRING); |
| 2740 | rust_lex_stringish_test ("r#\"str\\ting\"#", "str\\ting", STRING); |
| 2741 | rust_lex_stringish_test ("r###\"str\\\"ing\"###", "str\\\"ing", STRING); |
| 2742 | |
| 2743 | rust_lex_stringish_test ("b\"string\"", "string", BYTESTRING); |
| 2744 | rust_lex_stringish_test ("b\"\x73tring\"", "string", BYTESTRING); |
| 2745 | rust_lex_stringish_test ("b\"str\\\"ing\"", "str\"ing", BYTESTRING); |
| 2746 | rust_lex_stringish_test ("br####\"\\x73tring\"####", "\\x73tring", |
| 2747 | BYTESTRING); |
| 2748 | |
| 2749 | for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i) |
| 2750 | rust_lex_test_one (identifier_tokens[i].name, identifier_tokens[i].value); |
| 2751 | |
| 2752 | for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i) |
| 2753 | rust_lex_test_one (operator_tokens[i].name, operator_tokens[i].value); |
| 2754 | |
| 2755 | rust_lex_test_completion (); |
| 2756 | rust_lex_test_push_back (); |
| 2757 | } |
| 2758 | |
| 2759 | #endif /* GDB_SELF_TEST */ |
| 2760 | |
| 2761 | void |
| 2762 | _initialize_rust_exp (void) |
| 2763 | { |
| 2764 | int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED); |
| 2765 | /* If the regular expression was incorrect, it was a programming |
| 2766 | error. */ |
| 2767 | gdb_assert (code == 0); |
| 2768 | |
| 2769 | #if GDB_SELF_TEST |
| 2770 | selftests::register_test ("rust-lex", rust_lex_tests); |
| 2771 | #endif |
| 2772 | } |