Skip to content

Commit 339d48a

Browse files
committedNov 17, 2017
parse-c: support address of operator
1 parent 3e83597 commit 339d48a

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

‎src/parsec.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -1586,12 +1586,18 @@ static AstNode *trans_unary_operator(Context *c, bool result_used, AstNode *bloc
15861586
emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_PreDec");
15871587
return nullptr;
15881588
case UO_AddrOf:
1589-
emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_AddrOf");
1590-
return nullptr;
1589+
{
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+
}
15911595
case UO_Deref:
15921596
{
1593-
bool is_fn_ptr = qual_type_is_fn_ptr(c, stmt->getSubExpr()->getType());
15941597
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());
15951601
if (is_fn_ptr)
15961602
return value_node;
15971603
AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node);

‎test/parsec.zig

+13
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,19 @@ pub fn addCases(cases: &tests.ParseCContext) {
872872
\\pub const Foo = union_Foo;
873873
);
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+
);
875888
}
876889

877890

0 commit comments

Comments
 (0)
Please sign in to comment.