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