Make struct member access to work with `=` and `?:`

This commit is contained in:
Rui Ueyama 2020-12-07 11:50:43 +09:00
parent 982041fb1c
commit 90d1f7f199
2 changed files with 11 additions and 0 deletions

View File

@ -167,6 +167,13 @@ static void gen_addr(Node *node) {
return; return;
} }
break; break;
case ND_ASSIGN:
case ND_COND:
if (node->ty->kind == TY_STRUCT || node->ty->kind == TY_UNION) {
gen_expr(node);
return;
}
break;
case ND_VLA_PTR: case ND_VLA_PTR:
println(" lea %d(%%rbp), %%rax", node->var->offset); println(" lea %d(%%rbp), %%rax", node->var->offset);
return; return;

View File

@ -57,6 +57,10 @@ int main() {
ASSERT(1, ({ struct T { struct T *next; int x; } a; struct T b; b.x=1; a.next=&b; a.next->x; })); ASSERT(1, ({ struct T { struct T *next; int x; } a; struct T b; b.x=1; a.next=&b; a.next->x; }));
ASSERT(4, ({ typedef struct T T; struct T { int x; }; sizeof(T); })); ASSERT(4, ({ typedef struct T T; struct T { int x; }; 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; }));
printf("OK\n"); printf("OK\n");
return 0; return 0;
} }