feat(stub): .vpi 输出新语法(rawptr/novirtual/virtual/tls)

类型名不再输出 CPtr/CInt 等 C* 标记,改为 rawptr/int/sizet 等内建短名;@NoVTable/@CVTable 输出 novirtual/virtual class,TLS/CStatic/CExport/CExtern 输出关键字前缀。
This commit is contained in:
TermiNexus committed 2026-10-18 14:25:22 +08:00
1 parent 700a0ba602
commit 94557ad2c0
1 file changed
+48 -10
+48 -10
View File
@@ -19,6 +19,30 @@ _ConvertCacheMaxSize: int = 4096
_RE_LINE_COMMENT: re.Pattern[str] = re.compile(r'#.*$')
_STUB_TYPE_ALIASES: dict[str, str] = {
'CPtr': 'rawptr', 'CArrayPtr': 'rawptr', 'CAutoPtr': 'rawptr',
'CRef': 'rawptr', 'CTempRef': 'rawptr',
'CInt': 'int', 'CChar': 'char', 'CBool': 'bool', 'CVoid': 'void',
'CFloat': 'float', 'CDouble': 'double',
'CUnsignedChar': 'uchar', 'CUnsignedShort': 'ushort', 'CUnsignedInt': 'uint',
'CUnsignedLong': 'ulong', 'CUnsignedLongLong': 'ulonglong',
'CSignedChar': 'int8', 'CShort': 'short', 'CLong': 'long', 'CLongLong': 'longlong',
'CSizeT': 'sizet', 'CIntPtrT': 'int64', 'CUIntPtrT': 'uint64', 'CPtrDiffT': 'ptrdifft',
'CInt8T': 'int8', 'CInt16T': 'int16', 'CInt32T': 'int32', 'CInt64T': 'int64',
'CUInt8T': 'uint8', 'CUInt16T': 'uint16', 'CUInt32T': 'uint32', 'CUInt64T': 'uint64',
'CIntFast8T': 'intfast8', 'CIntFast16T': 'intfast16',
'CIntFast32T': 'intfast32', 'CIntFast64T': 'intfast64',
'CUIntFast8T': 'uintfast8', 'CUIntFast16T': 'uintfast16',
'CUIntFast32T': 'uintfast32', 'CUIntFast64T': 'uintfast64',
'CIntLeast8T': 'intleast8', 'CIntLeast16T': 'intleast16',
'CIntLeast32T': 'intleast32', 'CIntLeast64T': 'intleast64',
'CUIntLeast8T': 'uintleast8', 'CUIntLeast16T': 'uintleast16',
'CUIntLeast32T': 'uintleast32', 'CUIntLeast64T': 'uintleast64',
'CConst': 'const', 'CVolatile': 'volatile', 'CStatic': 'static',
'CExport': 'export', 'CExtern': 'extern', 'CInline': 'inline',
}
class PythonToStubConverter:
"""将 Python 文件转换为存根格式"""
@@ -237,10 +261,15 @@ class PythonToStubConverter:
PostdefVars = PostdefVars or []
IndentStr: str = ' ' * indent
# 处理类装饰器
# 处理类装饰器:virtual/novirtual 还原为关键字前缀
ClassPrefix: str = ''
for decorator in node.decorator_list:
DecoratorStr: str = PythonToStubConverter._GetDecoratorString(decorator)
if DecoratorStr:
if DecoratorStr == 'NoVTable':
ClassPrefix = 'novirtual '
elif DecoratorStr == 'CVTable':
ClassPrefix = 'virtual '
elif DecoratorStr:
lines.append(f'{IndentStr}@{DecoratorStr}')
# 检查是否是结构体、联合体或枚举
@@ -264,15 +293,15 @@ class PythonToStubConverter:
param_names: list[str] = [tp.name for tp in node.type_params]
ClassTypeParamStr = f'[{", ".join(param_names)}]'
if BaseStrs:
lines.append(f'{IndentStr}class {node.name}{ClassTypeParamStr}({", ".join(BaseStrs)}):')
lines.append(f'{IndentStr}{ClassPrefix}class {node.name}{ClassTypeParamStr}({", ".join(BaseStrs)}):')
else:
lines.append(f'{IndentStr}class {node.name}{ClassTypeParamStr}:')
lines.append(f'{IndentStr}{ClassPrefix}class {node.name}{ClassTypeParamStr}:')
else:
ClassTypeParamStr = ''
if hasattr(node, 'type_params') and node.type_params:
param_names = [tp.name for tp in node.type_params]
ClassTypeParamStr = f'[{", ".join(param_names)}]'
lines.append(f'{IndentStr}class {node.name}{ClassTypeParamStr}:')
lines.append(f'{IndentStr}{ClassPrefix}class {node.name}{ClassTypeParamStr}:')
# 处理类成员
HasMembers: bool = False
@@ -485,10 +514,19 @@ class PythonToStubConverter:
lines.append(f'{IndentStr}pass')
return lines
# 处理函数装饰器
# 处理函数装饰器:存储类/一次性初始化还原为关键字前缀
FuncPrefix: str = ''
for decorator in node.decorator_list:
DecoratorStr: str = PythonToStubConverter._GetDecoratorString(decorator)
if DecoratorStr:
if DecoratorStr == 'TLS':
FuncPrefix = 'tls '
elif DecoratorStr == 'CStatic':
FuncPrefix = 'static '
elif DecoratorStr == 'CExport':
FuncPrefix = 'export '
elif DecoratorStr == 'CExtern':
FuncPrefix = 'extern '
elif DecoratorStr:
lines.append(f'{IndentStr}@{DecoratorStr}')
# 构建参数列表
@@ -552,7 +590,7 @@ class PythonToStubConverter:
param_names: list[str] = [tp.name for tp in node.type_params]
TypeParamStr = f'[{", ".join(param_names)}]'
lines.append(f'{IndentStr}def {node.name}{TypeParamStr}({ParamStr}) -> {ReturnType}: pass')
lines.append(f'{IndentStr}{FuncPrefix}def {node.name}{TypeParamStr}({ParamStr}) -> {ReturnType}: pass')
return lines
@staticmethod
@@ -674,11 +712,11 @@ class PythonToStubConverter:
if annotation is None:
return 'CVoid'
if isinstance(annotation, ast.Name):
return annotation.id
return _STUB_TYPE_ALIASES.get(annotation.id, annotation.id)
elif isinstance(annotation, ast.Attribute):
if isinstance(annotation.value, ast.Name):
if annotation.value.id in ('t', 'c'):
return annotation.attr
return _STUB_TYPE_ALIASES.get(annotation.attr, annotation.attr)
return f'{annotation.value.id}.{annotation.attr}'
else:
# 处理嵌套属性访问,如 attr.packed