From 0cf80632c179183d9e5dbbca8b1a6855afa6507f Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Mon, 5 Oct 2026 20:36:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(gen):=20typedef=20=E6=BA=90=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E5=BD=92=E4=B8=80=20C=20=E5=BC=8F=20const/T*=EF=BC=88?= =?UTF-8?q?R16=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TPV 的 typedef 注册走裸源码扫描,需与 TPC 同形:_parse_typedef_line 把右值的前导 const/volatile 归一为 t.CConst | / t.CVolatile |、末尾 * 归一为 | t.CPtr;register_ctypedef_from_source 的缓冲 320->512 以容纳展开后的文本。 --- App/lib/core/Gen/Type/TypeGenCType.vp | 6 ++-- App/lib/core/Gen/Type/TypeGenResolve.vp | 41 +++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/App/lib/core/Gen/Type/TypeGenCType.vp b/App/lib/core/Gen/Type/TypeGenCType.vp index a0f90ef..b335065 100644 --- a/App/lib/core/Gen/Type/TypeGenCType.vp +++ b/App/lib/core/Gen/Type/TypeGenCType.vp @@ -419,8 +419,10 @@ class TypeGenCType(TypeGenBase.TypeGenBase): src_len: sizet = string.strlen(src) count: int = 0 line_start: sizet = 0 - # line parse buffer ("NAME\0VALUE\0"), reused per file (register_ctypedef copies) - td_buf: str = pool.alloc(320) + # line parse buffer ("NAME\0VALUE\0"), reused per file (register_ctypedef copies). + # 512 (not 320): _parse_typedef_line normalizes a C-style RHS (`const T` / `T*`) into + # the longer canonical `|` union text, so the VALUE area must hold the expanded form. + td_buf: str = pool.alloc(512) for i in range(src_len + 1): if i == src_len or src[i] == '\n': line_len: sizet = i - line_start diff --git a/App/lib/core/Gen/Type/TypeGenResolve.vp b/App/lib/core/Gen/Type/TypeGenResolve.vp index dc880e4..3ef43d6 100644 --- a/App/lib/core/Gen/Type/TypeGenResolve.vp +++ b/App/lib/core/Gen/Type/TypeGenResolve.vp @@ -978,9 +978,44 @@ novirtual class TypeGenResolve(TypeGenBase.TypeGenBase): out_buf[ci] = buf[line_start + name_s + ci] ci += 1 out_buf[name_l] = '\0' - for ci in range(vlen): - out_buf[name_l + 1 + ci] = buf[line_start + vs + ci] - out_buf[name_l + 1 + vlen] = '\0' + # Normalize a C-style typedef RHS (`const T` / `T*`) into the canonical `|` union text, + # mirroring TPC's TypeAnnotationResolver._NormalizeTypedefRhsText: leading const/volatile + # become `t.CConst |` / `t.CVolatile |`, trailing `*` runs become ` | t.CPtr`. Text without + # these forms is copied verbatim (byte-identical to the pre-normalization behavior). + pos: sizet = name_l + 1 + k: sizet = vs + while True: + if k + 6 <= ve and string.strncmp(buf + line_start + k, "const", 5) == 0 and (buf[line_start + k + 5] == ' ' or buf[line_start + k + 5] == '\t'): + string.memcpy(out_buf + pos, "t.CConst | ", 11) + pos += 11 + k += 6 + elif k + 9 <= ve and string.strncmp(buf + line_start + k, "volatile", 8) == 0 and (buf[line_start + k + 8] == ' ' or buf[line_start + k + 8] == '\t'): + string.memcpy(out_buf + pos, "t.CVolatile | ", 14) + pos += 14 + k += 9 + else: + break + while k < ve and (buf[line_start + k] == ' ' or buf[line_start + k] == '\t'): k += 1 + ptrs: sizet = 0 + ve2: sizet = ve + while ve2 > k and buf[line_start + ve2 - 1] == '*': + ptrs += 1 + ve2 -= 1 + while ve2 > k and (buf[line_start + ve2 - 1] == ' ' or buf[line_start + ve2 - 1] == '\t'): ve2 -= 1 + vlen2: sizet = ve2 - k + if vlen2 > 190: vlen2 = 190 + # Output capacity guard (register_ctypedef_from_source allocates 512 bytes for NAME\0VALUE\0). + if pos + vlen2 + ptrs * 11 > 500: + if pos + ptrs * 11 >= 500: ptrs = 0 + if pos + ptrs * 11 >= 500: vlen2 = 0 + else: vlen2 = 500 - pos - ptrs * 11 + for ci2 in range(vlen2): + out_buf[pos + ci2] = buf[line_start + k + ci2] + pos += vlen2 + for pi in range(ptrs): + string.memcpy(out_buf + pos, " | t.CPtr", 9) + pos += 9 + out_buf[pos] = '\0' return 1 def _lookup_builtins_typedef(self, pool: memhub.MemBuddy*, name: str) -> str: """Last-match fallback: look up a CTypedef declared in includes/builtins.vp."""