refactor(return): 移除 @c.CReturn 多返回值处理器,只保留 -> tuple[ty1, ty2, ...],遇到 CReturn 一律报错
DeclarationGenerator:tuple[...] 分支逐字保留(仅 CReturnTypes 机械改名 MultiRetTypes),装饰器分支改为 SyntaxError,报文含函数名与“请改用 -> tuple[ty1, ty2, ...]”提示 HandlesAssign(a, b = f() 解包路径):删除装饰器收集分支并改为报错;tuple[...] 恢复与逐个解包逻辑逐字不变 HandlesFunctions:新增 _CRETURN_REMOVED_HINT 与 _RejectCReturnDecorator,在函数定义入口(_GetFunctionSignatureLlvm / _EmitFunctionLlvm)fail-fast——即使只有装饰器、没有 -> 注解,也不会让返回类型静默退化成 i32 HandlesImports:外部函数声明处同样报错,并删除下游已失效的 CReturnTypes 参数拼接死代码 HandlesReturn / HandlesFor / BaseTranslator:CurrentCReturnTypes 等机械改名(读写端一致,无行为变化) includes/c.py:保留 CReturn 名字作墓碑(删掉会让 @c.CReturn 变成未知属性、可能被静默忽略),docstring 写明已移除并指路 -> tuple[...];README 措辞同步
This commit is contained in:
1 parent
e5a6616f8b
commit
35de6321f6
9 files changed
+96
-86
No files matched your search
@@ -182,24 +182,28 @@ class DeclarationGenerator:
|
||||
is_export: bool = self._is_export_func(node)
|
||||
if self.module_sha1 and not is_export:
|
||||
func_name = f"{self.module_sha1}.{func_name}"
|
||||
CReturnTypes: list[ast.AST] = []
|
||||
MultiRetTypes: list[ast.AST] = []
|
||||
if node.decorator_list:
|
||||
for decorator in node.decorator_list:
|
||||
# fail-fast:`@c.CReturn` 装饰器已移除,任何地方出现都必须报错,
|
||||
# 不得静默忽略或改写;多返回值请改用 `-> tuple[ty1, ty2, ...]`。
|
||||
if isinstance(decorator, ast.Call) and isinstance(decorator.func, ast.Attribute):
|
||||
if decorator.func.attr == 'CReturn':
|
||||
for arg in decorator.args:
|
||||
CReturnTypes.append(arg)
|
||||
raise SyntaxError(
|
||||
f"函数 '{node.name}':`@c.CReturn` 已移除:请改用返回注解 "
|
||||
f"`-> tuple[ty1, ty2, ...]`(多返回值打包为 struct)"
|
||||
)
|
||||
if node.returns:
|
||||
if isinstance(node.returns, ast.Subscript) and isinstance(node.returns.value, ast.Name) and node.returns.value.id == 'tuple':
|
||||
slice_node: ast.AST = node.returns.slice
|
||||
if isinstance(slice_node, ast.Tuple):
|
||||
for elt in slice_node.elts:
|
||||
CReturnTypes.append(elt)
|
||||
MultiRetTypes.append(elt)
|
||||
else:
|
||||
CReturnTypes.append(slice_node)
|
||||
MultiRetTypes.append(slice_node)
|
||||
ret_type: str
|
||||
if CReturnTypes:
|
||||
elem_types: list[str] = [self._get_type_str(rt, embedded=True) for rt in CReturnTypes]
|
||||
if MultiRetTypes:
|
||||
elem_types: list[str] = [self._get_type_str(rt, embedded=True) for rt in MultiRetTypes]
|
||||
ret_type = '{ ' + ', '.join(elem_types) + ' }'
|
||||
else:
|
||||
ret_type = self._get_type_str(node.returns)
|
||||
|
||||
Reference in new issue
Block a user