1 /* Bison parser for Rust expressions, for GDB.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 /* Removing the last conflict seems difficult. */
28 #include "cp-support.h"
29 #include "gdb_obstack.h"
30 #include "gdb_regex.h"
31 #include "rust-lang.h"
32 #include "parser-defs.h"
37 #define GDB_YY_REMAP_PREFIX rust
40 #define RUSTSTYPE YYSTYPE
43 typedef std::vector<const struct rust_op *> rust_op_vector;
45 /* A typed integer constant. */
53 /* A typed floating point constant. */
55 struct typed_val_float
61 /* An identifier and an expression. This is used to represent one
62 element of a struct initializer. */
67 const struct rust_op *init;
70 typedef std::vector<set_field> rust_set_vector;
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);
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);
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,
99 static const struct rust_op *ast_structop_anonymous
100 (const struct rust_op *left, struct typed_val_int number);
101 static const struct rust_op *ast_unary (enum exp_opcode opcode,
102 const struct rust_op *expr);
103 static const struct rust_op *ast_cast (const struct rust_op *expr,
104 const struct rust_op *type);
105 static const struct rust_op *ast_call_ish (enum exp_opcode opcode,
106 const struct rust_op *expr,
107 rust_op_vector *params);
108 static const struct rust_op *ast_path (struct stoken name,
109 rust_op_vector *params);
110 static const struct rust_op *ast_string (struct stoken str);
111 static const struct rust_op *ast_struct (const struct rust_op *name,
112 rust_set_vector *fields);
113 static const struct rust_op *ast_range (const struct rust_op *lhs,
114 const struct rust_op *rhs,
116 static const struct rust_op *ast_array_type (const struct rust_op *lhs,
117 struct typed_val_int val);
118 static const struct rust_op *ast_slice_type (const struct rust_op *type);
119 static const struct rust_op *ast_reference_type (const struct rust_op *type);
120 static const struct rust_op *ast_pointer_type (const struct rust_op *type,
122 static const struct rust_op *ast_function_type (const struct rust_op *result,
123 rust_op_vector *params);
124 static const struct rust_op *ast_tuple_type (rust_op_vector *params);
126 /* The current rust parser. */
129 static rust_parser *current_parser;
131 /* A regular expression for matching Rust numbers. This is split up
132 since it is very long and this gives us a way to comment the
135 static const char *number_regex_text =
136 /* subexpression 1: allows use of alternation, otherwise uninteresting */
138 /* First comes floating point. */
139 /* Recognize number after the decimal point, with optional
140 exponent and optional type suffix.
141 subexpression 2: allows "?", otherwise uninteresting
142 subexpression 3: if present, type suffix
144 "[0-9][0-9_]*\\.[0-9][0-9_]*([eE][-+]?[0-9][0-9_]*)?(f32|f64)?"
145 #define FLOAT_TYPE1 3
147 /* Recognize exponent without decimal point, with optional type
149 subexpression 4: if present, type suffix
151 #define FLOAT_TYPE2 4
152 "[0-9][0-9_]*[eE][-+]?[0-9][0-9_]*(f32|f64)?"
154 /* "23." is a valid floating point number, but "23.e5" and
155 "23.f32" are not. So, handle the trailing-. case
159 /* Finally come integers.
160 subexpression 5: text of integer
161 subexpression 6: if present, type suffix
162 subexpression 7: allows use of alternation, otherwise uninteresting
166 "(0x[a-fA-F0-9_]+|0o[0-7_]+|0b[01_]+|[0-9][0-9_]*)"
167 "([iu](size|8|16|32|64))?"
169 /* The number of subexpressions to allocate space for, including the
170 "0th" whole match subexpression. */
171 #define NUM_SUBEXPRESSIONS 8
173 /* The compiled number-matching regex. */
175 static regex_t number_regex;
177 /* Obstack for data temporarily allocated during parsing. Points to
178 the obstack in the rust_parser, or to a temporary obstack during
181 static auto_obstack *work_obstack;
183 /* An instance of this is created before parsing, and destroyed when
184 parsing is finished. */
188 rust_parser (struct parser_state *state)
189 : rust_ast (nullptr),
192 gdb_assert (current_parser == nullptr);
193 current_parser = this;
194 work_obstack = &obstack;
199 /* Clean up the globals we set. */
200 current_parser = nullptr;
201 work_obstack = nullptr;
204 /* Create a new rust_set_vector. The storage for the new vector is
205 managed by this class. */
206 rust_set_vector *new_set_vector ()
208 rust_set_vector *result = new rust_set_vector;
209 set_vectors.push_back (std::unique_ptr<rust_set_vector> (result));
213 /* Create a new rust_ops_vector. The storage for the new vector is
214 managed by this class. */
215 rust_op_vector *new_op_vector ()
217 rust_op_vector *result = new rust_op_vector;
218 op_vectors.push_back (std::unique_ptr<rust_op_vector> (result));
222 /* Return the parser's language. */
223 const struct language_defn *language () const
225 return parse_language (pstate);
228 /* Return the parser's gdbarch. */
229 struct gdbarch *arch () const
231 return parse_gdbarch (pstate);
234 /* A pointer to this is installed globally. */
235 auto_obstack obstack;
237 /* Result of parsing. Points into obstack. */
238 const struct rust_op *rust_ast;
240 /* This keeps track of the various vectors we allocate. */
241 std::vector<std::unique_ptr<rust_set_vector>> set_vectors;
242 std::vector<std::unique_ptr<rust_op_vector>> op_vectors;
244 /* The parser state gdb gave us. */
245 struct parser_state *pstate;
252 /* A typed integer constant. */
253 struct typed_val_int typed_val_int;
255 /* A typed floating point constant. */
256 struct typed_val_float typed_val_float;
258 /* An identifier or string. */
261 /* A token representing an opcode, like "==". */
262 enum exp_opcode opcode;
264 /* A list of expressions; for example, the arguments to a function
266 rust_op_vector *params;
268 /* A list of field initializers. */
269 rust_set_vector *field_inits;
271 /* A single field initializer. */
272 struct set_field one_field_init;
275 const struct rust_op *op;
277 /* A plain integer, for example used to count the number of
278 "super::" prefixes on a path. */
284 /* Rust AST operations. We build a tree of these; then lower them
285 to gdb expressions when parsing has completed. */
290 enum exp_opcode opcode;
291 /* If OPCODE is OP_TYPE, then this holds information about what type
292 is described by this node. */
293 enum type_code typecode;
294 /* Indicates whether OPCODE actually represents a compound
295 assignment. For example, if OPCODE is GTGT and this is false,
296 then this rust_op represents an ordinary ">>"; but if this is
297 true, then this rust_op represents ">>=". Unused in other
299 unsigned int compound_assignment : 1;
300 /* Only used by a field expression; if set, indicates that the field
301 name occurred at the end of the expression and is eligible for
303 unsigned int completing : 1;
304 /* For OP_RANGE, indicates whether the range is inclusive or
306 unsigned int inclusive : 1;
307 /* Operands of expression. Which one is used and how depends on the
308 particular opcode. */
317 %token <sval> COMPLETE
318 %token <typed_val_int> INTEGER
319 %token <typed_val_int> DECIMAL_INTEGER
321 %token <sval> BYTESTRING
322 %token <typed_val_float> FLOAT
323 %token <opcode> COMPOUND_ASSIGN
325 /* Keyword tokens. */
326 %token <voidval> KW_AS
327 %token <voidval> KW_IF
328 %token <voidval> KW_TRUE
329 %token <voidval> KW_FALSE
330 %token <voidval> KW_SUPER
331 %token <voidval> KW_SELF
332 %token <voidval> KW_MUT
333 %token <voidval> KW_EXTERN
334 %token <voidval> KW_CONST
335 %token <voidval> KW_FN
336 %token <voidval> KW_SIZEOF
338 /* Operator tokens. */
339 %token <voidval> DOTDOT
340 %token <voidval> DOTDOTEQ
341 %token <voidval> OROR
342 %token <voidval> ANDAND
343 %token <voidval> EQEQ
344 %token <voidval> NOTEQ
345 %token <voidval> LTEQ
346 %token <voidval> GTEQ
347 %token <voidval> LSH RSH
348 %token <voidval> COLONCOLON
349 %token <voidval> ARROW
352 %type <op> path_for_expr
353 %type <op> identifier_path_for_expr
354 %type <op> path_for_type
355 %type <op> identifier_path_for_type
356 %type <op> just_identifiers_for_type
358 %type <params> maybe_type_list
359 %type <params> type_list
361 %type <depth> super_path
365 %type <op> field_expr
368 %type <op> binop_expr
369 %type <op> binop_expr_expr
370 %type <op> type_cast_expr
371 %type <op> assignment_expr
372 %type <op> compound_assignment_expr
373 %type <op> paren_expr
376 %type <op> tuple_expr
378 %type <op> struct_expr
379 %type <op> array_expr
380 %type <op> range_expr
382 %type <params> expr_list
383 %type <params> maybe_expr_list
384 %type <params> paren_expr_list
386 %type <field_inits> struct_expr_list
387 %type <one_field_init> struct_expr_tail
390 %nonassoc DOTDOT DOTDOTEQ
391 %right '=' COMPOUND_ASSIGN
394 %nonassoc EQEQ NOTEQ '<' '>' LTEQ GTEQ
402 /* These could be %precedence in Bison, but that isn't a yacc
413 /* If we are completing and see a valid parse,
414 rust_ast will already have been set. */
415 if (current_parser->rust_ast == NULL)
416 current_parser->rust_ast = $1;
420 /* Note that the Rust grammar includes a method_call_expr, but we
421 handle this differently, to avoid a shift/reduce conflict with
433 | unop_expr /* Must precede call_expr because of ambiguity with
441 '(' expr ',' maybe_expr_list ')'
444 error (_("Tuple expressions not supported yet"));
451 struct typed_val_int val;
454 = (language_lookup_primitive_type
455 (current_parser->language (), current_parser->arch (),
458 $$ = ast_literal (val);
462 /* To avoid a shift/reduce conflict with call_expr, we don't handle
463 tuple struct expressions here, but instead when examining the
466 path_for_expr '{' struct_expr_list '}'
467 { $$ = ast_struct ($1, $3); }
494 sf.init = ast_path ($1, NULL);
502 $$ = current_parser->new_set_vector ();
506 rust_set_vector *result = current_parser->new_set_vector ();
507 result->push_back ($1);
510 | IDENT ':' expr ',' struct_expr_list
519 | IDENT ',' struct_expr_list
524 sf.init = ast_path ($1, NULL);
531 '[' KW_MUT expr_list ']'
532 { $$ = ast_call_ish (OP_ARRAY, NULL, $3); }
534 { $$ = ast_call_ish (OP_ARRAY, NULL, $2); }
535 | '[' KW_MUT expr ';' expr ']'
536 { $$ = ast_operation (OP_RUST_ARRAY, $3, $5); }
537 | '[' expr ';' expr ']'
538 { $$ = ast_operation (OP_RUST_ARRAY, $2, $4); }
543 { $$ = ast_range ($1, NULL, false); }
545 { $$ = ast_range ($1, $3, false); }
547 { $$ = ast_range ($1, $3, true); }
549 { $$ = ast_range (NULL, $2, false); }
551 { $$ = ast_range (NULL, $2, true); }
553 { $$ = ast_range (NULL, NULL, false); }
558 { $$ = ast_literal ($1); }
560 { $$ = ast_literal ($1); }
562 { $$ = ast_dliteral ($1); }
565 const struct rust_op *str = ast_string ($1);
566 struct set_field field;
567 struct typed_val_int val;
570 rust_set_vector *fields = current_parser->new_set_vector ();
572 /* Wrap the raw string in the &str struct. */
573 field.name.ptr = "data_ptr";
574 field.name.length = strlen (field.name.ptr);
575 field.init = ast_unary (UNOP_ADDR, ast_string ($1));
576 fields->push_back (field);
578 val.type = rust_type ("usize");
581 field.name.ptr = "length";
582 field.name.length = strlen (field.name.ptr);
583 field.init = ast_literal (val);
584 fields->push_back (field);
587 token.length = strlen (token.ptr);
588 $$ = ast_struct (ast_path (token, NULL), fields);
591 { $$ = ast_string ($1); }
594 struct typed_val_int val;
596 val.type = language_bool_type (current_parser->language (),
597 current_parser->arch ());
599 $$ = ast_literal (val);
603 struct typed_val_int val;
605 val.type = language_bool_type (current_parser->language (),
606 current_parser->arch ());
608 $$ = ast_literal (val);
614 { $$ = ast_structop ($1, $3.ptr, 0); }
617 $$ = ast_structop ($1, $3.ptr, 1);
618 current_parser->rust_ast = $$;
620 | expr '.' DECIMAL_INTEGER
621 { $$ = ast_structop_anonymous ($1, $3); }
626 { $$ = ast_operation (BINOP_SUBSCRIPT, $1, $3); }
631 { $$ = ast_unary (UNOP_PLUS, $2); }
633 | '-' expr %prec UNARY
634 { $$ = ast_unary (UNOP_NEG, $2); }
636 | '!' expr %prec UNARY
638 /* Note that we provide a Rust-specific evaluator
639 override for UNOP_COMPLEMENT, so it can do the
640 right thing for both bool and integral
642 $$ = ast_unary (UNOP_COMPLEMENT, $2);
645 | '*' expr %prec UNARY
646 { $$ = ast_unary (UNOP_IND, $2); }
648 | '&' expr %prec UNARY
649 { $$ = ast_unary (UNOP_ADDR, $2); }
651 | '&' KW_MUT expr %prec UNARY
652 { $$ = ast_unary (UNOP_ADDR, $3); }
653 | KW_SIZEOF '(' expr ')' %prec UNARY
654 { $$ = ast_unary (UNOP_SIZEOF, $3); }
661 | compound_assignment_expr
666 { $$ = ast_operation (BINOP_MUL, $1, $3); }
669 { $$ = ast_operation (BINOP_REPEAT, $1, $3); }
672 { $$ = ast_operation (BINOP_DIV, $1, $3); }
675 { $$ = ast_operation (BINOP_REM, $1, $3); }
678 { $$ = ast_operation (BINOP_LESS, $1, $3); }
681 { $$ = ast_operation (BINOP_GTR, $1, $3); }
684 { $$ = ast_operation (BINOP_BITWISE_AND, $1, $3); }
687 { $$ = ast_operation (BINOP_BITWISE_IOR, $1, $3); }
690 { $$ = ast_operation (BINOP_BITWISE_XOR, $1, $3); }
693 { $$ = ast_operation (BINOP_ADD, $1, $3); }
696 { $$ = ast_operation (BINOP_SUB, $1, $3); }
699 { $$ = ast_operation (BINOP_LOGICAL_OR, $1, $3); }
702 { $$ = ast_operation (BINOP_LOGICAL_AND, $1, $3); }
705 { $$ = ast_operation (BINOP_EQUAL, $1, $3); }
708 { $$ = ast_operation (BINOP_NOTEQUAL, $1, $3); }
711 { $$ = ast_operation (BINOP_LEQ, $1, $3); }
714 { $$ = ast_operation (BINOP_GEQ, $1, $3); }
717 { $$ = ast_operation (BINOP_LSH, $1, $3); }
720 { $$ = ast_operation (BINOP_RSH, $1, $3); }
725 { $$ = ast_cast ($1, $3); }
730 { $$ = ast_operation (BINOP_ASSIGN, $1, $3); }
733 compound_assignment_expr:
734 expr COMPOUND_ASSIGN expr
735 { $$ = ast_compound_assignment ($2, $1, $3); }
747 $$ = current_parser->new_op_vector ();
760 /* The result can't be NULL. */
761 $$ = current_parser->new_op_vector ();
768 '(' maybe_expr_list ')'
774 { $$ = ast_call_ish (OP_FUNCALL, $1, $2); }
785 | super_path KW_SUPER COLONCOLON
793 { $$ = ast_path ($1, NULL); }
795 { $$ = ast_path (make_stoken ("self"), NULL); }
799 identifier_path_for_expr
800 | KW_SELF COLONCOLON identifier_path_for_expr
801 { $$ = super_name ($3, 0); }
802 | maybe_self_path super_path identifier_path_for_expr
803 { $$ = super_name ($3, $2); }
804 | COLONCOLON identifier_path_for_expr
805 { $$ = crate_name ($2); }
806 | KW_EXTERN identifier_path_for_expr
808 /* This is a gdb extension to make it possible to
809 refer to items in other crates. It just bypasses
810 adding the current crate to the front of the
812 $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL),
817 identifier_path_for_expr:
819 { $$ = ast_path ($1, NULL); }
820 | identifier_path_for_expr COLONCOLON IDENT
822 $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::",
826 | identifier_path_for_expr COLONCOLON '<' type_list '>'
827 { $$ = ast_path ($1->left.sval, $4); }
828 | identifier_path_for_expr COLONCOLON '<' type_list RSH
830 $$ = ast_path ($1->left.sval, $4);
831 rust_push_back ('>');
836 identifier_path_for_type
837 | KW_SELF COLONCOLON identifier_path_for_type
838 { $$ = super_name ($3, 0); }
839 | maybe_self_path super_path identifier_path_for_type
840 { $$ = super_name ($3, $2); }
841 | COLONCOLON identifier_path_for_type
842 { $$ = crate_name ($2); }
843 | KW_EXTERN identifier_path_for_type
845 /* This is a gdb extension to make it possible to
846 refer to items in other crates. It just bypasses
847 adding the current crate to the front of the
849 $$ = ast_path (rust_concat3 ("::", $2->left.sval.ptr, NULL),
854 just_identifiers_for_type:
856 { $$ = ast_path ($1, NULL); }
857 | just_identifiers_for_type COLONCOLON IDENT
859 $$ = ast_path (rust_concat3 ($1->left.sval.ptr, "::",
865 identifier_path_for_type:
866 just_identifiers_for_type
867 | just_identifiers_for_type '<' type_list '>'
868 { $$ = ast_path ($1->left.sval, $3); }
869 | just_identifiers_for_type '<' type_list RSH
871 $$ = ast_path ($1->left.sval, $3);
872 rust_push_back ('>');
878 | '[' type ';' INTEGER ']'
879 { $$ = ast_array_type ($2, $4); }
880 | '[' type ';' DECIMAL_INTEGER ']'
881 { $$ = ast_array_type ($2, $4); }
883 { $$ = ast_slice_type ($3); }
885 { $$ = ast_reference_type ($2); }
887 { $$ = ast_pointer_type ($3, 1); }
889 { $$ = ast_pointer_type ($3, 0); }
890 | KW_FN '(' maybe_type_list ')' ARROW type
891 { $$ = ast_function_type ($6, $3); }
892 | '(' maybe_type_list ')'
893 { $$ = ast_tuple_type ($2); }
906 rust_op_vector *result = current_parser->new_op_vector ();
907 result->push_back ($1);
919 /* A struct of this type is used to describe a token. */
925 enum exp_opcode opcode;
928 /* Identifier tokens. */
930 static const struct token_info identifier_tokens[] =
932 { "as", KW_AS, OP_NULL },
933 { "false", KW_FALSE, OP_NULL },
934 { "if", 0, OP_NULL },
935 { "mut", KW_MUT, OP_NULL },
936 { "const", KW_CONST, OP_NULL },
937 { "self", KW_SELF, OP_NULL },
938 { "super", KW_SUPER, OP_NULL },
939 { "true", KW_TRUE, OP_NULL },
940 { "extern", KW_EXTERN, OP_NULL },
941 { "fn", KW_FN, OP_NULL },
942 { "sizeof", KW_SIZEOF, OP_NULL },
945 /* Operator tokens, sorted longest first. */
947 static const struct token_info operator_tokens[] =
949 { ">>=", COMPOUND_ASSIGN, BINOP_RSH },
950 { "<<=", COMPOUND_ASSIGN, BINOP_LSH },
952 { "<<", LSH, OP_NULL },
953 { ">>", RSH, OP_NULL },
954 { "&&", ANDAND, OP_NULL },
955 { "||", OROR, OP_NULL },
956 { "==", EQEQ, OP_NULL },
957 { "!=", NOTEQ, OP_NULL },
958 { "<=", LTEQ, OP_NULL },
959 { ">=", GTEQ, OP_NULL },
960 { "+=", COMPOUND_ASSIGN, BINOP_ADD },
961 { "-=", COMPOUND_ASSIGN, BINOP_SUB },
962 { "*=", COMPOUND_ASSIGN, BINOP_MUL },
963 { "/=", COMPOUND_ASSIGN, BINOP_DIV },
964 { "%=", COMPOUND_ASSIGN, BINOP_REM },
965 { "&=", COMPOUND_ASSIGN, BINOP_BITWISE_AND },
966 { "|=", COMPOUND_ASSIGN, BINOP_BITWISE_IOR },
967 { "^=", COMPOUND_ASSIGN, BINOP_BITWISE_XOR },
968 { "..=", DOTDOTEQ, OP_NULL },
970 { "::", COLONCOLON, OP_NULL },
971 { "..", DOTDOT, OP_NULL },
972 { "->", ARROW, OP_NULL }
975 /* Helper function to copy to the name obstack. */
978 rust_copy_name (const char *name, int len)
980 return (const char *) obstack_copy0 (work_obstack, name, len);
983 /* Helper function to make an stoken from a C string. */
986 make_stoken (const char *p)
988 struct stoken result;
991 result.length = strlen (result.ptr);
995 /* Helper function to concatenate three strings on the name
999 rust_concat3 (const char *s1, const char *s2, const char *s3)
1001 return make_stoken (obconcat (work_obstack, s1, s2, s3, (char *) NULL));
1004 /* Return an AST node referring to NAME, but relative to the crate's
1007 static const struct rust_op *
1008 crate_name (const struct rust_op *name)
1010 std::string crate = rust_crate_for_block (expression_context_block);
1011 struct stoken result;
1013 gdb_assert (name->opcode == OP_VAR_VALUE);
1016 error (_("Could not find crate for current location"));
1017 result = make_stoken (obconcat (work_obstack, "::", crate.c_str (), "::",
1018 name->left.sval.ptr, (char *) NULL));
1020 return ast_path (result, name->right.params);
1023 /* Create an AST node referring to a "super::" qualified name. IDENT
1024 is the base name and N_SUPERS is how many "super::"s were
1025 provided. N_SUPERS can be zero. */
1027 static const struct rust_op *
1028 super_name (const struct rust_op *ident, unsigned int n_supers)
1030 const char *scope = block_scope (expression_context_block);
1033 gdb_assert (ident->opcode == OP_VAR_VALUE);
1035 if (scope[0] == '\0')
1036 error (_("Couldn't find namespace scope for self::"));
1041 std::vector<int> offsets;
1042 unsigned int current_len;
1044 current_len = cp_find_first_component (scope);
1045 while (scope[current_len] != '\0')
1047 offsets.push_back (current_len);
1048 gdb_assert (scope[current_len] == ':');
1051 current_len += cp_find_first_component (scope
1055 len = offsets.size ();
1056 if (n_supers >= len)
1057 error (_("Too many super:: uses from '%s'"), scope);
1059 offset = offsets[len - n_supers];
1062 offset = strlen (scope);
1064 obstack_grow (work_obstack, "::", 2);
1065 obstack_grow (work_obstack, scope, offset);
1066 obstack_grow (work_obstack, "::", 2);
1067 obstack_grow0 (work_obstack, ident->left.sval.ptr, ident->left.sval.length);
1069 return ast_path (make_stoken ((const char *) obstack_finish (work_obstack)),
1070 ident->right.params);
1073 /* A helper that updates the innermost block as appropriate. */
1076 update_innermost_block (struct block_symbol sym)
1078 if (symbol_read_needs_frame (sym.symbol))
1079 innermost_block.update (sym);
1082 /* A helper to look up a Rust type, or fail. This only works for
1083 types defined by rust_language_arch_info. */
1085 static struct type *
1086 rust_type (const char *name)
1090 type = language_lookup_primitive_type (current_parser->language (),
1091 current_parser->arch (),
1094 error (_("Could not find Rust type %s"), name);
1098 /* Lex a hex number with at least MIN digits and at most MAX
1102 lex_hex (int min, int max)
1104 uint32_t result = 0;
1106 /* We only want to stop at MAX if we're lexing a byte escape. */
1107 int check_max = min == max;
1109 while ((check_max ? len <= max : 1)
1110 && ((lexptr[0] >= 'a' && lexptr[0] <= 'f')
1111 || (lexptr[0] >= 'A' && lexptr[0] <= 'F')
1112 || (lexptr[0] >= '0' && lexptr[0] <= '9')))
1115 if (lexptr[0] >= 'a' && lexptr[0] <= 'f')
1116 result = result + 10 + lexptr[0] - 'a';
1117 else if (lexptr[0] >= 'A' && lexptr[0] <= 'F')
1118 result = result + 10 + lexptr[0] - 'A';
1120 result = result + lexptr[0] - '0';
1126 error (_("Not enough hex digits seen"));
1129 gdb_assert (min != max);
1130 error (_("Overlong hex escape"));
1136 /* Lex an escape. IS_BYTE is true if we're lexing a byte escape;
1137 otherwise we're lexing a character escape. */
1140 lex_escape (int is_byte)
1144 gdb_assert (lexptr[0] == '\\');
1150 result = lex_hex (2, 2);
1155 error (_("Unicode escape in byte literal"));
1157 if (lexptr[0] != '{')
1158 error (_("Missing '{' in Unicode escape"));
1160 result = lex_hex (1, 6);
1161 /* Could do range checks here. */
1162 if (lexptr[0] != '}')
1163 error (_("Missing '}' in Unicode escape"));
1197 error (_("Invalid escape \\%c in literal"), lexptr[0]);
1203 /* Lex a character constant. */
1206 lex_character (void)
1211 if (lexptr[0] == 'b')
1216 gdb_assert (lexptr[0] == '\'');
1218 /* This should handle UTF-8 here. */
1219 if (lexptr[0] == '\\')
1220 value = lex_escape (is_byte);
1223 value = lexptr[0] & 0xff;
1227 if (lexptr[0] != '\'')
1228 error (_("Unterminated character literal"));
1231 rustyylval.typed_val_int.val = value;
1232 rustyylval.typed_val_int.type = rust_type (is_byte ? "u8" : "char");
1237 /* Return the offset of the double quote if STR looks like the start
1238 of a raw string, or 0 if STR does not start a raw string. */
1241 starts_raw_string (const char *str)
1243 const char *save = str;
1248 while (str[0] == '#')
1255 /* Return true if STR looks like the end of a raw string that had N
1256 hashes at the start. */
1259 ends_raw_string (const char *str, int n)
1263 gdb_assert (str[0] == '"');
1264 for (i = 0; i < n; ++i)
1265 if (str[i + 1] != '#')
1270 /* Lex a string constant. */
1275 int is_byte = lexptr[0] == 'b';
1280 raw_length = starts_raw_string (lexptr);
1281 lexptr += raw_length;
1282 gdb_assert (lexptr[0] == '"');
1291 if (lexptr[0] == '"' && ends_raw_string (lexptr, raw_length - 1))
1293 /* Exit with lexptr pointing after the final "#". */
1294 lexptr += raw_length;
1297 else if (lexptr[0] == '\0')
1298 error (_("Unexpected EOF in string"));
1300 value = lexptr[0] & 0xff;
1301 if (is_byte && value > 127)
1302 error (_("Non-ASCII value in raw byte string"));
1303 obstack_1grow (work_obstack, value);
1307 else if (lexptr[0] == '"')
1309 /* Make sure to skip the quote. */
1313 else if (lexptr[0] == '\\')
1315 value = lex_escape (is_byte);
1318 obstack_1grow (work_obstack, value);
1320 convert_between_encodings ("UTF-32", "UTF-8", (gdb_byte *) &value,
1321 sizeof (value), sizeof (value),
1322 work_obstack, translit_none);
1324 else if (lexptr[0] == '\0')
1325 error (_("Unexpected EOF in string"));
1328 value = lexptr[0] & 0xff;
1329 if (is_byte && value > 127)
1330 error (_("Non-ASCII value in byte string"));
1331 obstack_1grow (work_obstack, value);
1336 rustyylval.sval.length = obstack_object_size (work_obstack);
1337 rustyylval.sval.ptr = (const char *) obstack_finish (work_obstack);
1338 return is_byte ? BYTESTRING : STRING;
1341 /* Return true if STRING starts with whitespace followed by a digit. */
1344 space_then_number (const char *string)
1346 const char *p = string;
1348 while (p[0] == ' ' || p[0] == '\t')
1353 return *p >= '0' && *p <= '9';
1356 /* Return true if C can start an identifier. */
1359 rust_identifier_start_p (char c)
1361 return ((c >= 'a' && c <= 'z')
1362 || (c >= 'A' && c <= 'Z')
1367 /* Lex an identifier. */
1370 lex_identifier (void)
1372 const char *start = lexptr;
1373 unsigned int length;
1374 const struct token_info *token;
1376 int is_gdb_var = lexptr[0] == '$';
1378 gdb_assert (rust_identifier_start_p (lexptr[0]));
1382 /* For the time being this doesn't handle Unicode rules. Non-ASCII
1383 identifiers are gated anyway. */
1384 while ((lexptr[0] >= 'a' && lexptr[0] <= 'z')
1385 || (lexptr[0] >= 'A' && lexptr[0] <= 'Z')
1387 || (is_gdb_var && lexptr[0] == '$')
1388 || (lexptr[0] >= '0' && lexptr[0] <= '9'))
1392 length = lexptr - start;
1394 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
1396 if (length == strlen (identifier_tokens[i].name)
1397 && strncmp (identifier_tokens[i].name, start, length) == 0)
1399 token = &identifier_tokens[i];
1406 if (token->value == 0)
1408 /* Leave the terminating token alone. */
1413 else if (token == NULL
1414 && (strncmp (start, "thread", length) == 0
1415 || strncmp (start, "task", length) == 0)
1416 && space_then_number (lexptr))
1418 /* "task" or "thread" followed by a number terminates the
1419 parse, per gdb rules. */
1424 if (token == NULL || (parse_completion && lexptr[0] == '\0'))
1425 rustyylval.sval = make_stoken (rust_copy_name (start, length));
1427 if (parse_completion && lexptr[0] == '\0')
1429 /* Prevent rustyylex from returning two COMPLETE tokens. */
1430 prev_lexptr = lexptr;
1435 return token->value;
1441 /* Lex an operator. */
1446 const struct token_info *token = NULL;
1449 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
1451 if (strncmp (operator_tokens[i].name, lexptr,
1452 strlen (operator_tokens[i].name)) == 0)
1454 lexptr += strlen (operator_tokens[i].name);
1455 token = &operator_tokens[i];
1462 rustyylval.opcode = token->opcode;
1463 return token->value;
1474 regmatch_t subexps[NUM_SUBEXPRESSIONS];
1477 int could_be_decimal = 1;
1478 int implicit_i32 = 0;
1479 const char *type_name = NULL;
1482 int type_index = -1;
1485 match = regexec (&number_regex, lexptr, ARRAY_SIZE (subexps), subexps, 0);
1486 /* Failure means the regexp is broken. */
1487 gdb_assert (match == 0);
1489 if (subexps[INT_TEXT].rm_so != -1)
1491 /* Integer part matched. */
1493 end_index = subexps[INT_TEXT].rm_eo;
1494 if (subexps[INT_TYPE].rm_so == -1)
1501 type_index = INT_TYPE;
1502 could_be_decimal = 0;
1505 else if (subexps[FLOAT_TYPE1].rm_so != -1)
1507 /* Found floating point type suffix. */
1508 end_index = subexps[FLOAT_TYPE1].rm_so;
1509 type_index = FLOAT_TYPE1;
1511 else if (subexps[FLOAT_TYPE2].rm_so != -1)
1513 /* Found floating point type suffix. */
1514 end_index = subexps[FLOAT_TYPE2].rm_so;
1515 type_index = FLOAT_TYPE2;
1519 /* Any other floating point match. */
1520 end_index = subexps[0].rm_eo;
1524 /* We need a special case if the final character is ".". In this
1525 case we might need to parse an integer. For example, "23.f()" is
1526 a request for a trait method call, not a syntax error involving
1527 the floating point number "23.". */
1528 gdb_assert (subexps[0].rm_eo > 0);
1529 if (lexptr[subexps[0].rm_eo - 1] == '.')
1531 const char *next = skip_spaces (&lexptr[subexps[0].rm_eo]);
1533 if (rust_identifier_start_p (*next) || *next == '.')
1537 end_index = subexps[0].rm_eo;
1539 could_be_decimal = 1;
1544 /* Compute the type name if we haven't already. */
1545 std::string type_name_holder;
1546 if (type_name == NULL)
1548 gdb_assert (type_index != -1);
1549 type_name_holder = std::string (lexptr + subexps[type_index].rm_so,
1550 (subexps[type_index].rm_eo
1551 - subexps[type_index].rm_so));
1552 type_name = type_name_holder.c_str ();
1555 /* Look up the type. */
1556 type = rust_type (type_name);
1558 /* Copy the text of the number and remove the "_"s. */
1560 for (i = 0; i < end_index && lexptr[i]; ++i)
1562 if (lexptr[i] == '_')
1563 could_be_decimal = 0;
1565 number.push_back (lexptr[i]);
1568 /* Advance past the match. */
1569 lexptr += subexps[0].rm_eo;
1571 /* Parse the number. */
1578 if (number[0] == '0')
1580 if (number[1] == 'x')
1582 else if (number[1] == 'o')
1584 else if (number[1] == 'b')
1589 could_be_decimal = 0;
1593 value = strtoul (number.c_str () + offset, NULL, radix);
1594 if (implicit_i32 && value >= ((uint64_t) 1) << 31)
1595 type = rust_type ("i64");
1597 rustyylval.typed_val_int.val = value;
1598 rustyylval.typed_val_int.type = type;
1602 rustyylval.typed_val_float.type = type;
1603 bool parsed = parse_float (number.c_str (), number.length (),
1604 rustyylval.typed_val_float.type,
1605 rustyylval.typed_val_float.val);
1606 gdb_assert (parsed);
1609 return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
1617 /* Skip all leading whitespace. */
1618 while (lexptr[0] == ' ' || lexptr[0] == '\t' || lexptr[0] == '\r'
1619 || lexptr[0] == '\n')
1622 /* If we hit EOF and we're completing, then return COMPLETE -- maybe
1623 we're completing an empty string at the end of a field_expr.
1624 But, we don't want to return two COMPLETE tokens in a row. */
1625 if (lexptr[0] == '\0' && lexptr == prev_lexptr)
1627 prev_lexptr = lexptr;
1628 if (lexptr[0] == '\0')
1630 if (parse_completion)
1632 rustyylval.sval = make_stoken ("");
1638 if (lexptr[0] >= '0' && lexptr[0] <= '9')
1639 return lex_number ();
1640 else if (lexptr[0] == 'b' && lexptr[1] == '\'')
1641 return lex_character ();
1642 else if (lexptr[0] == 'b' && lexptr[1] == '"')
1643 return lex_string ();
1644 else if (lexptr[0] == 'b' && starts_raw_string (lexptr + 1))
1645 return lex_string ();
1646 else if (starts_raw_string (lexptr))
1647 return lex_string ();
1648 else if (rust_identifier_start_p (lexptr[0]))
1649 return lex_identifier ();
1650 else if (lexptr[0] == '"')
1651 return lex_string ();
1652 else if (lexptr[0] == '\'')
1653 return lex_character ();
1654 else if (lexptr[0] == '}' || lexptr[0] == ']')
1656 /* Falls through to lex_operator. */
1659 else if (lexptr[0] == '(' || lexptr[0] == '{')
1661 /* Falls through to lex_operator. */
1664 else if (lexptr[0] == ',' && comma_terminates && paren_depth == 0)
1667 return lex_operator ();
1670 /* Push back a single character to be re-lexed. */
1673 rust_push_back (char c)
1675 /* Can't be called before any lexing. */
1676 gdb_assert (prev_lexptr != NULL);
1679 gdb_assert (*lexptr == c);
1684 /* Make an arbitrary operation and fill in the fields. */
1686 static const struct rust_op *
1687 ast_operation (enum exp_opcode opcode, const struct rust_op *left,
1688 const struct rust_op *right)
1690 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1692 result->opcode = opcode;
1693 result->left.op = left;
1694 result->right.op = right;
1699 /* Make a compound assignment operation. */
1701 static const struct rust_op *
1702 ast_compound_assignment (enum exp_opcode opcode, const struct rust_op *left,
1703 const struct rust_op *right)
1705 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1707 result->opcode = opcode;
1708 result->compound_assignment = 1;
1709 result->left.op = left;
1710 result->right.op = right;
1715 /* Make a typed integer literal operation. */
1717 static const struct rust_op *
1718 ast_literal (struct typed_val_int val)
1720 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1722 result->opcode = OP_LONG;
1723 result->left.typed_val_int = val;
1728 /* Make a typed floating point literal operation. */
1730 static const struct rust_op *
1731 ast_dliteral (struct typed_val_float val)
1733 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1735 result->opcode = OP_FLOAT;
1736 result->left.typed_val_float = val;
1741 /* Make a unary operation. */
1743 static const struct rust_op *
1744 ast_unary (enum exp_opcode opcode, const struct rust_op *expr)
1746 return ast_operation (opcode, expr, NULL);
1749 /* Make a cast operation. */
1751 static const struct rust_op *
1752 ast_cast (const struct rust_op *expr, const struct rust_op *type)
1754 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1756 result->opcode = UNOP_CAST;
1757 result->left.op = expr;
1758 result->right.op = type;
1763 /* Make a call-like operation. This is nominally a function call, but
1764 when lowering we may discover that it actually represents the
1765 creation of a tuple struct. */
1767 static const struct rust_op *
1768 ast_call_ish (enum exp_opcode opcode, const struct rust_op *expr,
1769 rust_op_vector *params)
1771 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1773 result->opcode = opcode;
1774 result->left.op = expr;
1775 result->right.params = params;
1780 /* Make a structure creation operation. */
1782 static const struct rust_op *
1783 ast_struct (const struct rust_op *name, rust_set_vector *fields)
1785 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1787 result->opcode = OP_AGGREGATE;
1788 result->left.op = name;
1789 result->right.field_inits = fields;
1794 /* Make an identifier path. */
1796 static const struct rust_op *
1797 ast_path (struct stoken path, rust_op_vector *params)
1799 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1801 result->opcode = OP_VAR_VALUE;
1802 result->left.sval = path;
1803 result->right.params = params;
1808 /* Make a string constant operation. */
1810 static const struct rust_op *
1811 ast_string (struct stoken str)
1813 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1815 result->opcode = OP_STRING;
1816 result->left.sval = str;
1821 /* Make a field expression. */
1823 static const struct rust_op *
1824 ast_structop (const struct rust_op *left, const char *name, int completing)
1826 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1828 result->opcode = STRUCTOP_STRUCT;
1829 result->completing = completing;
1830 result->left.op = left;
1831 result->right.sval = make_stoken (name);
1836 /* Make an anonymous struct operation, like 'x.0'. */
1838 static const struct rust_op *
1839 ast_structop_anonymous (const struct rust_op *left,
1840 struct typed_val_int number)
1842 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1844 result->opcode = STRUCTOP_ANONYMOUS;
1845 result->left.op = left;
1846 result->right.typed_val_int = number;
1851 /* Make a range operation. */
1853 static const struct rust_op *
1854 ast_range (const struct rust_op *lhs, const struct rust_op *rhs,
1857 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1859 result->opcode = OP_RANGE;
1860 result->inclusive = inclusive;
1861 result->left.op = lhs;
1862 result->right.op = rhs;
1867 /* A helper function to make a type-related AST node. */
1869 static struct rust_op *
1870 ast_basic_type (enum type_code typecode)
1872 struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
1874 result->opcode = OP_TYPE;
1875 result->typecode = typecode;
1879 /* Create an AST node describing an array type. */
1881 static const struct rust_op *
1882 ast_array_type (const struct rust_op *lhs, struct typed_val_int val)
1884 struct rust_op *result = ast_basic_type (TYPE_CODE_ARRAY);
1886 result->left.op = lhs;
1887 result->right.typed_val_int = val;
1891 /* Create an AST node describing a reference type. */
1893 static const struct rust_op *
1894 ast_slice_type (const struct rust_op *type)
1896 /* Use TYPE_CODE_COMPLEX just because it is handy. */
1897 struct rust_op *result = ast_basic_type (TYPE_CODE_COMPLEX);
1899 result->left.op = type;
1903 /* Create an AST node describing a reference type. */
1905 static const struct rust_op *
1906 ast_reference_type (const struct rust_op *type)
1908 struct rust_op *result = ast_basic_type (TYPE_CODE_REF);
1910 result->left.op = type;
1914 /* Create an AST node describing a pointer type. */
1916 static const struct rust_op *
1917 ast_pointer_type (const struct rust_op *type, int is_mut)
1919 struct rust_op *result = ast_basic_type (TYPE_CODE_PTR);
1921 result->left.op = type;
1922 /* For the time being we ignore is_mut. */
1926 /* Create an AST node describing a function type. */
1928 static const struct rust_op *
1929 ast_function_type (const struct rust_op *rtype, rust_op_vector *params)
1931 struct rust_op *result = ast_basic_type (TYPE_CODE_FUNC);
1933 result->left.op = rtype;
1934 result->right.params = params;
1938 /* Create an AST node describing a tuple type. */
1940 static const struct rust_op *
1941 ast_tuple_type (rust_op_vector *params)
1943 struct rust_op *result = ast_basic_type (TYPE_CODE_STRUCT);
1945 result->left.params = params;
1949 /* A helper to appropriately munge NAME and BLOCK depending on the
1950 presence of a leading "::". */
1953 munge_name_and_block (const char **name, const struct block **block)
1955 /* If it is a global reference, skip the current block in favor of
1956 the static block. */
1957 if (strncmp (*name, "::", 2) == 0)
1960 *block = block_static_block (*block);
1964 /* Like lookup_symbol, but handles Rust namespace conventions, and
1965 doesn't require field_of_this_result. */
1967 static struct block_symbol
1968 rust_lookup_symbol (const char *name, const struct block *block,
1969 const domain_enum domain)
1971 struct block_symbol result;
1973 munge_name_and_block (&name, &block);
1975 result = lookup_symbol (name, block, domain, NULL);
1976 if (result.symbol != NULL)
1977 update_innermost_block (result);
1981 /* Look up a type, following Rust namespace conventions. */
1983 static struct type *
1984 rust_lookup_type (const char *name, const struct block *block)
1986 struct block_symbol result;
1989 munge_name_and_block (&name, &block);
1991 result = lookup_symbol (name, block, STRUCT_DOMAIN, NULL);
1992 if (result.symbol != NULL)
1994 update_innermost_block (result);
1995 return SYMBOL_TYPE (result.symbol);
1998 type = lookup_typename (current_parser->language (), current_parser->arch (),
2003 /* Last chance, try a built-in type. */
2004 return language_lookup_primitive_type (current_parser->language (),
2005 current_parser->arch (),
2009 static struct type *convert_ast_to_type (struct parser_state *state,
2010 const struct rust_op *operation);
2011 static const char *convert_name (struct parser_state *state,
2012 const struct rust_op *operation);
2014 /* Convert a vector of rust_ops representing types to a vector of
2017 static std::vector<struct type *>
2018 convert_params_to_types (struct parser_state *state, rust_op_vector *params)
2020 std::vector<struct type *> result;
2022 if (params != nullptr)
2024 for (const rust_op *op : *params)
2025 result.push_back (convert_ast_to_type (state, op));
2031 /* Convert a rust_op representing a type to a struct type *. */
2033 static struct type *
2034 convert_ast_to_type (struct parser_state *state,
2035 const struct rust_op *operation)
2037 struct type *type, *result = NULL;
2039 if (operation->opcode == OP_VAR_VALUE)
2041 const char *varname = convert_name (state, operation);
2043 result = rust_lookup_type (varname, expression_context_block);
2045 error (_("No typed name '%s' in current context"), varname);
2049 gdb_assert (operation->opcode == OP_TYPE);
2051 switch (operation->typecode)
2053 case TYPE_CODE_ARRAY:
2054 type = convert_ast_to_type (state, operation->left.op);
2055 if (operation->right.typed_val_int.val < 0)
2056 error (_("Negative array length"));
2057 result = lookup_array_range_type (type, 0,
2058 operation->right.typed_val_int.val - 1);
2061 case TYPE_CODE_COMPLEX:
2063 struct type *usize = rust_type ("usize");
2065 type = convert_ast_to_type (state, operation->left.op);
2066 result = rust_slice_type ("&[*gdb*]", type, usize);
2072 /* For now we treat &x and *x identically. */
2073 type = convert_ast_to_type (state, operation->left.op);
2074 result = lookup_pointer_type (type);
2077 case TYPE_CODE_FUNC:
2079 std::vector<struct type *> args
2080 (convert_params_to_types (state, operation->right.params));
2081 struct type **argtypes = NULL;
2083 type = convert_ast_to_type (state, operation->left.op);
2085 argtypes = args.data ();
2088 = lookup_function_type_with_arguments (type, args.size (),
2090 result = lookup_pointer_type (result);
2094 case TYPE_CODE_STRUCT:
2096 std::vector<struct type *> args
2097 (convert_params_to_types (state, operation->left.params));
2101 obstack_1grow (work_obstack, '(');
2102 for (i = 0; i < args.size (); ++i)
2104 std::string type_name = type_to_string (args[i]);
2107 obstack_1grow (work_obstack, ',');
2108 obstack_grow_str (work_obstack, type_name.c_str ());
2111 obstack_grow_str0 (work_obstack, ")");
2112 name = (const char *) obstack_finish (work_obstack);
2114 /* We don't allow creating new tuple types (yet), but we do
2115 allow looking up existing tuple types. */
2116 result = rust_lookup_type (name, expression_context_block);
2118 error (_("could not find tuple type '%s'"), name);
2123 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_type");
2126 gdb_assert (result != NULL);
2130 /* A helper function to turn a rust_op representing a name into a full
2131 name. This applies generic arguments as needed. The returned name
2132 is allocated on the work obstack. */
2135 convert_name (struct parser_state *state, const struct rust_op *operation)
2139 gdb_assert (operation->opcode == OP_VAR_VALUE);
2141 if (operation->right.params == NULL)
2142 return operation->left.sval.ptr;
2144 std::vector<struct type *> types
2145 (convert_params_to_types (state, operation->right.params));
2147 obstack_grow_str (work_obstack, operation->left.sval.ptr);
2148 obstack_1grow (work_obstack, '<');
2149 for (i = 0; i < types.size (); ++i)
2151 std::string type_name = type_to_string (types[i]);
2154 obstack_1grow (work_obstack, ',');
2156 obstack_grow_str (work_obstack, type_name.c_str ());
2158 obstack_grow_str0 (work_obstack, ">");
2160 return (const char *) obstack_finish (work_obstack);
2163 static void convert_ast_to_expression (struct parser_state *state,
2164 const struct rust_op *operation,
2165 const struct rust_op *top,
2166 bool want_type = false);
2168 /* A helper function that converts a vec of rust_ops to a gdb
2172 convert_params_to_expression (struct parser_state *state,
2173 rust_op_vector *params,
2174 const struct rust_op *top)
2176 for (const rust_op *elem : *params)
2177 convert_ast_to_expression (state, elem, top);
2180 /* Lower a rust_op to a gdb expression. STATE is the parser state.
2181 OPERATION is the operation to lower. TOP is a pointer to the
2182 top-most operation; it is used to handle the special case where the
2183 top-most expression is an identifier and can be optionally lowered
2184 to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression
2185 is the name of a type, then emit an OP_TYPE for it (rather than
2186 erroring). If WANT_TYPE is set, then the similar TOP handling is
2190 convert_ast_to_expression (struct parser_state *state,
2191 const struct rust_op *operation,
2192 const struct rust_op *top,
2195 switch (operation->opcode)
2198 write_exp_elt_opcode (state, OP_LONG);
2199 write_exp_elt_type (state, operation->left.typed_val_int.type);
2200 write_exp_elt_longcst (state, operation->left.typed_val_int.val);
2201 write_exp_elt_opcode (state, OP_LONG);
2205 write_exp_elt_opcode (state, OP_FLOAT);
2206 write_exp_elt_type (state, operation->left.typed_val_float.type);
2207 write_exp_elt_floatcst (state, operation->left.typed_val_float.val);
2208 write_exp_elt_opcode (state, OP_FLOAT);
2211 case STRUCTOP_STRUCT:
2213 convert_ast_to_expression (state, operation->left.op, top);
2215 if (operation->completing)
2216 mark_struct_expression (state);
2217 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2218 write_exp_string (state, operation->right.sval);
2219 write_exp_elt_opcode (state, STRUCTOP_STRUCT);
2223 case STRUCTOP_ANONYMOUS:
2225 convert_ast_to_expression (state, operation->left.op, top);
2227 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2228 write_exp_elt_longcst (state, operation->right.typed_val_int.val);
2229 write_exp_elt_opcode (state, STRUCTOP_ANONYMOUS);
2234 convert_ast_to_expression (state, operation->left.op, top, true);
2235 write_exp_elt_opcode (state, UNOP_SIZEOF);
2240 case UNOP_COMPLEMENT:
2243 convert_ast_to_expression (state, operation->left.op, top);
2244 write_exp_elt_opcode (state, operation->opcode);
2247 case BINOP_SUBSCRIPT:
2254 case BINOP_BITWISE_AND:
2255 case BINOP_BITWISE_IOR:
2256 case BINOP_BITWISE_XOR:
2259 case BINOP_LOGICAL_OR:
2260 case BINOP_LOGICAL_AND:
2262 case BINOP_NOTEQUAL:
2269 convert_ast_to_expression (state, operation->left.op, top);
2270 convert_ast_to_expression (state, operation->right.op, top);
2271 if (operation->compound_assignment)
2273 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2274 write_exp_elt_opcode (state, operation->opcode);
2275 write_exp_elt_opcode (state, BINOP_ASSIGN_MODIFY);
2278 write_exp_elt_opcode (state, operation->opcode);
2280 if (operation->compound_assignment
2281 || operation->opcode == BINOP_ASSIGN)
2285 type = language_lookup_primitive_type (parse_language (state),
2286 parse_gdbarch (state),
2289 write_exp_elt_opcode (state, OP_LONG);
2290 write_exp_elt_type (state, type);
2291 write_exp_elt_longcst (state, 0);
2292 write_exp_elt_opcode (state, OP_LONG);
2294 write_exp_elt_opcode (state, BINOP_COMMA);
2300 struct type *type = convert_ast_to_type (state, operation->right.op);
2302 convert_ast_to_expression (state, operation->left.op, top);
2303 write_exp_elt_opcode (state, UNOP_CAST);
2304 write_exp_elt_type (state, type);
2305 write_exp_elt_opcode (state, UNOP_CAST);
2311 if (operation->left.op->opcode == OP_VAR_VALUE)
2314 const char *varname = convert_name (state, operation->left.op);
2316 type = rust_lookup_type (varname, expression_context_block);
2319 /* This is actually a tuple struct expression, not a
2321 rust_op_vector *params = operation->right.params;
2323 if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
2325 if (!rust_tuple_struct_type_p (type))
2326 error (_("Type %s is not a tuple struct"), varname);
2328 for (int i = 0; i < params->size (); ++i)
2330 char *cell = get_print_cell ();
2332 xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i);
2333 write_exp_elt_opcode (state, OP_NAME);
2334 write_exp_string (state, make_stoken (cell));
2335 write_exp_elt_opcode (state, OP_NAME);
2337 convert_ast_to_expression (state, (*params)[i], top);
2340 write_exp_elt_opcode (state, OP_AGGREGATE);
2341 write_exp_elt_type (state, type);
2342 write_exp_elt_longcst (state, 2 * params->size ());
2343 write_exp_elt_opcode (state, OP_AGGREGATE);
2348 convert_ast_to_expression (state, operation->left.op, top);
2349 convert_params_to_expression (state, operation->right.params, top);
2350 write_exp_elt_opcode (state, OP_FUNCALL);
2351 write_exp_elt_longcst (state, operation->right.params->size ());
2352 write_exp_elt_longcst (state, OP_FUNCALL);
2357 gdb_assert (operation->left.op == NULL);
2358 convert_params_to_expression (state, operation->right.params, top);
2359 write_exp_elt_opcode (state, OP_ARRAY);
2360 write_exp_elt_longcst (state, 0);
2361 write_exp_elt_longcst (state, operation->right.params->size () - 1);
2362 write_exp_elt_longcst (state, OP_ARRAY);
2367 struct block_symbol sym;
2368 const char *varname;
2370 if (operation->left.sval.ptr[0] == '$')
2372 write_dollar_variable (state, operation->left.sval);
2376 varname = convert_name (state, operation);
2377 sym = rust_lookup_symbol (varname, expression_context_block,
2379 if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
2381 write_exp_elt_opcode (state, OP_VAR_VALUE);
2382 write_exp_elt_block (state, sym.block);
2383 write_exp_elt_sym (state, sym.symbol);
2384 write_exp_elt_opcode (state, OP_VAR_VALUE);
2388 struct type *type = NULL;
2390 if (sym.symbol != NULL)
2392 gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF);
2393 type = SYMBOL_TYPE (sym.symbol);
2396 type = rust_lookup_type (varname, expression_context_block);
2398 error (_("No symbol '%s' in current context"), varname);
2401 && TYPE_CODE (type) == TYPE_CODE_STRUCT
2402 && TYPE_NFIELDS (type) == 0)
2404 /* A unit-like struct. */
2405 write_exp_elt_opcode (state, OP_AGGREGATE);
2406 write_exp_elt_type (state, type);
2407 write_exp_elt_longcst (state, 0);
2408 write_exp_elt_opcode (state, OP_AGGREGATE);
2410 else if (want_type || operation == top)
2412 write_exp_elt_opcode (state, OP_TYPE);
2413 write_exp_elt_type (state, type);
2414 write_exp_elt_opcode (state, OP_TYPE);
2417 error (_("Found type '%s', which can't be "
2418 "evaluated in this context"),
2427 rust_set_vector *fields = operation->right.field_inits;
2432 for (const set_field &init : *fields)
2434 if (init.name.ptr != NULL)
2436 write_exp_elt_opcode (state, OP_NAME);
2437 write_exp_string (state, init.name);
2438 write_exp_elt_opcode (state, OP_NAME);
2442 convert_ast_to_expression (state, init.init, top);
2445 if (init.name.ptr == NULL)
2447 /* This is handled differently from Ada in our
2449 write_exp_elt_opcode (state, OP_OTHERS);
2453 name = convert_name (state, operation->left.op);
2454 type = rust_lookup_type (name, expression_context_block);
2456 error (_("Could not find type '%s'"), operation->left.sval.ptr);
2458 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
2459 || rust_tuple_type_p (type)
2460 || rust_tuple_struct_type_p (type))
2461 error (_("Struct expression applied to non-struct type"));
2463 write_exp_elt_opcode (state, OP_AGGREGATE);
2464 write_exp_elt_type (state, type);
2465 write_exp_elt_longcst (state, length);
2466 write_exp_elt_opcode (state, OP_AGGREGATE);
2472 write_exp_elt_opcode (state, OP_STRING);
2473 write_exp_string (state, operation->left.sval);
2474 write_exp_elt_opcode (state, OP_STRING);
2480 enum range_type kind = BOTH_BOUND_DEFAULT;
2482 if (operation->left.op != NULL)
2484 convert_ast_to_expression (state, operation->left.op, top);
2485 kind = HIGH_BOUND_DEFAULT;
2487 if (operation->right.op != NULL)
2489 convert_ast_to_expression (state, operation->right.op, top);
2490 if (kind == BOTH_BOUND_DEFAULT)
2491 kind = (operation->inclusive
2492 ? LOW_BOUND_DEFAULT : LOW_BOUND_DEFAULT_EXCLUSIVE);
2495 gdb_assert (kind == HIGH_BOUND_DEFAULT);
2496 kind = (operation->inclusive
2497 ? NONE_BOUND_DEFAULT : NONE_BOUND_DEFAULT_EXCLUSIVE);
2502 /* Nothing should make an inclusive range without an upper
2504 gdb_assert (!operation->inclusive);
2507 write_exp_elt_opcode (state, OP_RANGE);
2508 write_exp_elt_longcst (state, kind);
2509 write_exp_elt_opcode (state, OP_RANGE);
2514 gdb_assert_not_reached ("unhandled opcode in convert_ast_to_expression");
2520 /* The parser as exposed to gdb. */
2523 rust_parse (struct parser_state *state)
2527 /* This sets various globals and also clears them on
2529 rust_parser parser (state);
2531 result = rustyyparse ();
2533 if (!result || (parse_completion && parser.rust_ast != NULL))
2534 convert_ast_to_expression (state, parser.rust_ast, parser.rust_ast);
2539 /* The parser error handler. */
2542 rustyyerror (const char *msg)
2544 const char *where = prev_lexptr ? prev_lexptr : lexptr;
2545 error (_("%s in expression, near `%s'."), (msg ? msg : "Error"), where);
2552 /* Initialize the lexer for testing. */
2555 rust_lex_test_init (const char *input)
2562 /* A test helper that lexes a string, expecting a single token. It
2563 returns the lexer data for this token. */
2566 rust_lex_test_one (const char *input, int expected)
2571 rust_lex_test_init (input);
2573 token = rustyylex ();
2574 SELF_CHECK (token == expected);
2575 result = rustyylval;
2579 token = rustyylex ();
2580 SELF_CHECK (token == 0);
2586 /* Test that INPUT lexes as the integer VALUE. */
2589 rust_lex_int_test (const char *input, int value, int kind)
2591 RUSTSTYPE result = rust_lex_test_one (input, kind);
2592 SELF_CHECK (result.typed_val_int.val == value);
2595 /* Test that INPUT throws an exception with text ERR. */
2598 rust_lex_exception_test (const char *input, const char *err)
2602 /* The "kind" doesn't matter. */
2603 rust_lex_test_one (input, DECIMAL_INTEGER);
2606 CATCH (except, RETURN_MASK_ERROR)
2608 SELF_CHECK (strcmp (except.message, err) == 0);
2613 /* Test that INPUT lexes as the identifier, string, or byte-string
2614 VALUE. KIND holds the expected token kind. */
2617 rust_lex_stringish_test (const char *input, const char *value, int kind)
2619 RUSTSTYPE result = rust_lex_test_one (input, kind);
2620 SELF_CHECK (result.sval.length == strlen (value));
2621 SELF_CHECK (strncmp (result.sval.ptr, value, result.sval.length) == 0);
2624 /* Helper to test that a string parses as a given token sequence. */
2627 rust_lex_test_sequence (const char *input, int len, const int expected[])
2634 for (i = 0; i < len; ++i)
2636 int token = rustyylex ();
2638 SELF_CHECK (token == expected[i]);
2642 /* Tests for an integer-parsing corner case. */
2645 rust_lex_test_trailing_dot (void)
2647 const int expected1[] = { DECIMAL_INTEGER, '.', IDENT, '(', ')', 0 };
2648 const int expected2[] = { INTEGER, '.', IDENT, '(', ')', 0 };
2649 const int expected3[] = { FLOAT, EQEQ, '(', ')', 0 };
2650 const int expected4[] = { DECIMAL_INTEGER, DOTDOT, DECIMAL_INTEGER, 0 };
2652 rust_lex_test_sequence ("23.g()", ARRAY_SIZE (expected1), expected1);
2653 rust_lex_test_sequence ("23_0.g()", ARRAY_SIZE (expected2), expected2);
2654 rust_lex_test_sequence ("23.==()", ARRAY_SIZE (expected3), expected3);
2655 rust_lex_test_sequence ("23..25", ARRAY_SIZE (expected4), expected4);
2658 /* Tests of completion. */
2661 rust_lex_test_completion (void)
2663 const int expected[] = { IDENT, '.', COMPLETE, 0 };
2665 parse_completion = 1;
2667 rust_lex_test_sequence ("something.wha", ARRAY_SIZE (expected), expected);
2668 rust_lex_test_sequence ("something.", ARRAY_SIZE (expected), expected);
2670 parse_completion = 0;
2673 /* Test pushback. */
2676 rust_lex_test_push_back (void)
2680 rust_lex_test_init (">>=");
2682 token = rustyylex ();
2683 SELF_CHECK (token == COMPOUND_ASSIGN);
2684 SELF_CHECK (rustyylval.opcode == BINOP_RSH);
2686 rust_push_back ('=');
2688 token = rustyylex ();
2689 SELF_CHECK (token == '=');
2691 token = rustyylex ();
2692 SELF_CHECK (token == 0);
2695 /* Unit test the lexer. */
2698 rust_lex_tests (void)
2702 auto_obstack test_obstack;
2703 scoped_restore obstack_holder = make_scoped_restore (&work_obstack,
2706 // Set up dummy "parser", so that rust_type works.
2707 struct parser_state ps (0, &rust_language_defn, target_gdbarch ());
2708 rust_parser parser (&ps);
2710 rust_lex_test_one ("", 0);
2711 rust_lex_test_one (" \t \n \r ", 0);
2712 rust_lex_test_one ("thread 23", 0);
2713 rust_lex_test_one ("task 23", 0);
2714 rust_lex_test_one ("th 104", 0);
2715 rust_lex_test_one ("ta 97", 0);
2717 rust_lex_int_test ("'z'", 'z', INTEGER);
2718 rust_lex_int_test ("'\\xff'", 0xff, INTEGER);
2719 rust_lex_int_test ("'\\u{1016f}'", 0x1016f, INTEGER);
2720 rust_lex_int_test ("b'z'", 'z', INTEGER);
2721 rust_lex_int_test ("b'\\xfe'", 0xfe, INTEGER);
2722 rust_lex_int_test ("b'\\xFE'", 0xfe, INTEGER);
2723 rust_lex_int_test ("b'\\xfE'", 0xfe, INTEGER);
2725 /* Test all escapes in both modes. */
2726 rust_lex_int_test ("'\\n'", '\n', INTEGER);
2727 rust_lex_int_test ("'\\r'", '\r', INTEGER);
2728 rust_lex_int_test ("'\\t'", '\t', INTEGER);
2729 rust_lex_int_test ("'\\\\'", '\\', INTEGER);
2730 rust_lex_int_test ("'\\0'", '\0', INTEGER);
2731 rust_lex_int_test ("'\\''", '\'', INTEGER);
2732 rust_lex_int_test ("'\\\"'", '"', INTEGER);
2734 rust_lex_int_test ("b'\\n'", '\n', INTEGER);
2735 rust_lex_int_test ("b'\\r'", '\r', INTEGER);
2736 rust_lex_int_test ("b'\\t'", '\t', INTEGER);
2737 rust_lex_int_test ("b'\\\\'", '\\', INTEGER);
2738 rust_lex_int_test ("b'\\0'", '\0', INTEGER);
2739 rust_lex_int_test ("b'\\''", '\'', INTEGER);
2740 rust_lex_int_test ("b'\\\"'", '"', INTEGER);
2742 rust_lex_exception_test ("'z", "Unterminated character literal");
2743 rust_lex_exception_test ("b'\\x0'", "Not enough hex digits seen");
2744 rust_lex_exception_test ("b'\\u{0}'", "Unicode escape in byte literal");
2745 rust_lex_exception_test ("'\\x0'", "Not enough hex digits seen");
2746 rust_lex_exception_test ("'\\u0'", "Missing '{' in Unicode escape");
2747 rust_lex_exception_test ("'\\u{0", "Missing '}' in Unicode escape");
2748 rust_lex_exception_test ("'\\u{0000007}", "Overlong hex escape");
2749 rust_lex_exception_test ("'\\u{}", "Not enough hex digits seen");
2750 rust_lex_exception_test ("'\\Q'", "Invalid escape \\Q in literal");
2751 rust_lex_exception_test ("b'\\Q'", "Invalid escape \\Q in literal");
2753 rust_lex_int_test ("23", 23, DECIMAL_INTEGER);
2754 rust_lex_int_test ("2_344__29", 234429, INTEGER);
2755 rust_lex_int_test ("0x1f", 0x1f, INTEGER);
2756 rust_lex_int_test ("23usize", 23, INTEGER);
2757 rust_lex_int_test ("23i32", 23, INTEGER);
2758 rust_lex_int_test ("0x1_f", 0x1f, INTEGER);
2759 rust_lex_int_test ("0b1_101011__", 0x6b, INTEGER);
2760 rust_lex_int_test ("0o001177i64", 639, INTEGER);
2762 rust_lex_test_trailing_dot ();
2764 rust_lex_test_one ("23.", FLOAT);
2765 rust_lex_test_one ("23.99f32", FLOAT);
2766 rust_lex_test_one ("23e7", FLOAT);
2767 rust_lex_test_one ("23E-7", FLOAT);
2768 rust_lex_test_one ("23e+7", FLOAT);
2769 rust_lex_test_one ("23.99e+7f64", FLOAT);
2770 rust_lex_test_one ("23.82f32", FLOAT);
2772 rust_lex_stringish_test ("hibob", "hibob", IDENT);
2773 rust_lex_stringish_test ("hibob__93", "hibob__93", IDENT);
2774 rust_lex_stringish_test ("thread", "thread", IDENT);
2776 rust_lex_stringish_test ("\"string\"", "string", STRING);
2777 rust_lex_stringish_test ("\"str\\ting\"", "str\ting", STRING);
2778 rust_lex_stringish_test ("\"str\\\"ing\"", "str\"ing", STRING);
2779 rust_lex_stringish_test ("r\"str\\ing\"", "str\\ing", STRING);
2780 rust_lex_stringish_test ("r#\"str\\ting\"#", "str\\ting", STRING);
2781 rust_lex_stringish_test ("r###\"str\\\"ing\"###", "str\\\"ing", STRING);
2783 rust_lex_stringish_test ("b\"string\"", "string", BYTESTRING);
2784 rust_lex_stringish_test ("b\"\x73tring\"", "string", BYTESTRING);
2785 rust_lex_stringish_test ("b\"str\\\"ing\"", "str\"ing", BYTESTRING);
2786 rust_lex_stringish_test ("br####\"\\x73tring\"####", "\\x73tring",
2789 for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i)
2790 rust_lex_test_one (identifier_tokens[i].name, identifier_tokens[i].value);
2792 for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i)
2793 rust_lex_test_one (operator_tokens[i].name, operator_tokens[i].value);
2795 rust_lex_test_completion ();
2796 rust_lex_test_push_back ();
2799 #endif /* GDB_SELF_TEST */
2802 _initialize_rust_exp (void)
2804 int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
2805 /* If the regular expression was incorrect, it was a programming
2807 gdb_assert (code == 0);
2810 selftests::register_test ("rust-lex", rust_lex_tests);