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