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