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