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
@@ -640,7 +640,12 @@ def _ExtractCDefineConstantsFromPyi(pyi_path: str, all_dc: dict[str, object],
|
||||
|
||||
for node in ast.iter_child_nodes(tree):
|
||||
if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
|
||||
if AnnotationContainsName(node.annotation, 'CDefine') and node.value:
|
||||
Markers: tuple[str, ...] = (tuple(getattr(node, 'Markers', None) or ())
|
||||
+ tuple(getattr(node.annotation, 'Markers', None) or ()))
|
||||
# 新语法 `define NAME = v` 由解析器写入 Markers;旧
|
||||
# `NAME: CDefine = v` 仅作兼容回落。
|
||||
if ('define' in Markers
|
||||
or AnnotationContainsName(node.annotation, 'CDefine')) and node.value:
|
||||
val: object = ConstEvaluator.eval_full(node.value, ctx)
|
||||
if val is None and isinstance(node.value, ast.Call) and len(node.value.args) == 1:
|
||||
# 类型构造的**裸别名**形态(如 `DWORD(-11)`,非 `CUnsignedLong(-11)`):
|
||||
@@ -664,6 +669,17 @@ def _ExtractCDefineConstantsFromPyi(pyi_path: str, all_dc: dict[str, object],
|
||||
_vlog().warning(f"Phase2Translator: 忽略异常 {e}", exc_info=e)
|
||||
|
||||
|
||||
def _IsExportExternFuncNode(node: ast.FunctionDef) -> bool:
|
||||
"""函数是否导出/外部可见:优先读 `export def` / `extern def` 的 AST 标记,
|
||||
再回落到 Markers,最后兼容旧的 `-> CExport/CExtern/State` 注解写法。"""
|
||||
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 'extern' in Markers or 'state' in Markers:
|
||||
return True
|
||||
return _check_annotation_for_export(node.returns)
|
||||
|
||||
|
||||
def _CountUnresolvedTypedefs(module_symbols: object) -> int:
|
||||
"""该模块"有值注解却解析不出类型"的 typedef 个数(供声明阶段判断是否要下一轮)。
|
||||
|
||||
@@ -2388,7 +2404,7 @@ class Phase2Translator:
|
||||
sig_tree = ast.parse(sig_content, sig_path)
|
||||
for node in ast.iter_child_nodes(sig_tree):
|
||||
if isinstance(node, ast.FunctionDef):
|
||||
if _check_annotation_for_export(node.returns):
|
||||
if _IsExportExternFuncNode(node):
|
||||
export_extern_funcs[node.name] = sha1_key
|
||||
except Exception as _e:
|
||||
if _config_mode == "strict":
|
||||
@@ -4345,19 +4361,19 @@ class Phase2Translator:
|
||||
module_name = os.path.splitext(os.path.basename(src_path))[0]
|
||||
for node in ast.iter_child_nodes(tree):
|
||||
if isinstance(node, ast.FunctionDef) and node.returns:
|
||||
is_inline = False
|
||||
if isinstance(node.returns, ast.BinOp) and isinstance(node.returns.op, ast.BitOr):
|
||||
for part in [node.returns.left, node.returns.right]:
|
||||
if isinstance(part, ast.Attribute) and part.attr == 'CInline':
|
||||
is_inline = True
|
||||
break
|
||||
if isinstance(part, ast.Name) and part.id == 'CInline':
|
||||
is_inline = True
|
||||
break
|
||||
elif isinstance(node.returns, ast.Attribute) and node.returns.attr == 'CInline':
|
||||
# 新语法 `inline` 由解析器写入返回注解的 Markers;
|
||||
# 旧 `-> CInline | T` 的联合形态已废止(下方 fail-fast)。
|
||||
ReturnMarkers: tuple[str, ...] = getattr(node.returns, 'Markers', None) or ()
|
||||
is_inline: bool = 'inline' in ReturnMarkers
|
||||
if isinstance(node.returns, ast.Attribute) and node.returns.attr == 'CInline':
|
||||
is_inline = True
|
||||
elif isinstance(node.returns, ast.Name) and node.returns.id == 'CInline':
|
||||
is_inline = True
|
||||
elif isinstance(node.returns, ast.BinOp) and isinstance(node.returns.op, ast.BitOr):
|
||||
_Line: int = getattr(node.returns, 'lineno', 0) or 0
|
||||
raise TypeError(
|
||||
f"[TPC] 不支持类型联合 'T1 | T2'(第 {_Line} 行);"
|
||||
f"内联标记请写 `-> inline T`,类型注解只能是单一类型")
|
||||
if is_inline:
|
||||
func_name = node.name
|
||||
sym_key = f"{module_name}.{func_name}"
|
||||
@@ -4370,6 +4386,9 @@ class Phase2Translator:
|
||||
info.Storage = TypeSystem.CInline()
|
||||
self.inline_func_symbols[func_name] = info
|
||||
self.inline_func_symbols[sym_key] = info
|
||||
except TypeError:
|
||||
# 类型注解 fail-fast(BinOp(BitOr) 联合)不得被 strict 开关吞掉
|
||||
raise
|
||||
except Exception as _e:
|
||||
if _config_mode == "strict":
|
||||
raise
|
||||
@@ -4499,7 +4518,7 @@ class Phase2Translator:
|
||||
continue
|
||||
for node in ast.iter_child_nodes(sig_tree):
|
||||
if isinstance(node, ast.FunctionDef):
|
||||
if _check_annotation_for_export(node.returns):
|
||||
if _IsExportExternFuncNode(node):
|
||||
export_extern_funcs[node.name] = sha1_key
|
||||
except Exception as _e:
|
||||
if _config_mode == "strict":
|
||||
|
||||
Reference in new issue
Block a user