From 0c6330ce7ee50c1fa241793588fa83a3c00bc3c1 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 6 Aug 2022 16:11:26 +0200 Subject: inferred length array init --- bootstrap/parse.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'bootstrap/parse.c') diff --git a/bootstrap/parse.c b/bootstrap/parse.c index 9ab1b53..e32a2a3 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -786,6 +786,7 @@ parsearrini(struct parser *P, const struct type *ty) { fatal(P, e.span, "excess elements in array initializer"); vec_push(&args, ((struct iniarg) { .idx = iota++, e })); + ex.ini.maxn = MAX(ex.ini.maxn, iota - 1); if (!lexmatch(P, &tok, ',')) { lexexpect(P, '}'); @@ -1511,6 +1512,21 @@ parsevardecl(decl_yielder_t yield, void *yarg, struct parser *P) { fatal(P, tok.span, "incompatible initializer type (%t, expected %t)", ini->ty, ty); + if (ty->t == TYarr && ty->length < 0) { + struct type ty2 = *ty; + if (ini->t == Ezeroini) { + ty2.length = 0; + } else if (ini->t == Eini) { + ty2.length = ini->ini.maxn + 1; + } else { + fatal(P, ini->span, + "initializer of inferred-length array must be compound literal"); + } + ty2.size = ty->child->size * ty2.length; + ty2.align = ty->child->align; + ty = interntype(ty2); + } + if (!completetype(ty)) fatal(P, tok.span, "let `%s': variable type %t is incomplete", name, ty); -- cgit v1.2.3