authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-17 12:11:03-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-17 12:11:03-05:00
log339d48ac1558dcd1977574372becd21f7fc4a075
tree271edd749e32ea13bcea8dfc7c6f58ecdd837c8f
parent3e835973db361ebdb35d34c371870acd13d95e17

parse-c: support address of operator


2 files changed, 22 insertions(+), 3 deletions(-)

src/parsec.cpp+9-3
...@@ -1586,12 +1586,18 @@ static AstNode *trans_unary_operator(Context *c, bool result_used, AstNode *bloc...@@ -1586,12 +1586,18 @@ static AstNode *trans_unary_operator(Context *c, bool result_used, AstNode *bloc
1586 emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_PreDec");1586 emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_PreDec");
1587 return nullptr;1587 return nullptr;
1588 case UO_AddrOf:1588 case UO_AddrOf:
1589 emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_AddrOf");1589 {
1590 return nullptr;1590 AstNode *value_node = trans_expr(c, result_used, block, stmt->getSubExpr(), TransLValue);
1591 if (value_node == nullptr)
1592 return value_node;
1593 return trans_create_node_addr_of(c, false, false, value_node);
1594 }
1591 case UO_Deref:1595 case UO_Deref:
1592 {1596 {
1593 bool is_fn_ptr = qual_type_is_fn_ptr(c, stmt->getSubExpr()->getType());
1594 AstNode *value_node = trans_expr(c, result_used, block, stmt->getSubExpr(), TransRValue);1597 AstNode *value_node = trans_expr(c, result_used, block, stmt->getSubExpr(), TransRValue);
1598 if (value_node == nullptr)
1599 return nullptr;
1600 bool is_fn_ptr = qual_type_is_fn_ptr(c, stmt->getSubExpr()->getType());
1595 if (is_fn_ptr)1601 if (is_fn_ptr)
1596 return value_node;1602 return value_node;
1597 AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node);1603 AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node);
test/parsec.zig+13
...@@ -872,6 +872,19 @@ pub fn addCases(cases: &tests.ParseCContext) {...@@ -872,6 +872,19 @@ pub fn addCases(cases: &tests.ParseCContext) {
872 \\pub const Foo = union_Foo;872 \\pub const Foo = union_Foo;
873 );873 );
874874
875 cases.add("address of operator",
876 \\int foo(void) {
877 \\ int x = 1234;
878 \\ int *ptr = &x;
879 \\ return *ptr;
880 \\}
881 ,
882 \\pub fn foo() -> c_int {
883 \\ var x: c_int = 1234;
884 \\ var ptr: ?&c_int = &x;
885 \\ return *(??ptr);
886 \\}
887 );
875}888}
876889
877890