diff --git a/src/parsec.cpp b/src/parsec.cpp index 4ffee98cc7..79ba2ab990 100644 --- a/src/parsec.cpp +++ b/src/parsec.cpp @@ -1586,12 +1586,18 @@ static AstNode *trans_unary_operator(Context *c, bool result_used, AstNode *bloc emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_PreDec"); return nullptr; case UO_AddrOf: - emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_AddrOf"); - return nullptr; + { + AstNode *value_node = trans_expr(c, result_used, block, stmt->getSubExpr(), TransLValue); + if (value_node == nullptr) + return value_node; + return trans_create_node_addr_of(c, false, false, value_node); + } case UO_Deref: { - bool is_fn_ptr = qual_type_is_fn_ptr(c, stmt->getSubExpr()->getType()); AstNode *value_node = trans_expr(c, result_used, block, stmt->getSubExpr(), TransRValue); + if (value_node == nullptr) + return nullptr; + bool is_fn_ptr = qual_type_is_fn_ptr(c, stmt->getSubExpr()->getType()); if (is_fn_ptr) return value_node; AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node); diff --git a/test/parsec.zig b/test/parsec.zig index 1fe55290bb..f830131262 100644 --- a/test/parsec.zig +++ b/test/parsec.zig @@ -872,6 +872,19 @@ pub fn addCases(cases: &tests.ParseCContext) { \\pub const Foo = union_Foo; ); + cases.add("address of operator", + \\int foo(void) { + \\ int x = 1234; + \\ int *ptr = &x; + \\ return *ptr; + \\} + , + \\pub fn foo() -> c_int { + \\ var x: c_int = 1234; + \\ var ptr: ?&c_int = &x; + \\ return *(??ptr); + \\} + ); }