refactor(viperc): 退役 t/c 模块标记,内建类型改由编译器直接识别

This commit is contained in:
TermiNexus committed 2026-10-18 12:56:57 +08:00
1 parent 3242560d02
commit 42b094a575
25 files changed
+437 -660

No files matched your search

+5 -27
View File
@@ -776,27 +776,8 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
tree_children: list[ast.AST*]* = tree.children
n: sizet = len(tree_children)
# Pass 1: detect import t/c
has_import_t: int = 0
has_import_c: int = 0
for i in range(int(n)):
stmt: ast.AST* = tree_children.get(i)
if stmt is not None and stmt.kind() == ast.ASTKind.Import:
imp: ast.Import* = ast.Import*(stmt)
if imp.names is not None:
imp_names: list[ast.AST*]* = imp.names
an: sizet = len(imp_names)
for ai in range(int(an)):
al: ast.Alias* = ast.Alias*(imp_names.get(ai))
if al is not None and al.name is not None:
if string.strcmp(al.name, "t") == 0: has_import_t = 1
elif string.strcmp(al.name, "c") == 0: has_import_c = 1
# Add missing import t/c
if has_import_t == 0 and has_import_c == 0: pos = _PyiEmit(buf, buf_size, pos, "import t, c\n\n")
elif has_import_t == 0: pos = _PyiEmit(buf, buf_size, pos, "import t\n\n")
elif has_import_c == 0: pos = _PyiEmit(buf, buf_size, pos, "import c\n\n")
else: pos = _PyiEmit(buf, buf_size, pos, "\n")
# t / c are retired compiler-internal modules: the stub never injects their imports.
pos = _PyiEmit(buf, buf_size, pos, "\n")
# Pass 2: emit declarations
for i in range(int(n)):
@@ -886,12 +867,9 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*, rel_path: str, buf
# If there is a value (e.g. a CDefine constant) emit = value.
# CTypedef is special: its value is a type expression, rendered by _PyiTypeStr.
is_ctypedef_ann: int = 0
if aa.annotation is not None and aa.annotation.kind() == ast.ASTKind.Attribute:
ctd_at: ast.Attribute* = ast.Attribute*(aa.annotation)
if ctd_at.attr is not None and string.strcmp(ctd_at.attr, "CTypedef") == 0:
if ctd_at.value is not None and ctd_at.value.kind() == ast.ASTKind.Name:
ctd_mod: ast.Name* = ast.Name*(ctd_at.value)
if ctd_mod.id is not None and string.strcmp(ctd_mod.id, "t") == 0: is_ctypedef_ann = 1
if aa.annotation is not None and aa.annotation.kind() == ast.ASTKind.Name:
ctd_nm: ast.Name* = ast.Name*(aa.annotation)
if ctd_nm.id is not None and string.strcmp(ctd_nm.id, "CTypedef") == 0: is_ctypedef_ann = 1
if is_ctypedef_ann != 0 and aa.value is not None:
pos = _PyiEmit(buf, buf_size, pos, " = ")
pos = _PyiTypeStr(buf, buf_size, pos, aa.value)
+18 -26
View File
@@ -1645,10 +1645,10 @@ def _resolve_single_vpi_type_str(pool: memhub.MemBuddy*, seg: str) -> mlirlite.L
stdlib.free(inner_buf_rp)
if inner_ty_rp is not None: return mlirlite.Ptr(pool, inner_ty_rp)
return None
# t.CPtr[T] / c.CPtr[T] subscript pointers (nesting allowed, e.g. t.CPtr[t.CPtr]
# -> Ptr(Ptr(i8))); same as TypeGenResolve's Subscript(CPtr) branch: Ptr(resolve(T)).
# CPtr[T] subscript pointers (nesting allowed, e.g. CPtr[CPtr] -> Ptr(Ptr(i8)));
# same as TypeGenResolve's Subscript(CPtr) branch: Ptr(resolve(T)).
cptr_sub_pfx: sizet = 0
if string.strncmp(seg, "t.CPtr[", 7) == 0 or string.strncmp(seg, "c.CPtr[", 7) == 0: cptr_sub_pfx = 7
if string.strncmp(seg, "CPtr[", 6) == 0: cptr_sub_pfx = 6
if cptr_sub_pfx > 0:
seg_len_cp: sizet = string.strlen(seg)
if seg_len_cp > cptr_sub_pfx + 1 and seg[seg_len_cp - 1] == ']':
@@ -1661,15 +1661,12 @@ def _resolve_single_vpi_type_str(pool: memhub.MemBuddy*, seg: str) -> mlirlite.L
stdlib.free(inner_buf_cp)
if inner_ty_cp is not None: return mlirlite.Ptr(pool, inner_ty_cp)
return None
# t.XXX / c.XXX built-in type mapping
if string.strncmp(seg, "t.", 2) == 0 or string.strncmp(seg, "c.", 2) == 0:
attr_nm: str = seg + 2
# t.CExport is an export marker, not a real type; FuncGenDef maps it to Void,
# Bare builtin type names (int8 / uint64 / sizet / CPtr / CExport ...)
if string.strchr(seg, '.') is None:
# CExport is an export marker, not a real type; FuncGenDef maps it to Void,
# so normalize it here too or cross-module return types fail fast.
if string.strcmp(attr_nm, "CExport") == 0: return mlirlite.Void(pool)
# [stage 2] unified: CTypedef dynamic table first (library typedefs analyzed normally),
# on miss fall back to map_t_type (t/c library internal special-casing).
mapped_ty: mlirlite.LLVMType* = GenApi.resolve_typedef_or_map(pool, attr_nm, None, None)
if string.strcmp(seg, "CExport") == 0: return mlirlite.Void(pool)
mapped_ty: mlirlite.LLVMType* = GenApi.resolve_typedef_or_map(pool, seg, None, None)
if mapped_ty is not None: return mapped_ty
return None
# module-qualified class name "memhub.MemBuddy" -> use last segment to look up struct
@@ -1689,7 +1686,7 @@ def _resolve_single_vpi_type_str(pool: memhub.MemBuddy*, seg: str) -> mlirlite.L
# bare enum name -> EnumTable fallback (as above: enums are not in StructTable)
bare_enum_ty: mlirlite.LLVMType* = GenApi.get_enum_base_type(seg)
if bare_enum_ty is not None: return bare_enum_ty
# bare typedef name (e.g. VOIDPTR): CTypedef dynamic table first, on miss fall back to map_t_type
# bare typedef name (e.g. VOIDPTR): CTypedef dynamic table first, on miss fall back to map_builtin_type
mapped_bare: mlirlite.LLVMType* = GenApi.resolve_typedef_or_map(pool, seg, None, None)
if mapped_bare is not None: return mapped_bare
return None
@@ -1793,7 +1790,7 @@ def _resolve_vpi_ret_annot_str(pool: memhub.MemBuddy*, ret_annot: str, i32_ty: m
seg_buf[seg_len2] = '\0'
# CPtr subscript segments must be recognized first: the definition side treats
# `X*[T]` as "CPtr side contributes N pointer levels, the other side the base".
if string.strncmp(seg_buf, "t.CPtr[", 7) == 0 or string.strncmp(seg_buf, "c.CPtr[", 7) == 0:
if string.strncmp(seg_buf, "CPtr[", 6) == 0:
seg_ty_cs: mlirlite.LLVMType* = _resolve_single_vpi_type_str(pool, seg_buf)
if seg_ty_cs is not None:
# Count outer Ptr nesting (same as the definition side). Do not update the
@@ -1813,10 +1810,10 @@ def _resolve_vpi_ret_annot_str(pool: memhub.MemBuddy*, ret_annot: str, i32_ty: m
if lvl_cs > cptr_sub_lvl:
cptr_sub_lvl = lvl_cs
cptr_sub_ty = seg_ty_cs
elif string.strcmp(seg_buf, "t.CPtr") == 0 or string.strcmp(seg_buf, "CPtr") == 0: has_cptr = 1
elif string.strcmp(seg_buf, "t.CRef") == 0 or string.strcmp(seg_buf, "CRef") == 0: has_cref = 1
elif string.strcmp(seg_buf, "t.CTempRef") == 0 or string.strcmp(seg_buf, "CTempRef") == 0: has_cref = 1
elif string.strcmp(seg_buf, "void") == 0 or string.strcmp(seg_buf, "CVoid") == 0 or string.strcmp(seg_buf, "void") == 0:
elif string.strcmp(seg_buf, "CPtr") == 0: has_cptr = 1
elif string.strcmp(seg_buf, "CRef") == 0: has_cref = 1
elif string.strcmp(seg_buf, "CTempRef") == 0: has_cref = 1
elif string.strcmp(seg_buf, "void") == 0 or string.strcmp(seg_buf, "CVoid") == 0:
# Bare void is an alias of t.CVoid and must be handled the same, else the
# else branch sets first_td and produces the illegal Ptr(Void) (void* = i8*).
has_cvoid = 1
@@ -2614,9 +2611,9 @@ def _vpi_class_has_packed(decorator_list: list[ast.AST*]*) -> int:
if deco is None: continue
if deco.kind() == ast.ASTKind.Call:
cl: ast.Call* = ast.Call*(deco)
if cl.func is None or cl.func.kind() != ast.ASTKind.Attribute: continue
at0: ast.Attribute* = ast.Attribute*(cl.func)
if at0.attr is None or string.strcmp(at0.attr, "Attribute") != 0: continue
if cl.func is None or cl.func.kind() != ast.ASTKind.Name: continue
at0: ast.Name* = ast.Name*(cl.func)
if at0.id is None or string.strcmp(at0.id, "Attribute") != 0: continue
if cl.args is None: continue
an: sizet = len(cl.args)
for ai in range(int(an)):
@@ -3527,12 +3524,7 @@ def _repl_stub_deco_flags(deco_list: list[ast.AST*]*) -> int:
if deco is None: continue
nm: str = None
k: int = deco.kind()
if k == ast.ASTKind.Attribute:
at: ast.Attribute* = ast.Attribute*(deco)
if at.attr is not None and at.value is not None and at.value.kind() == ast.ASTKind.Name:
vn: ast.Name* = ast.Name*(at.value)
if vn.id is not None and string.strcmp(vn.id, "t") == 0: nm = at.attr
elif k == ast.ASTKind.Name:
if k == ast.ASTKind.Name:
nn: ast.Name* = ast.Name*(deco)
nm = nn.id
if nm is None: continue