refactor(type): 全面清理旧联合与 t/c 标记判断
审计并修复 23 个文件:类型注解中的 T1 | T2 一律 fail-fast;typedef/define/export/extern/static/const/volatile/attribute 优先读取 Markers/Attributes/ByteOrder/BitWidth;指针统一 CPtr/rawptr/CRef/CTempRef/CArrayPtr/CAutoPtr Subscript;stub 输出统一新语法。
This commit is contained in:
1 parent
a410de2cf7
commit
0bded58999
23 files changed
+887
-694
No files matched your search
@@ -97,11 +97,9 @@ class DeclarationGenerator:
|
||||
Markers: tuple[str, ...] = tuple(getattr(node, 'Markers', None) or ()) + tuple(
|
||||
getattr(node.annotation, 'Markers', None) or ())
|
||||
is_typedef: bool = 'typedef' in Markers
|
||||
if not is_typedef:
|
||||
if isinstance(node.annotation, ast.Attribute) and getattr(node.annotation, 'attr', '') == 'CTypedef':
|
||||
is_typedef = True
|
||||
elif isinstance(node.annotation, ast.Name) and node.annotation.id == 'CTypedef':
|
||||
is_typedef = True
|
||||
if not is_typedef and AnnotationContainsName(node.annotation, 'CTypedef'):
|
||||
# 兼容旧写法 `名字: CTypedef = 类型`(仅文本判据回落)。
|
||||
is_typedef = True
|
||||
if is_typedef and node.value:
|
||||
self.typedef_map[var_name] = node.value
|
||||
|
||||
@@ -230,22 +228,33 @@ class DeclarationGenerator:
|
||||
return f'declare {ret_type} @{func_name}({param_str})'
|
||||
|
||||
def _is_export_func(self, node: ast.FunctionDef) -> bool:
|
||||
"""检查函数是否标记为 CExport"""
|
||||
"""检查函数是否导出/外部可见(export / extern / state)。
|
||||
|
||||
新语法的 `export def` / `extern def` 由解析器写入 FunctionDef.Export /
|
||||
Extern;旧语法的 `-> CExport | T` / `-> State | T` 仍走注解兼容判据。
|
||||
"""
|
||||
if bool(getattr(node, 'Export', False)) or bool(getattr(node, 'Extern', False)):
|
||||
return True
|
||||
Markers: tuple[str, ...] = getattr(node, 'Markers', None) or ()
|
||||
if 'export' in Markers or 'state' in Markers:
|
||||
if 'export' in Markers or 'extern' in Markers or 'state' in Markers:
|
||||
return True
|
||||
if not node.returns:
|
||||
return False
|
||||
return self._check_annotation_for_export(node.returns)
|
||||
|
||||
def _check_annotation_for_export(self, annotation: ast.AST | None) -> bool:
|
||||
"""检查类型注解是否带 export / state 标记"""
|
||||
"""检查类型注解是否带 export / extern / state 标记。
|
||||
|
||||
优先读 AST 的 Markers 字段;旧的 CExport/State 文本判断仅作兼容。
|
||||
"""
|
||||
if annotation is None:
|
||||
return False
|
||||
Markers: tuple[str, ...] = getattr(annotation, 'Markers', None) or ()
|
||||
if 'export' in Markers or 'state' in Markers:
|
||||
if 'export' in Markers or 'extern' in Markers or 'state' in Markers:
|
||||
return True
|
||||
return AnnotationContainsName(annotation, 'CExport') or AnnotationContainsName(annotation, 'State')
|
||||
return (AnnotationContainsName(annotation, 'CExport')
|
||||
or AnnotationContainsName(annotation, 'CExtern')
|
||||
or AnnotationContainsName(annotation, 'State'))
|
||||
|
||||
def _generate_global_decl(self, node: ast.AnnAssign) -> str | None:
|
||||
"""生成全局变量声明"""
|
||||
@@ -258,11 +267,7 @@ class DeclarationGenerator:
|
||||
getattr(node.annotation, 'Markers', None) or ())
|
||||
if 'define' in Markers or AnnotationContainsName(node.annotation, 'CDefine'):
|
||||
return None
|
||||
if 'typedef' in Markers:
|
||||
return None
|
||||
if isinstance(node.annotation, ast.Attribute) and getattr(node.annotation, 'attr', '') == 'CTypedef':
|
||||
return None
|
||||
if isinstance(node.annotation, ast.Name) and node.annotation.id == 'CTypedef':
|
||||
if 'typedef' in Markers or AnnotationContainsName(node.annotation, 'CTypedef'):
|
||||
return None
|
||||
if isinstance(node.annotation, ast.List):
|
||||
for elt in node.annotation.elts:
|
||||
@@ -1145,7 +1150,8 @@ class DeclarationGenerator:
|
||||
Line: int = getattr(annotation, 'lineno', 0) or 0
|
||||
raise TypeError(
|
||||
f"[TPC] 不支持类型联合 'T1 | T2'(第 {Line} 行);"
|
||||
f"类型注解只能是单一类型:指针请写 CPtr[T],修饰符请用关键字")
|
||||
f"类型注解只能是单一类型:指针请写 `T*` 或 `rawptr[T]`,"
|
||||
f"修饰符请用关键字(const/volatile/static/export/extern)")
|
||||
|
||||
non_struct_types: set[str] = {'list', 'dict', 'tuple', 'set', 'array', 'CArray', 'rawarray'}
|
||||
|
||||
@@ -1154,6 +1160,10 @@ class DeclarationGenerator:
|
||||
return 'void'
|
||||
if annotation.id in ('str', 'bytes'):
|
||||
return 'i8*'
|
||||
# 裸指针标记(rawptr = void*,CPtr 亦为裸指针):一律 i8*。
|
||||
# 不得落入下方"未知类型 → %"{sha}.rawptr"*" 的伪结构体分支。
|
||||
if annotation.id in ('rawptr', 'CPtr'):
|
||||
return 'i8*'
|
||||
# 值引用参数标记(CRef = `&T`、CTempRef = `&temp T`,非真实类):按
|
||||
# CPtr 同形的一层指针(i8*)参与联合合并(`T | CRef` → T*)。
|
||||
if annotation.id in ('CRef', 'CTempRef'):
|
||||
@@ -1215,6 +1225,9 @@ class DeclarationGenerator:
|
||||
return f'{sname}*'
|
||||
elif isinstance(annotation, ast.Attribute):
|
||||
attr_name: str = annotation.attr if hasattr(annotation, 'attr') else ''
|
||||
# 裸指针标记(t.rawptr / t.CPtr):i8*,不做结构体回落。
|
||||
if attr_name in ('rawptr', 'CPtr'):
|
||||
return 'i8*'
|
||||
# 值引用参数标记(CRef = `&T`、CTempRef = `&temp T`,非真实类):按 CPtr
|
||||
# 同形的「一层指针 = i8*」参与联合合并(`T | CRef` → T*),保证跨模块
|
||||
# .vpi 声明端类型与定义端 _ctype_to_llvm 一致。
|
||||
|
||||
Reference in new issue
Block a user