Commit 339d48ac15

Andrew Kelley <superjoe30@gmail.com>
2017-11-17 18:11:03
parse-c: support address of operator
1 parent 3e83597
Changed files (2)
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);
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);
+        \\}
+    );
 }