#define RUSTSTYPE YYSTYPE
-extern initialize_file_ftype _initialize_rust_exp;
-
struct rust_op;
-typedef const struct rust_op *rust_op_ptr;
-DEF_VEC_P (rust_op_ptr);
+typedef std::vector<const struct rust_op *> rust_op_vector;
/* A typed integer constant. */
struct typed_val_float
{
- DOUBLEST dval;
+ gdb_byte val[16];
struct type *type;
};
const struct rust_op *init;
};
-typedef struct set_field set_field;
-
-DEF_VEC_O (set_field);
-
+typedef std::vector<set_field> rust_set_vector;
static int rustyylex (void);
static void rust_push_back (char c);
const struct rust_op *type);
static const struct rust_op *ast_call_ish (enum exp_opcode opcode,
const struct rust_op *expr,
- VEC (rust_op_ptr) **params);
+ rust_op_vector *params);
static const struct rust_op *ast_path (struct stoken name,
- VEC (rust_op_ptr) **params);
+ rust_op_vector *params);
static const struct rust_op *ast_string (struct stoken str);
static const struct rust_op *ast_struct (const struct rust_op *name,
- VEC (set_field) **fields);
+ rust_set_vector *fields);
static const struct rust_op *ast_range (const struct rust_op *lhs,
const struct rust_op *rhs);
static const struct rust_op *ast_array_type (const struct rust_op *lhs,
static const struct rust_op *ast_pointer_type (const struct rust_op *type,
int is_mut);
static const struct rust_op *ast_function_type (const struct rust_op *result,
- VEC (rust_op_ptr) **params);
-static const struct rust_op *ast_tuple_type (VEC (rust_op_ptr) **params);
+ rust_op_vector *params);
+static const struct rust_op *ast_tuple_type (rust_op_vector *params);
-/* The state of the parser, used internally when we are parsing the
- expression. */
+/* The current rust parser. */
-static struct parser_state *pstate = NULL;
+struct rust_parser;
+static rust_parser *current_parser;
/* A regular expression for matching Rust numbers. This is split up
since it is very long and this gives us a way to comment the
static regex_t number_regex;
-/* True if we're running unit tests. */
-
-static int unit_testing;
-
-/* Obstack for data temporarily allocated during parsing. */
-
-static struct obstack work_obstack;
-
-/* Result of parsing. Points into work_obstack. */
-
-static const struct rust_op *rust_ast;
+/* Obstack for data temporarily allocated during parsing. Points to
+ the obstack in the rust_parser, or to a temporary obstack during
+ unit testing. */
+
+static auto_obstack *work_obstack;
+
+/* An instance of this is created before parsing, and destroyed when
+ parsing is finished. */
+
+struct rust_parser
+{
+ rust_parser (struct parser_state *state)
+ : rust_ast (nullptr),
+ pstate (state)
+ {
+ gdb_assert (current_parser == nullptr);
+ current_parser = this;
+ work_obstack = &obstack;
+ }
+
+ ~rust_parser ()
+ {
+ /* Clean up the globals we set. */
+ current_parser = nullptr;
+ work_obstack = nullptr;
+ }
+
+ /* Create a new rust_set_vector. The storage for the new vector is
+ managed by this class. */
+ rust_set_vector *new_set_vector ()
+ {
+ rust_set_vector *result = new rust_set_vector;
+ set_vectors.push_back (std::unique_ptr<rust_set_vector> (result));
+ return result;
+ }
+
+ /* Create a new rust_ops_vector. The storage for the new vector is
+ managed by this class. */
+ rust_op_vector *new_op_vector ()
+ {
+ rust_op_vector *result = new rust_op_vector;
+ op_vectors.push_back (std::unique_ptr<rust_op_vector> (result));
+ return result;
+ }
+
+ /* Return the parser's language. */
+ const struct language_defn *language () const
+ {
+ return parse_language (pstate);
+ }
+
+ /* Return the parser's gdbarch. */
+ struct gdbarch *arch () const
+ {
+ return parse_gdbarch (pstate);
+ }
+
+ /* A pointer to this is installed globally. */
+ auto_obstack obstack;
+
+ /* Result of parsing. Points into obstack. */
+ const struct rust_op *rust_ast;
+
+ /* This keeps track of the various vectors we allocate. */
+ std::vector<std::unique_ptr<rust_set_vector>> set_vectors;
+ std::vector<std::unique_ptr<rust_op_vector>> op_vectors;
+
+ /* The parser state gdb gave us. */
+ struct parser_state *pstate;
+};
%}
/* A list of expressions; for example, the arguments to a function
call. */
- VEC (rust_op_ptr) **params;
+ rust_op_vector *params;
/* A list of field initializers. */
- VEC (set_field) **field_inits;
+ rust_set_vector *field_inits;
/* A single field initializer. */
struct set_field one_field_init;
{
/* If we are completing and see a valid parse,
rust_ast will already have been set. */
- if (rust_ast == NULL)
- rust_ast = $1;
+ if (current_parser->rust_ast == NULL)
+ current_parser->rust_ast = $1;
}
;
tuple_expr:
'(' expr ',' maybe_expr_list ')'
{
- VEC_safe_insert (rust_op_ptr, *$4, 0, $2);
+ $4->push_back ($2);
error (_("Tuple expressions not supported yet"));
}
;
struct typed_val_int val;
val.type
- = language_lookup_primitive_type (parse_language (pstate),
- parse_gdbarch (pstate),
+ = language_lookup_primitive_type (current_parser->language (),
+ current_parser->arch (),
"()");
val.val = 0;
$$ = ast_literal (val);
struct_expr_list:
/* %empty */
{
- VEC (set_field) **result
- = OBSTACK_ZALLOC (&work_obstack, VEC (set_field) *);
- $$ = result;
+ $$ = current_parser->new_set_vector ();
}
| struct_expr_tail
{
- VEC (set_field) **result
- = OBSTACK_ZALLOC (&work_obstack, VEC (set_field) *);
-
- make_cleanup (VEC_cleanup (set_field), result);
- VEC_safe_push (set_field, *result, &$1);
-
+ rust_set_vector *result = current_parser->new_set_vector ();
+ result->push_back ($1);
$$ = result;
}
| IDENT ':' expr ',' struct_expr_list
sf.name = $1;
sf.init = $3;
- VEC_safe_push (set_field, *$5, &sf);
+ $5->push_back (sf);
$$ = $5;
}
;
| STRING
{
const struct rust_op *str = ast_string ($1);
- VEC (set_field) **fields;
struct set_field field;
struct typed_val_int val;
struct stoken token;
- fields = OBSTACK_ZALLOC (&work_obstack, VEC (set_field) *);
- make_cleanup (VEC_cleanup (set_field), fields);
+ rust_set_vector *fields = current_parser->new_set_vector ();
/* Wrap the raw string in the &str struct. */
field.name.ptr = "data_ptr";
field.name.length = strlen (field.name.ptr);
field.init = ast_unary (UNOP_ADDR, ast_string ($1));
- VEC_safe_push (set_field, *fields, &field);
+ fields->push_back (field);
val.type = rust_type ("usize");
val.val = $1.length;
field.name.ptr = "length";
field.name.length = strlen (field.name.ptr);
field.init = ast_literal (val);
- VEC_safe_push (set_field, *fields, &field);
+ fields->push_back (field);
token.ptr = "&str";
token.length = strlen (token.ptr);
{
struct typed_val_int val;
- val.type = language_bool_type (parse_language (pstate),
- parse_gdbarch (pstate));
+ val.type = language_bool_type (current_parser->language (),
+ current_parser->arch ());
val.val = 1;
$$ = ast_literal (val);
}
{
struct typed_val_int val;
- val.type = language_bool_type (parse_language (pstate),
- parse_gdbarch (pstate));
+ val.type = language_bool_type (current_parser->language (),
+ current_parser->arch ());
val.val = 0;
$$ = ast_literal (val);
}
| expr '.' COMPLETE
{
$$ = ast_structop ($1, $3.ptr, 1);
- rust_ast = $$;
+ current_parser->rust_ast = $$;
}
| expr '.' DECIMAL_INTEGER
{ $$ = ast_structop_anonymous ($1, $3); }
expr_list:
expr
{
- $$ = OBSTACK_ZALLOC (&work_obstack, VEC (rust_op_ptr) *);
- make_cleanup (VEC_cleanup (rust_op_ptr), $$);
- VEC_safe_push (rust_op_ptr, *$$, $1);
+ $$ = current_parser->new_op_vector ();
+ $$->push_back ($1);
}
| expr_list ',' expr
{
- VEC_safe_push (rust_op_ptr, *$1, $3);
+ $1->push_back ($3);
$$ = $1;
}
;
/* %empty */
{
/* The result can't be NULL. */
- $$ = OBSTACK_ZALLOC (&work_obstack, VEC (rust_op_ptr) *);
- make_cleanup (VEC_cleanup (rust_op_ptr), $$);
+ $$ = current_parser->new_op_vector ();
}
| expr_list
{ $$ = $1; }
type_list:
type
{
- VEC (rust_op_ptr) **result
- = OBSTACK_ZALLOC (&work_obstack, VEC (rust_op_ptr) *);
-
- make_cleanup (VEC_cleanup (rust_op_ptr), result);
- VEC_safe_push (rust_op_ptr, *result, $1);
+ rust_op_vector *result = current_parser->new_op_vector ();
+ result->push_back ($1);
$$ = result;
}
| type_list ',' type
{
- VEC_safe_push (rust_op_ptr, *$1, $3);
+ $1->push_back ($3);
$$ = $1;
}
;
static const char *
rust_copy_name (const char *name, int len)
{
- return (const char *) obstack_copy0 (&work_obstack, name, len);
+ return (const char *) obstack_copy0 (work_obstack, name, len);
}
/* Helper function to make an stoken from a C string. */
static struct stoken
rust_concat3 (const char *s1, const char *s2, const char *s3)
{
- return make_stoken (obconcat (&work_obstack, s1, s2, s3, (char *) NULL));
+ return make_stoken (obconcat (work_obstack, s1, s2, s3, (char *) NULL));
}
/* Return an AST node referring to NAME, but relative to the crate's
if (crate.empty ())
error (_("Could not find crate for current location"));
- result = make_stoken (obconcat (&work_obstack, "::", crate.c_str (), "::",
+ result = make_stoken (obconcat (work_obstack, "::", crate.c_str (), "::",
name->left.sval.ptr, (char *) NULL));
return ast_path (result, name->right.params);
if (n_supers > 0)
{
- int i;
int len;
std::vector<int> offsets;
unsigned int current_len;
else
offset = strlen (scope);
- obstack_grow (&work_obstack, "::", 2);
- obstack_grow (&work_obstack, scope, offset);
- obstack_grow (&work_obstack, "::", 2);
- obstack_grow0 (&work_obstack, ident->left.sval.ptr, ident->left.sval.length);
+ obstack_grow (work_obstack, "::", 2);
+ obstack_grow (work_obstack, scope, offset);
+ obstack_grow (work_obstack, "::", 2);
+ obstack_grow0 (work_obstack, ident->left.sval.ptr, ident->left.sval.length);
- return ast_path (make_stoken ((const char *) obstack_finish (&work_obstack)),
+ return ast_path (make_stoken ((const char *) obstack_finish (work_obstack)),
ident->right.params);
}
{
struct type *type;
- /* When unit testing, we don't bother checking the types, so avoid a
- possibly-failing lookup here. */
- if (unit_testing)
- return NULL;
-
- type = language_lookup_primitive_type (parse_language (pstate),
- parse_gdbarch (pstate),
+ type = language_lookup_primitive_type (current_parser->language (),
+ current_parser->arch (),
name);
if (type == NULL)
error (_("Could not find Rust type %s"), name);
{
int is_byte = lexptr[0] == 'b';
int raw_length;
- int len_in_chars = 0;
if (is_byte)
++lexptr;
value = lexptr[0] & 0xff;
if (is_byte && value > 127)
error (_("Non-ASCII value in raw byte string"));
- obstack_1grow (&work_obstack, value);
+ obstack_1grow (work_obstack, value);
++lexptr;
}
value = lex_escape (is_byte);
if (is_byte)
- obstack_1grow (&work_obstack, value);
+ obstack_1grow (work_obstack, value);
else
convert_between_encodings ("UTF-32", "UTF-8", (gdb_byte *) &value,
sizeof (value), sizeof (value),
- &work_obstack, translit_none);
+ work_obstack, translit_none);
}
else if (lexptr[0] == '\0')
error (_("Unexpected EOF in string"));
value = lexptr[0] & 0xff;
if (is_byte && value > 127)
error (_("Non-ASCII value in byte string"));
- obstack_1grow (&work_obstack, value);
+ obstack_1grow (work_obstack, value);
++lexptr;
}
}
- rustyylval.sval.length = obstack_object_size (&work_obstack);
- rustyylval.sval.ptr = (const char *) obstack_finish (&work_obstack);
+ rustyylval.sval.length = obstack_object_size (work_obstack);
+ rustyylval.sval.ptr = (const char *) obstack_finish (work_obstack);
return is_byte ? BYTESTRING : STRING;
}
gdb_assert (subexps[0].rm_eo > 0);
if (lexptr[subexps[0].rm_eo - 1] == '.')
{
- const char *next = skip_spaces_const (&lexptr[subexps[0].rm_eo]);
+ const char *next = skip_spaces (&lexptr[subexps[0].rm_eo]);
if (rust_identifier_start_p (*next) || *next == '.')
{
}
else
{
- rustyylval.typed_val_float.dval = strtod (number.c_str (), NULL);
rustyylval.typed_val_float.type = type;
+ bool parsed = parse_float (number.c_str (), number.length (),
+ rustyylval.typed_val_float.type,
+ rustyylval.typed_val_float.val);
+ gdb_assert (parsed);
}
return is_integer ? (could_be_decimal ? DECIMAL_INTEGER : INTEGER) : FLOAT;
ast_operation (enum exp_opcode opcode, const struct rust_op *left,
const struct rust_op *right)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = opcode;
result->left.op = left;
ast_compound_assignment (enum exp_opcode opcode, const struct rust_op *left,
const struct rust_op *right)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = opcode;
result->compound_assignment = 1;
static const struct rust_op *
ast_literal (struct typed_val_int val)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_LONG;
result->left.typed_val_int = val;
static const struct rust_op *
ast_dliteral (struct typed_val_float val)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
- result->opcode = OP_DOUBLE;
+ result->opcode = OP_FLOAT;
result->left.typed_val_float = val;
return result;
static const struct rust_op *
ast_cast (const struct rust_op *expr, const struct rust_op *type)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = UNOP_CAST;
result->left.op = expr;
static const struct rust_op *
ast_call_ish (enum exp_opcode opcode, const struct rust_op *expr,
- VEC (rust_op_ptr) **params)
+ rust_op_vector *params)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = opcode;
result->left.op = expr;
/* Make a structure creation operation. */
static const struct rust_op *
-ast_struct (const struct rust_op *name, VEC (set_field) **fields)
+ast_struct (const struct rust_op *name, rust_set_vector *fields)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_AGGREGATE;
result->left.op = name;
/* Make an identifier path. */
static const struct rust_op *
-ast_path (struct stoken path, VEC (rust_op_ptr) **params)
+ast_path (struct stoken path, rust_op_vector *params)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_VAR_VALUE;
result->left.sval = path;
static const struct rust_op *
ast_string (struct stoken str)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_STRING;
result->left.sval = str;
static const struct rust_op *
ast_structop (const struct rust_op *left, const char *name, int completing)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = STRUCTOP_STRUCT;
result->completing = completing;
ast_structop_anonymous (const struct rust_op *left,
struct typed_val_int number)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = STRUCTOP_ANONYMOUS;
result->left.op = left;
static const struct rust_op *
ast_range (const struct rust_op *lhs, const struct rust_op *rhs)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_RANGE;
result->left.op = lhs;
static struct rust_op *
ast_basic_type (enum type_code typecode)
{
- struct rust_op *result = OBSTACK_ZALLOC (&work_obstack, struct rust_op);
+ struct rust_op *result = OBSTACK_ZALLOC (work_obstack, struct rust_op);
result->opcode = OP_TYPE;
result->typecode = typecode;
/* Create an AST node describing a function type. */
static const struct rust_op *
-ast_function_type (const struct rust_op *rtype, VEC (rust_op_ptr) **params)
+ast_function_type (const struct rust_op *rtype, rust_op_vector *params)
{
struct rust_op *result = ast_basic_type (TYPE_CODE_FUNC);
/* Create an AST node describing a tuple type. */
static const struct rust_op *
-ast_tuple_type (VEC (rust_op_ptr) **params)
+ast_tuple_type (rust_op_vector *params)
{
struct rust_op *result = ast_basic_type (TYPE_CODE_STRUCT);
return SYMBOL_TYPE (result.symbol);
}
- type = lookup_typename (parse_language (pstate), parse_gdbarch (pstate),
+ type = lookup_typename (current_parser->language (), current_parser->arch (),
name, NULL, 1);
if (type != NULL)
return type;
/* Last chance, try a built-in type. */
- return language_lookup_primitive_type (parse_language (pstate),
- parse_gdbarch (pstate),
+ return language_lookup_primitive_type (current_parser->language (),
+ current_parser->arch (),
name);
}
types. */
static std::vector<struct type *>
-convert_params_to_types (struct parser_state *state, VEC (rust_op_ptr) *params)
+convert_params_to_types (struct parser_state *state, rust_op_vector *params)
{
- int i;
- const struct rust_op *op;
std::vector<struct type *> result;
- for (i = 0; VEC_iterate (rust_op_ptr, params, i, op); ++i)
+ for (const rust_op *op : *params)
result.push_back (convert_ast_to_type (state, op));
return result;
case TYPE_CODE_FUNC:
{
std::vector<struct type *> args
- (convert_params_to_types (state, *operation->right.params));
+ (convert_params_to_types (state, operation->right.params));
struct type **argtypes = NULL;
type = convert_ast_to_type (state, operation->left.op);
case TYPE_CODE_STRUCT:
{
std::vector<struct type *> args
- (convert_params_to_types (state, *operation->left.params));
+ (convert_params_to_types (state, operation->left.params));
int i;
- struct type *type;
const char *name;
- obstack_1grow (&work_obstack, '(');
+ obstack_1grow (work_obstack, '(');
for (i = 0; i < args.size (); ++i)
{
std::string type_name = type_to_string (args[i]);
if (i > 0)
- obstack_1grow (&work_obstack, ',');
- obstack_grow_str (&work_obstack, type_name.c_str ());
+ obstack_1grow (work_obstack, ',');
+ obstack_grow_str (work_obstack, type_name.c_str ());
}
- obstack_grow_str0 (&work_obstack, ")");
- name = (const char *) obstack_finish (&work_obstack);
+ obstack_grow_str0 (work_obstack, ")");
+ name = (const char *) obstack_finish (work_obstack);
/* We don't allow creating new tuple types (yet), but we do
allow looking up existing tuple types. */
return operation->left.sval.ptr;
std::vector<struct type *> types
- (convert_params_to_types (state, *operation->right.params));
+ (convert_params_to_types (state, operation->right.params));
- obstack_grow_str (&work_obstack, operation->left.sval.ptr);
- obstack_1grow (&work_obstack, '<');
+ obstack_grow_str (work_obstack, operation->left.sval.ptr);
+ obstack_1grow (work_obstack, '<');
for (i = 0; i < types.size (); ++i)
{
std::string type_name = type_to_string (types[i]);
if (i > 0)
- obstack_1grow (&work_obstack, ',');
+ obstack_1grow (work_obstack, ',');
- obstack_grow_str (&work_obstack, type_name.c_str ());
+ obstack_grow_str (work_obstack, type_name.c_str ());
}
- obstack_grow_str0 (&work_obstack, ">");
+ obstack_grow_str0 (work_obstack, ">");
- return (const char *) obstack_finish (&work_obstack);
+ return (const char *) obstack_finish (work_obstack);
}
static void convert_ast_to_expression (struct parser_state *state,
const struct rust_op *operation,
- const struct rust_op *top);
+ const struct rust_op *top,
+ bool want_type = false);
/* A helper function that converts a vec of rust_ops to a gdb
expression. */
static void
convert_params_to_expression (struct parser_state *state,
- VEC (rust_op_ptr) *params,
+ rust_op_vector *params,
const struct rust_op *top)
{
- int i;
- rust_op_ptr elem;
-
- for (i = 0; VEC_iterate (rust_op_ptr, params, i, elem); ++i)
+ for (const rust_op *elem : *params)
convert_ast_to_expression (state, elem, top);
}
OPERATION is the operation to lower. TOP is a pointer to the
top-most operation; it is used to handle the special case where the
top-most expression is an identifier and can be optionally lowered
- to OP_TYPE. */
+ to OP_TYPE. WANT_TYPE is a flag indicating that, if the expression
+ is the name of a type, then emit an OP_TYPE for it (rather than
+ erroring). If WANT_TYPE is set, then the similar TOP handling is
+ not done. */
static void
convert_ast_to_expression (struct parser_state *state,
const struct rust_op *operation,
- const struct rust_op *top)
+ const struct rust_op *top,
+ bool want_type)
{
switch (operation->opcode)
{
write_exp_elt_opcode (state, OP_LONG);
break;
- case OP_DOUBLE:
- write_exp_elt_opcode (state, OP_DOUBLE);
+ case OP_FLOAT:
+ write_exp_elt_opcode (state, OP_FLOAT);
write_exp_elt_type (state, operation->left.typed_val_float.type);
- write_exp_elt_dblcst (state, operation->left.typed_val_float.dval);
- write_exp_elt_opcode (state, OP_DOUBLE);
+ write_exp_elt_floatcst (state, operation->left.typed_val_float.val);
+ write_exp_elt_opcode (state, OP_FLOAT);
break;
case STRUCTOP_STRUCT:
}
break;
+ case UNOP_SIZEOF:
+ convert_ast_to_expression (state, operation->left.op, top, true);
+ write_exp_elt_opcode (state, UNOP_SIZEOF);
+ break;
+
case UNOP_PLUS:
case UNOP_NEG:
case UNOP_COMPLEMENT:
case UNOP_IND:
case UNOP_ADDR:
- case UNOP_SIZEOF:
convert_ast_to_expression (state, operation->left.op, top);
write_exp_elt_opcode (state, operation->opcode);
break;
{
/* This is actually a tuple struct expression, not a
call expression. */
- rust_op_ptr elem;
- int i;
- VEC (rust_op_ptr) *params = *operation->right.params;
+ rust_op_vector *params = operation->right.params;
if (TYPE_CODE (type) != TYPE_CODE_NAMESPACE)
{
if (!rust_tuple_struct_type_p (type))
error (_("Type %s is not a tuple struct"), varname);
- for (i = 0;
- VEC_iterate (rust_op_ptr, params, i, elem);
- ++i)
+ for (int i = 0; i < params->size (); ++i)
{
char *cell = get_print_cell ();
write_exp_string (state, make_stoken (cell));
write_exp_elt_opcode (state, OP_NAME);
- convert_ast_to_expression (state, elem, top);
+ convert_ast_to_expression (state, (*params)[i], top);
}
write_exp_elt_opcode (state, OP_AGGREGATE);
write_exp_elt_type (state, type);
- write_exp_elt_longcst (state,
- 2 * VEC_length (rust_op_ptr,
- params));
+ write_exp_elt_longcst (state, 2 * params->size ());
write_exp_elt_opcode (state, OP_AGGREGATE);
break;
}
}
}
convert_ast_to_expression (state, operation->left.op, top);
- convert_params_to_expression (state, *operation->right.params, top);
+ convert_params_to_expression (state, operation->right.params, top);
write_exp_elt_opcode (state, OP_FUNCALL);
- write_exp_elt_longcst (state, VEC_length (rust_op_ptr,
- *operation->right.params));
+ write_exp_elt_longcst (state, operation->right.params->size ());
write_exp_elt_longcst (state, OP_FUNCALL);
}
break;
case OP_ARRAY:
gdb_assert (operation->left.op == NULL);
- convert_params_to_expression (state, *operation->right.params, top);
+ convert_params_to_expression (state, operation->right.params, top);
write_exp_elt_opcode (state, OP_ARRAY);
write_exp_elt_longcst (state, 0);
- write_exp_elt_longcst (state, VEC_length (rust_op_ptr,
- *operation->right.params) - 1);
+ write_exp_elt_longcst (state, operation->right.params->size () - 1);
write_exp_elt_longcst (state, OP_ARRAY);
break;
varname = convert_name (state, operation);
sym = rust_lookup_symbol (varname, expression_context_block,
VAR_DOMAIN);
- if (sym.symbol != NULL)
+ if (sym.symbol != NULL && SYMBOL_CLASS (sym.symbol) != LOC_TYPEDEF)
{
write_exp_elt_opcode (state, OP_VAR_VALUE);
write_exp_elt_block (state, sym.block);
}
else
{
- struct type *type;
+ struct type *type = NULL;
- type = rust_lookup_type (varname, expression_context_block);
+ if (sym.symbol != NULL)
+ {
+ gdb_assert (SYMBOL_CLASS (sym.symbol) == LOC_TYPEDEF);
+ type = SYMBOL_TYPE (sym.symbol);
+ }
+ if (type == NULL)
+ type = rust_lookup_type (varname, expression_context_block);
if (type == NULL)
error (_("No symbol '%s' in current context"), varname);
- if (TYPE_CODE (type) == TYPE_CODE_STRUCT
+ if (!want_type
+ && TYPE_CODE (type) == TYPE_CODE_STRUCT
&& TYPE_NFIELDS (type) == 0)
{
/* A unit-like struct. */
write_exp_elt_longcst (state, 0);
write_exp_elt_opcode (state, OP_AGGREGATE);
}
- else if (operation == top)
+ else if (want_type || operation == top)
{
write_exp_elt_opcode (state, OP_TYPE);
write_exp_elt_type (state, type);
write_exp_elt_opcode (state, OP_TYPE);
- break;
}
+ else
+ error (_("Found type '%s', which can't be "
+ "evaluated in this context"),
+ varname);
}
}
break;
case OP_AGGREGATE:
{
- int i;
int length;
- struct set_field *init;
- VEC (set_field) *fields = *operation->right.field_inits;
+ rust_set_vector *fields = operation->right.field_inits;
struct type *type;
const char *name;
length = 0;
- for (i = 0; VEC_iterate (set_field, fields, i, init); ++i)
+ for (const set_field &init : *fields)
{
- if (init->name.ptr != NULL)
+ if (init.name.ptr != NULL)
{
write_exp_elt_opcode (state, OP_NAME);
- write_exp_string (state, init->name);
+ write_exp_string (state, init.name);
write_exp_elt_opcode (state, OP_NAME);
++length;
}
- convert_ast_to_expression (state, init->init, top);
+ convert_ast_to_expression (state, init.init, top);
++length;
- if (init->name.ptr == NULL)
+ if (init.name.ptr == NULL)
{
/* This is handled differently from Ada in our
evaluator. */
rust_parse (struct parser_state *state)
{
int result;
- struct cleanup *cleanup;
- obstack_init (&work_obstack);
- cleanup = make_cleanup_obstack_free (&work_obstack);
- rust_ast = NULL;
+ /* This sets various globals and also clears them on
+ destruction. */
+ rust_parser parser (state);
- pstate = state;
result = rustyyparse ();
- if (!result || (parse_completion && rust_ast != NULL))
- {
- const struct rust_op *ast = rust_ast;
+ if (!result || (parse_completion && parser.rust_ast != NULL))
+ convert_ast_to_expression (state, parser.rust_ast, parser.rust_ast);
- rust_ast = NULL;
- gdb_assert (ast != NULL);
- convert_ast_to_expression (state, ast, ast);
- }
-
- do_cleanups (cleanup);
return result;
}
/* The parser error handler. */
void
-rustyyerror (char *msg)
+rustyyerror (const char *msg)
{
const char *where = prev_lexptr ? prev_lexptr : lexptr;
error (_("%s in expression, near `%s'."), (msg ? msg : "Error"), where);
{
int i;
- obstack_init (&work_obstack);
- unit_testing = 1;
+ auto_obstack test_obstack;
+ scoped_restore obstack_holder = make_scoped_restore (&work_obstack,
+ &test_obstack);
+
+ // Set up dummy "parser", so that rust_type works.
+ struct parser_state ps;
+ initialize_expout (&ps, 0, &rust_language_defn, target_gdbarch ());
+ rust_parser parser (&ps);
rust_lex_test_one ("", 0);
rust_lex_test_one (" \t \n \r ", 0);
rust_lex_test_completion ();
rust_lex_test_push_back ();
-
- obstack_free (&work_obstack, NULL);
- unit_testing = 0;
}
#endif /* GDB_SELF_TEST */
gdb_assert (code == 0);
#if GDB_SELF_TEST
- register_self_test (rust_lex_tests);
+ selftests::register_test ("rust-lex", rust_lex_tests);
#endif
}