Add support for the sizeof function in Rust
authorManish Goregaokar <manish@mozilla.com>
Sat, 29 Oct 2016 12:55:58 +0000 (05:55 -0700)
committerManish Goregaokar <manish@mozilla.com>
Thu, 3 Nov 2016 22:45:14 +0000 (15:45 -0700)
2016-10-29  Manish Goregaokar  <manish@mozilla.com>

gdb/ChangeLog:
    * rust-exp.y: Parse `sizeof(exp)` as `UNOP_SIZEOF`

gdb/testsuite/ChangeLog:
    * gdb.rust/simple.exp: Add tests for `sizeof(expr)`

gdb/ChangeLog
gdb/rust-exp.y
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.rust/simple.exp

index 9d88984ca34c453bfab731f0865caf593212e6f4..8e173a7a9432adf71de0a9db9eba5a122fbf47f7 100644 (file)
@@ -1,3 +1,7 @@
+2016-10-29  Manish Goregaokar  <manish@mozilla.com>
+
+    * rust-exp.y: Parse `sizeof(exp)` as `UNOP_SIZEOF`
+
 2016-10-28  Manish Goregaokar  <manish@mozilla.com>
 
     * rust-lang.c (rust_union_is_untagged): Add function to
index 6dc4704efe7c037102e03618f9d1d87e8d05173a..dffccd0605bb5ef6e2e9d25d8eabd248461a01d7 100644 (file)
@@ -276,6 +276,7 @@ struct rust_op
 %token <voidval> KW_EXTERN
 %token <voidval> KW_CONST
 %token <voidval> KW_FN
+%token <voidval> KW_SIZEOF
 
 /* Operator tokens.  */
 %token <voidval> DOTDOT
@@ -371,7 +372,7 @@ expr:
 |      array_expr
 |      idx_expr
 |      range_expr
-|      unop_expr
+|      unop_expr /* Must precede call_expr because of ambiguity with sizeof.  */
 |      binop_expr
 |      paren_expr
 |      call_expr
@@ -577,7 +578,8 @@ unop_expr:
 
 |      '&' KW_MUT expr %prec UNARY
                { $$ = ast_unary (UNOP_ADDR, $3); }
-
+|   KW_SIZEOF '(' expr ')' %prec UNARY
+        { $$ = ast_unary (UNOP_SIZEOF, $3); }
 ;
 
 binop_expr:
@@ -872,6 +874,7 @@ static const struct token_info identifier_tokens[] =
   { "true", KW_TRUE, OP_NULL },
   { "extern", KW_EXTERN, OP_NULL },
   { "fn", KW_FN, OP_NULL },
+  { "sizeof", KW_SIZEOF, OP_NULL },
 };
 
 /* Operator tokens, sorted longest first.  */
@@ -2194,6 +2197,7 @@ convert_ast_to_expression (struct parser_state *state,
     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;
index 19cb7dc82a27fe84ebc3fec1fafe538219e033d1..ebeecf99b47dba0e4b07bfac767a21890626f0d9 100644 (file)
@@ -1,3 +1,7 @@
+2016-10-29  Manish Goregaokar  <manish@mozilla.com>
+
+    * gdb.rust/simple.exp: Add tests for `sizeof(expr)`
+
 2016-10-27  Manish Goregaokar  <manish@mozilla.com>
 
     * gdb.rust/simple.rs: Add test for univariant enums without discriminants
index 8e84daab3a9a8a64ba35ca713e9abed0d6962ce6..4b9a979968ef2ff7f50f2682df32e613e82030f8 100644 (file)
@@ -33,6 +33,7 @@ if {![runto ${srcfile}:$line]} {
 
 gdb_test "print a" " = \\(\\)"
 gdb_test "ptype a" " = \\(\\)"
+gdb_test "print sizeof(a)" " = 0"
 
 gdb_test "print b" " = \\\[\\\]"
 gdb_test "ptype b" " = \\\[i32; 0\\\]"
@@ -41,6 +42,7 @@ gdb_test "print *(&b as *const \[i32; 0_0\])" " = \\\[\\\]"
 
 gdb_test "print c" " = 99"
 gdb_test "ptype c" " = i32"
+gdb_test "print sizeof(c)" " = 4"
 
 gdb_test "print c = 87" " = \\(\\)"
 gdb_test "print c" " = 87"
@@ -127,7 +129,7 @@ gdb_test "print nosuchsymbol" \
 gdb_test "print e" " = simple::MoreComplicated::Two\\(73\\)"
 gdb_test "print e2" \
     " = simple::MoreComplicated::Four\\{this: true, is: 8, a: 109 'm', struct_: 100, variant: 10\\}"
-
+gdb_test "print sizeof(e)" " = 24"
 gdb_test_sequence "ptype e" "" {
     " = enum simple::MoreComplicated \\{"
     "  One,"
This page took 0.037762 seconds and 4 git commands to generate.