From 3048a0b59baae16727d0c259353ff4be1ae559b4 Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 12 Oct 2025 10:06:06 +0200 Subject: typeof --- c.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'c.c') diff --git a/c.c b/c.c index 30ea13a..29028b2 100644 --- a/c.c +++ b/c.c @@ -102,6 +102,7 @@ isdecltok(struct comp *cm) case TKWextern: case TKWstatic: case TKWinline: case TKW_Noreturn: case TKWconst: case TKWvolatile: case TKWvoid: case TKWfloat: case TKWdouble: case TKWregister: case TKW_Static_assert: + case TKW__typeof__: case TKWtypeof: case TKWtypeof_unqual: return 1; case TKIDENT: return (decl = finddecl(cm, tk.s)) && decl->scls == SCTYPEDEF; @@ -1837,6 +1838,21 @@ tagtype(struct comp *cm, enum toktag kind) return t; } +static union type +ptypeof(struct comp *cm) +{ + union type ty; + expect(cm, '(', NULL); + if (isdecltok(cm)) { /* typeof (type) */ + struct declstate st = { DCASTEXPR }; + ty = pdecl(&st, cm).ty; + } else { /* typeof (expr) */ + ty = commaexpr(cm).ty; + } + expect(cm, ')', NULL); + return ty; +} + static void declspec(struct declstate *st, struct comp *cm) { @@ -1924,6 +1940,12 @@ declspec(struct declstate *st, struct comp *cm) if (!span.ex.len) span.ex = tk.span.ex; joinspan(&span.ex, tk.span.ex); goto End; + case TKW__typeof__: case TKWtypeof: + lex(cm, &tk); + st->base = ptypeof(cm); + if (!span.ex.len) span.ex = tk.span.ex; + joinspan(&span.ex, tk.span.ex); + goto End; case TKIDENT: if (!st->base.t && !arith && (decl = finddecl(cm, tk.s)) && decl->scls == SCTYPEDEF) { -- cgit v1.2.3