wcc: Hare-style struct embedding (anon + bare-name)

Struct members can be `struct { ... }` (anonymous nested) or a bare
named type, in addition to `name: type`. The inner struct's fields
are promoted to the outer scope with offsets shifted by the embed
base; codegen already keys off Tfield.offset so cgen needs no change.
Errors on non-struct embed or name collision.
This commit is contained in:
2026-05-11 23:32:44 +09:00
parent dd188ca460
commit 35421f2561
3 changed files with 79 additions and 13 deletions

View File

@@ -191,9 +191,21 @@ parsetype(Parser *p)
while (p->cur.kind != TK_RBRACE && p->cur.kind != TK_EOF) {
Pos fp = p->cur.pos;
Node *f = newnode(p->a, N_TFIELD, fp);
f->str = expectident(p);
expect(p, TK_COLON);
f->lhs = parsetype(p);
/* Three member forms:
* name: type — regular field
* struct { ... } — anonymous embedded struct
* Identifier — bare-name embedded type
* Embeds carry f->str == NULL and the type in f->lhs. */
if (p->cur.kind == TK_STRUCT) {
f->lhs = parsetype(p);
} else if (p->cur.kind == TK_IDENT
&& peek(p).kind != TK_COLON) {
f->lhs = parsetype(p);
} else {
f->str = expectident(p);
expect(p, TK_COLON);
f->lhs = parsetype(p);
}
if (head == NULL) head = f;
else tail->next = f;
tail = f;