fix(stub): .vpi 保留 extern 关键字与 T* 指针写法
符号表字段改为从 AST Markers 渲染:extern/static/export 变量输出关键字前缀;CPtr[T]/rawptr[T] 输出 T*,不再输出 CPtr[...] 或 CExtern | T。
This commit is contained in:
1 parent
660a15bef8
commit
e770e1964f
1 file changed
+26
-5
@@ -562,8 +562,10 @@ class PythonToStubConverter:
|
||||
if isinstance(node.target, ast.Name):
|
||||
VarName: str = node.target.id
|
||||
TypeStr: str = PythonToStubConverter._GetTypeString(node.annotation)
|
||||
# 检查是否是宏变量(类型包含 CDefine)
|
||||
if 'CDefine' in TypeStr:
|
||||
Markers: tuple[str, ...] = (tuple(getattr(node, 'Markers', None) or ())
|
||||
+ tuple(getattr(node.annotation, 'Markers', None) or ()))
|
||||
# 检查是否是宏变量(类型包含 CDefine 或 Markers 标记 define)
|
||||
if 'CDefine' in TypeStr or 'define' in Markers:
|
||||
# 检查宏变量是否应该被排除
|
||||
if PythonToStubConverter._ShouldExcludeMacro(VarName):
|
||||
return lines # 返回空列表,排除该宏
|
||||
@@ -578,13 +580,17 @@ class PythonToStubConverter:
|
||||
return lines
|
||||
# 非宏变量:不加 State
|
||||
# 含有 typedef 的变量不加 extern,普通变量需要加 extern
|
||||
has_typedef: bool = 'CTypedef' in TypeStr or 'CTypedef' in TypeStr
|
||||
has_typedef: bool = 'CTypedef' in TypeStr or 'typedef' in Markers
|
||||
if has_typedef and node.value is not None:
|
||||
ValueTypeStr: str = PythonToStubConverter._GetTypeString(node.value)
|
||||
lines.append(f'{VarName}: {TypeStr} = {ValueTypeStr}')
|
||||
elif 'extern' in Markers:
|
||||
lines.append(f'extern {VarName}: {TypeStr}')
|
||||
elif 'static' in Markers:
|
||||
lines.append(f'static {VarName}: {TypeStr}')
|
||||
elif 'export' in Markers:
|
||||
lines.append(f'export {VarName}: {TypeStr}')
|
||||
else:
|
||||
if not has_typedef and 'CExtern' not in TypeStr and 'CStatic' not in TypeStr:
|
||||
TypeStr = f'CExtern | {TypeStr}'
|
||||
lines.append(f'{VarName}: {TypeStr}')
|
||||
return lines
|
||||
|
||||
@@ -646,6 +652,15 @@ class PythonToStubConverter:
|
||||
|
||||
@staticmethod
|
||||
def _GetTypeString(annotation: ast.AST) -> str:
|
||||
if annotation is None:
|
||||
return 'CVoid'
|
||||
Core: str = PythonToStubConverter._GetTypeStringCore(annotation)
|
||||
Markers: tuple[str, ...] = tuple(getattr(annotation, 'Markers', None) or ())
|
||||
Prefix: str = ''.join(f'{m} ' for m in Markers if m in ('const', 'volatile', 'inline'))
|
||||
return Prefix + Core
|
||||
|
||||
@staticmethod
|
||||
def _GetTypeStringCore(annotation: ast.AST) -> str:
|
||||
"""获取类型字符串
|
||||
|
||||
支持集与 VPC(ViperC/App/lib/Stub/StubGen/Converter.vp 的 _PyiTypeStr)
|
||||
@@ -673,6 +688,12 @@ class PythonToStubConverter:
|
||||
SliceStr: str = ', '.join(slice_strs)
|
||||
else:
|
||||
SliceStr = PythonToStubConverter._GetTypeString(annotation.slice)
|
||||
if base in ('CPtr', 'rawptr'):
|
||||
return PythonToStubConverter._GetTypeString(annotation.slice) + '*'
|
||||
if base == 'CRef':
|
||||
return '&' + PythonToStubConverter._GetTypeString(annotation.slice)
|
||||
if base == 'CTempRef':
|
||||
return '&temp ' + PythonToStubConverter._GetTypeString(annotation.slice)
|
||||
return f'{base}[{SliceStr}]'
|
||||
elif isinstance(annotation, ast.BinOp):
|
||||
# 处理 BinOp,如 CExtern | fd_table.__set_default__(...)
|
||||
|
||||
Reference in new issue
Block a user