Skip to content

Commit

Permalink
implement tuple struct expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
tromey committed Apr 14, 2016
1 parent d6c9492 commit 54c13d8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
88 changes: 66 additions & 22 deletions gdb/rust-exp.y
Original file line number Diff line number Diff line change
Expand Up @@ -1447,22 +1447,6 @@ make_structop (const struct rust_op *left, const char *name)
return result;
}

static void convert_ast_to_expression (struct parser_state *state,
const struct rust_op *operation,
const struct rust_op *top);

static void
convert_params_to_expression (struct parser_state *state,
VEC (rust_op_ptr) *params,
const struct rust_op *top)
{
int i;
rust_op_ptr elem;

for (i = 0; VEC_iterate (rust_op_ptr, params, i, elem); ++i)
convert_ast_to_expression (state, elem, top);
}

/* Like lookup_symbol, but handles Rust namespace conventions, and
doesn't require field_of_this_result. */

Expand All @@ -1487,6 +1471,22 @@ rust_lookup_symbol (const char *name, const struct block *block,
return result;
}

static void convert_ast_to_expression (struct parser_state *state,
const struct rust_op *operation,
const struct rust_op *top);

static void
convert_params_to_expression (struct parser_state *state,
VEC (rust_op_ptr) *params,
const struct rust_op *top)
{
int i;
rust_op_ptr elem;

for (i = 0; VEC_iterate (rust_op_ptr, params, i, elem); ++i)
convert_ast_to_expression (state, elem, top);
}

static void
convert_ast_to_expression (struct parser_state *state,
const struct rust_op *operation,
Expand Down Expand Up @@ -1586,12 +1586,56 @@ convert_ast_to_expression (struct parser_state *state,
break;

case OP_FUNCALL:
convert_ast_to_expression (state, operation->left.op, 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, OP_FUNCALL);
{
if (operation->left.op->opcode == OP_VAR_VALUE)
{
struct block_symbol sym;
const char *varname = operation->left.op->left.sval.ptr;

sym = rust_lookup_symbol (varname, expression_context_block,
STRUCT_DOMAIN);
if (sym.symbol != NULL)
{
/* This is actually a tuple struct expression, not a
call expression. */
struct type *type = SYMBOL_TYPE (sym.symbol);
rust_op_ptr elem;
int i;
VEC (rust_op_ptr) *params = *operation->right.params;

if (!rust_tuple_struct_type_p (type))
error (_("type %s is not a tuple struct"),
SYMBOL_PRINT_NAME (sym.symbol));

for (i = 0;
VEC_iterate (rust_op_ptr, params, i, elem);
++i)
{
char *cell = get_print_cell ();

xsnprintf (cell, PRINT_CELL_SIZE, "__%d", i);
write_exp_elt_opcode (state, OP_NAME);
write_exp_string (state, make_stoken (cell));
write_exp_elt_opcode (state, OP_NAME);

convert_ast_to_expression (state, elem, 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_opcode (state, OP_AGGREGATE);
break;
}
}
convert_ast_to_expression (state, operation->left.op, 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, OP_FUNCALL);
}
break;

case OP_ARRAY:
Expand Down
5 changes: 2 additions & 3 deletions gdb/rust-lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ rust_tuple_type_p (struct type *type)
return TYPE_TAG_NAME (type) != NULL && TYPE_TAG_NAME (type)[0] == '(';
}

/* Return true if the struct TYPE is a tuple struct type; otherwise
false. */
/* See rust-lang.h. */

static int
int
rust_tuple_struct_type_p (struct type *type)
{
int i;
Expand Down
5 changes: 5 additions & 0 deletions gdb/rust-lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
#define RUST_LANG_H

struct parser_state;
struct type;

extern int rust_parse (struct parser_state *);

extern void rusterror (char *);

/* Return true if the struct TYPE is a tuple struct type; otherwise
false. */
extern int rust_tuple_struct_type_p (struct type *type);

#endif /* RUST_LANG_H */
3 changes: 3 additions & 0 deletions gdb/testsuite/gdb.rust/simple.exp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ gdb_test_sequence "ptype z" "" \
"\\)"]
gdb_test "print z.1" " = 8"

gdb_test "print simple::ByeBob(0xff, 5)" \
" = simple::ByeBob \\(255, 5\\)"

# FIXME - can't find the name
# gdb_test "print diff2(3, 7)" " = -4"
gdb_test "print self::diff2(8, 9)" " = -1"
Expand Down

0 comments on commit 54c13d8

Please sign in to comment.