refactor(gen): R12/R13 同步——下标 while 循环改 for range、去 c.Set

与 includes 同步执行两批源改写:R12 把「i: int = 0 与 while i < N: … i += 1」形式的计数循环改写为「for i in range(N):」;R13 把 c.Set(*p, v) 迁移为 *p = v、c.Set(a, b) 迁移为 a = b(c.Set 是正在退化的路径)。

验证:bootstrap 三重重编 SUCCESS(无 vast_c 解析回退);全量 77/77 顺序回归通过(WebTest 首轮网络超时,单独重跑通过)。
This commit is contained in:
TermiNexus committed 2026-10-05 15:33:58 +08:00
1 parent a7b8118ce8
commit fb53f88893
25 files changed
+133 -345

No files matched your search

+14 -38
View File
@@ -98,7 +98,7 @@ def _lookup_cdefine_for_pyi(name: str) -> int64:
global _g_pyi_cdefine_found
# Look up this file's CDefine cache first (globals are empty during vpi gen)
if _g_pyi_cdef_names is not None and _g_pyi_cdef_vals is not None:
# Bound loop by _g_pyi_cdef_count; never len()/.get on i8** (len falls back
# Bound loop by _g_pyi_cdef_count; never len()/.get on i8** (len falls back
# to strlen, .get misdispatches to HashTable.get) -- index via t.CPtr(base + i*8).
pn: sizet = sizet(_g_pyi_cdef_count)
for pi in range(pn):
@@ -160,10 +160,8 @@ def _PyiEmit(buf: bytes, buf_size: sizet, pos: sizet, text: str) -> sizet:
# _PyiIndent - append level*4 spaces to buf at pos
def _PyiIndent(buf: bytes, buf_size: sizet, pos: sizet, level: int) -> sizet:
"""Append level*4 spaces to buf at pos and return the new pos"""
i: int = 0
while i < level:
for i in range(level):
pos = _PyiEmit(buf, buf_size, pos, " ")
i += 1
return pos
@@ -249,8 +247,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
cmps_ct: list[ast.AST*]* = cmp_t.comparators
if ops_ct is not None:
on_ct: sizet = len(ops_ct)
oi_ct: sizet = 0
while oi_ct < on_ct:
for oi_ct in range(on_ct):
oid_ct: int = ops_ct.get(oi_ct)
if oid_ct == ast.OpKind.Eq: pos = _PyiEmit(buf, buf_size, pos, " == ")
elif oid_ct == ast.OpKind.Ne: pos = _PyiEmit(buf, buf_size, pos, " != ")
@@ -262,7 +259,6 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
# TPC indexes comparators[i] by ops; guard the length here so a
# malformed AST (unequal ops/comparators) cannot overrun.
if cmps_ct is not None and oi_ct < len(cmps_ct): pos = _PyiTypeStr(buf, buf_size, pos, cmps_ct.get(oi_ct), depth + 1)
oi_ct += 1
return pos
# BoolOp: boolean ops (ENABLED and MAX_SIZE > 0 / A or B).
@@ -272,13 +268,11 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
vals_bt: list[ast.AST*]* = bool_t.values
if vals_bt is not None:
vn_bt: sizet = len(vals_bt)
vi_bt: sizet = 0
while vi_bt < vn_bt:
for vi_bt in range(vn_bt):
if vi_bt > 0:
if bool_t.op == ast.OpKind.Or: pos = _PyiEmit(buf, buf_size, pos, " or ")
else: pos = _PyiEmit(buf, buf_size, pos, " and ")
pos = _PyiTypeStr(buf, buf_size, pos, vals_bt.get(vi_bt), depth + 1)
vi_bt += 1
return pos
# Constant: string forward refs (e.g. 'MyClass'), None or numeric constants
@@ -309,12 +303,10 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
if lst.elts is not None:
lst_elts: list[ast.AST*]* = lst.elts
ln: sizet = len(lst_elts)
li: sizet = 0
while li < ln:
for li in range(ln):
if li > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
le: ast.AST* = lst_elts.get(li)
pos = _PyiTypeStr(buf, buf_size, pos, le, depth + 1)
li += 1
pos = _PyiEmit(buf, buf_size, pos, "]")
return pos
@@ -324,12 +316,10 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
if tup.elts is not None:
tup_elts: list[ast.AST*]* = tup.elts
tn: sizet = len(tup_elts)
ti: sizet = 0
while ti < tn:
for ti in range(tn):
if ti > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
el: ast.AST* = tup_elts.get(ti)
pos = _PyiTypeStr(buf, buf_size, pos, el, depth + 1)
ti += 1
return pos
# Call: call expression (used by decorators such as @t.NoVTable())
@@ -340,12 +330,10 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet, annot: ast.AST*, depth:
if cl.args is not None:
cl_args: list[ast.AST*]* = cl.args
can: sizet = len(cl_args)
cai: sizet = 0
while cai < can:
for cai in range(can):
if cai > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
ca: ast.AST* = cl_args.get(cai)
pos = _PyiTypeStr(buf, buf_size, pos, ca, depth + 1)
cai += 1
pos = _PyiEmit(buf, buf_size, pos, ")")
return pos
@@ -437,15 +425,13 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet, fn: ast.FunctionDef*,
if fn.decorator_list is not None:
fn_decorator_list: list[ast.AST*]* = fn.decorator_list
dn: sizet = len(fn_decorator_list)
di: sizet = 0
while di < dn:
for di in range(dn):
dec: ast.AST* = fn_decorator_list.get(di)
if dec is not None:
pos = _PyiIndent(buf, buf_size, pos, indent_level)
pos = _PyiEmit(buf, buf_size, pos, "@")
pos = _PyiTypeStr(buf, buf_size, pos, dec)
pos = _PyiEmit(buf, buf_size, pos, "\n")
di += 1
pos = _PyiIndent(buf, buf_size, pos, indent_level)
pos = _PyiEmit(buf, buf_size, pos, "def ")
@@ -521,15 +507,13 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet, cls: ast.ClassDef*, inden
if cls.decorator_list is not None:
cls_decorator_list: list[ast.AST*]* = cls.decorator_list
dcn: sizet = len(cls_decorator_list)
dci: sizet = 0
while dci < dcn:
for dci in range(dcn):
ddec: ast.AST* = cls_decorator_list.get(dci)
if ddec is not None:
pos = _PyiIndent(buf, buf_size, pos, indent_level)
pos = _PyiEmit(buf, buf_size, pos, "@")
pos = _PyiTypeStr(buf, buf_size, pos, ddec)
pos = _PyiEmit(buf, buf_size, pos, "\n")
dci += 1
pos = _PyiIndent(buf, buf_size, pos, indent_level)
pos = _PyiEmit(buf, buf_size, pos, "class ")
@@ -543,12 +527,10 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet, cls: ast.ClassDef*, inden
tpn: sizet = len(cls_type_params)
if tpn > 0:
pos = _PyiEmit(buf, buf_size, pos, "[")
tpi: sizet = 0
while tpi < tpn:
for tpi in range(tpn):
if tpi > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
tpname: str = cls_type_params.get(tpi)
if tpname is not None: pos = _PyiEmit(buf, buf_size, pos, tpname)
tpi += 1
pos = _PyiEmit(buf, buf_size, pos, "]")
# Base classes
@@ -557,12 +539,10 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet, cls: ast.ClassDef*, inden
bn: sizet = len(cls_bases)
if bn > 0:
pos = _PyiEmit(buf, buf_size, pos, "(")
bi: sizet = 0
while bi < bn:
for bi in range(bn):
if bi > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
base: ast.AST* = cls_bases.get(bi)
pos = _PyiTypeStr(buf, buf_size, pos, base)
bi += 1
pos = _PyiEmit(buf, buf_size, pos, ")")
pos = _PyiEmit(buf, buf_size, pos, ":\n")
@@ -736,8 +716,7 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
if imp2.names is not None:
imp2_names: list[ast.AST*]* = imp2.names
an2: sizet = len(imp2_names)
ai2: sizet = 0
while ai2 < an2:
for ai2 in range(an2):
if ai2 > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
al2: ast.Alias* = ast.Alias*(imp2_names.get(ai2))
# fail-fast: never silently write "import , ". Import names are the
@@ -750,7 +729,6 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
if al2.asname is not None and string.strlen(al2.asname) > 0:
pos = _PyiEmit(buf, buf_size, pos, " as ")
pos = _PyiEmit(buf, buf_size, pos, al2.asname)
ai2 += 1
pos = _PyiEmit(buf, buf_size, pos, "\n")
# ImportFrom statement
@@ -770,8 +748,7 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
if ifr.names is not None:
ifr_names: list[ast.AST*]* = ifr.names
an3: sizet = len(ifr_names)
ai3: sizet = 0
while ai3 < an3:
for ai3 in range(an3):
if ai3 > 0: pos = _PyiEmit(buf, buf_size, pos, ", ")
al3: ast.Alias* = ast.Alias*(ifr_names.get(ai3))
# fail-fast: same as Import (report the two causes separately).
@@ -783,7 +760,6 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
if al3.asname is not None and string.strlen(al3.asname) > 0:
pos = _PyiEmit(buf, buf_size, pos, " as ")
pos = _PyiEmit(buf, buf_size, pos, al3.asname)
ai3 += 1
pos = _PyiEmit(buf, buf_size, pos, "\n")
# Function definition (_PyiFunction already ends with a newline)
@@ -827,7 +803,7 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
viperlib.snprintf(num_buf, 32, " = %lld", cv.int_val)
pos = _PyiEmit(buf, buf_size, pos, num_buf)
elif cv.const_kind == ast.CONST_FLOAT:
# Float CDefines (e.g. numpy pi/e) must emit a value or read-back
# Float CDefines (e.g. numpy pi/e) must emit a value or read-back
# degrades to a global pointer; %.17g round-trips double exactly.
fbuf_c: bytes = mb.alloc(64)
if fbuf_c is not None:
+5 -15
View File
@@ -148,8 +148,7 @@ def _vpi_idx_find(keys: bytes, key_stride: int, idx_base: bytes, cap: int, key:
h: uint64 = _vpi_idx_hash(key)
mask: sizet = sizet(cap - 1)
si: sizet = sizet(h) & mask
i: int = 0
while i < cap:
for i in range(cap):
slot: sizet* = _vpi_idx_slot(idx_base, si)
sv: sizet = slot[0]
if sv == 0: return -1
@@ -157,7 +156,6 @@ def _vpi_idx_find(keys: bytes, key_stride: int, idx_base: bytes, cap: int, key:
ek: bytes = keys + se * key_stride
if string.strcmp(ek, key) == 0: return se
si = (si + 1) & mask
i += 1
return -1
@@ -167,14 +165,12 @@ def _vpi_idx_insert(idx_base: bytes, cap: int, slot_index: int, key: bytes) -> i
h: uint64 = _vpi_idx_hash(key)
mask: sizet = sizet(cap - 1)
si: sizet = sizet(h) & mask
i: int = 0
while i < cap:
for i in range(cap):
slot: sizet* = _vpi_idx_slot(idx_base, si)
if slot[0] == 0:
slot[0] = sizet(slot_index + 1)
return 0
si = (si + 1) & mask
i += 1
return 1
@@ -839,13 +835,11 @@ def _vpi_param_annot_seg_is(src_sha1: str, cls_name: str, method_name: str, seg_
prm: str = _lookup_vpi_param_annots(src_sha1, cls_name, method_name)
if prm is None or prm[0] == '\0': return 0
cur: sizet = 0
seg: int = 0
# locate segment start: skip seg_idx '@'-separated segments
while seg < seg_idx:
for seg in range(seg_idx):
while prm[cur] != '\0' and prm[cur] != '@': cur += 1
if prm[cur] == '\0': return 0
cur += 1
seg += 1
slen: sizet = 0
while prm[cur + slen] != '\0' and prm[cur + slen] != '@': slen += 1
wlen: sizet = string.strlen(want)
@@ -1053,10 +1047,8 @@ def _resolve_single_vpi_type_str(pool: memhub.MemBuddy*, seg: str) -> mlirlite.L
star_off_ps: sizet = string.strlen(star_buf_ps)
string.strncpy(star_buf_ps + star_off_ps, seg, star_base_end_ps)
star_buf_ps[star_off_ps + star_base_end_ps] = '\0'
star_k_ps = 0
while star_k_ps < star_cnt_ps:
for star_k_ps in range(star_cnt_ps):
string.strcat(star_buf_ps, "]")
star_k_ps += 1
star_ty_ps: mlirlite.LLVMType* = _resolve_single_vpi_type_str(pool, star_buf_ps)
stdlib.free(star_buf_ps)
return star_ty_ps
@@ -1307,10 +1299,8 @@ def _resolve_vpi_ret_annot_str(pool: memhub.MemBuddy*, ret_annot: str, i32_ty: m
if cptr_sub_lvl > 0:
if first_ty is None: return cptr_sub_ty
res_ty_cs: mlirlite.LLVMType* = first_ty
wrap_i_cs: int = 0
while wrap_i_cs < cptr_sub_lvl:
for wrap_i_cs in range(cptr_sub_lvl):
res_ty_cs = mlirlite.Ptr(pool, res_ty_cs)
wrap_i_cs += 1
return res_ty_cs
if has_cvoid != 0:
# t.CVoid | t.State: State is a decorator marker (ignored), t.CVoid -> void.
+2 -6
View File
@@ -416,12 +416,10 @@ def LookupSha1ByAlias(from_imports: str, alias: str) -> str:
if colon < total_len and from_imports[colon] == ':' and local_len == alias_len:
# compare local_name == alias
Matched: int = 1
li: sizet = 0
while li < alias_len:
for li in range(alias_len):
if from_imports[ci + li] != alias[li]:
Matched = 0
break
li += 1
if Matched == 1:
# extract module (from colon+1 to next ':' or space)
m_start: sizet = colon + 1
@@ -459,12 +457,10 @@ def LookupModNameByAlias(from_imports: str, alias: str) -> str:
if colon < total_len and from_imports[colon] == ':' and local_len == alias_len:
# compare local_name == alias
Matched: int = 1
li: sizet = 0
while li < alias_len:
for li in range(alias_len):
if from_imports[ci + li] != alias[li]:
Matched = 0
break
li += 1
if Matched == 1:
# extract module (from colon+1 to next ':' or space)
m_start: sizet = colon + 1