diff --git a/third_party/chibicc/codegen.c b/third_party/chibicc/codegen.c index c73be8300..c6c74a054 100644 --- a/third_party/chibicc/codegen.c +++ b/third_party/chibicc/codegen.c @@ -342,6 +342,13 @@ void gen_addr(Node *node) { gen_expr(node->lhs); gen_addr(node->rhs); return; + case ND_ASSIGN: + case ND_COND: + if (node->ty->kind == TY_STRUCT || node->ty->kind == TY_UNION) { + gen_expr(node); + return; + } + break; case ND_MEMBER: gen_addr(node->lhs); if (node->member->offset) { @@ -358,7 +365,6 @@ void gen_addr(Node *node) { println("\tlea\t%d(%%rbp),%%rax", node->var->offset); return; default: - DCHECK(0); error_tok(node->tok, "not an lvalue %d", node->kind); } } diff --git a/third_party/chibicc/test/struct_test.c b/third_party/chibicc/test/struct_test.c index c008c01a6..810996d84 100644 --- a/third_party/chibicc/test/struct_test.c +++ b/third_party/chibicc/test/struct_test.c @@ -374,6 +374,24 @@ int main() { }; sizeof(T); })); + ASSERT(2, ({ + struct { + int a; + } x = {1}, y = {2}; + (x = y).a; + })); + ASSERT(1, ({ + struct { + int a; + } x = {1}, y = {2}; + (1 ? x : y).a; + })); + ASSERT(2, ({ + struct { + int a; + } x = {1}, y = {2}; + (0 ? x : y).a; + })); return 0; }