feat(gen): typedef 源文本归一 C 式 const/T*(R16)
TPV 的 typedef 注册走裸源码扫描,需与 TPC 同形:_parse_typedef_line 把右值的前导 const/volatile 归一为 t.CConst | / t.CVolatile |、末尾 * 归一为 | t.CPtr;register_ctypedef_from_source 的缓冲 320->512 以容纳展开后的文本。
This commit is contained in:
1 parent
374921623b
commit
0cf80632c1
2 files changed
+42
-5
No files matched your search
@@ -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
|
||||
|
||||
@@ -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."""
|
||||
|
||||
Reference in new issue
Block a user