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 ++++++++++++++++++++++ keywords.def | 1 + 2 files changed, 23 insertions(+) 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) { diff --git a/keywords.def b/keywords.def index e0bb92f..258a396 100644 --- a/keywords.def +++ b/keywords.def @@ -13,6 +13,7 @@ _(_Imaginary, STDC99) _(_Noreturn, STDC11) _(_Static_assert, STDC11) _(_Thread_local, STDC11) +_(__typeof__, 0) _(alignas, STDC23) _(alignof, STDC23) _(auto, 0) -- cgit v1.2.3