refactor: 删除保留性兼容兜底,仅保留新语法主路径

移除 len(list) 直读 __count__、list 方法裸模板回退、旧 C* 文本标记兼容、旧联合兼容;vast 扩展过期直接报错,已重建 _parser.pyd。
This commit is contained in:
TermiNexus committed 2026-10-18 19:22:32 +08:00
1 parent f8d84a1941
commit 109e71a477
13 files changed
+72 -232

No files matched your search

-6
View File
@@ -2407,12 +2407,6 @@ class AssignHandle(BaseHandle):
raise TypeError(
f"[TPC] 不支持类型联合 'T1 | T2'(第 {Line} 行);"
f"类型注解只能是单一类型:指针请写 CPtr[T],修饰符请用关键字")
if isinstance(annotation, ast.Attribute):
if annotation.attr in ('CTypedef', 'CDefine'):
return True
# 兼容旧 AST:裸标记名(新表示为 Markers,已在函数入口命中)。
if isinstance(annotation, ast.Name):
return annotation.id in ('CTypedef', 'CDefine')
if isinstance(annotation, ast.Subscript):
return self._IsTypedefAnnotation(annotation.value)
return False
-14
View File
@@ -737,20 +737,6 @@ class ExprBuiltinHandle(BaseHandle):
result = self.Trans.ExprUtils._try_operator_overLoad(_matched_cn, '__len__', val, Gen)
if result is not None:
return result
if _matched_cn.startswith('list['):
try:
_count_off: int | None = Gen._get_member_offset('__count__', _matched_cn, pointee)
except Exception:
# 特化结构体尚未展开布局(opaque):_list.vp 的 list 依次为
# __data__(ptr) / __count__ / __capacity__ / __pool__ / ...
_count_off = 8
if _count_off is not None:
_i8p = Gen.builder.bitcast(val, ir.PointerType(ir.IntType(8)), name='list_i8p')
_count_addr = Gen.builder.gep(
_i8p, [ir.Constant(ir.IntType(64), _count_off)], name='list_count_addr')
_count_ptr = Gen.builder.bitcast(
_count_addr, ir.PointerType(ir.IntType(64)), name='list_count_ptr')
return Gen._load(_count_ptr, name='list_count')
self._FailFastLenUnsupported(arg_node, Gen, val, f"len() 参数类型 '{_matched_cn}' 没有 __len__ 方法")
else:
# 未在 Gen.structs 登记的 struct(opaque/匿名)→ fail-fast。
-41
View File
@@ -3031,17 +3031,6 @@ class ExprCallHandle(BaseHandle):
Gen._generic_spec_error = f'{type(_spec_err).__name__}: {_spec_err}'
return
cached_name: str = cached[0]
was_declare_only: bool = cached[1]
if not was_declare_only:
# 缓存声称已完整,但方法确实不存在:强制补发一次
spec_cache[spec_key] = (cached_name, True)
try:
self.Trans.ClassHandler._specialize_generic_class(
base_name, _TypeArgs, Gen,
type_names=_TypeNames, declare_only=False)
except Exception as _spec_err:
Gen._generic_spec_error = f'强制补发失败: {type(_spec_err).__name__}: {_spec_err}'
return
# 检查方法是否已有 define(blocks 非空表示已编译方法体)
struct_sha1_map = getattr(Gen, '_struct_sha1_map', {})
class_sha1 = struct_sha1_map.get(cached_name)
@@ -3942,22 +3931,6 @@ class ExprCallHandle(BaseHandle):
adjusted = Gen._adjust_args(call_args_fb, func)
result = Gen.builder.call(func, adjusted, name=f"call_{FullMethodName}")
return result
if ClassName.startswith('list[') and ClassName in Gen.structs:
# 泛型特化方法未生成时,回退到裸模板 list 的同名方法
struct_type = Gen.structs[ClassName]
self_ptr_type = ir.PointerType(struct_type)
BareFuncName: str = f'list.{MethodName}'
func = Gen._find_function(BareFuncName)
if func is not None:
if isinstance(ObjVal.type, ir.PointerType) and ObjVal.type.pointee == struct_type:
call_self = ObjVal
else:
call_self = Gen.builder.bitcast(ObjVal, self_ptr_type, name=f"method_self_{ClassName}")
call_args_fb = [call_self] + CallArgs
self._fill_default_args(call_args_fb, func, BareFuncName)
self._append_eh_msg_out_arg(call_args_fb, func, Gen)
adjusted = Gen._adjust_args(call_args_fb, func)
return Gen.builder.call(func, adjusted, name=f"call_{BareFuncName}")
_spec_diag: str = getattr(Gen, '_generic_spec_error', '')
raise Exception(
f"Undefined method '{MethodName}' in class '{ClassName}' "
@@ -4161,20 +4134,6 @@ class ExprCallHandle(BaseHandle):
result = self.Trans.ExprUtils._try_operator_overLoad(_matched_cn, '__len__', val, Gen)
if result is not None:
return result
if _matched_cn.startswith('list['):
try:
_count_off: int | None = Gen._get_member_offset('__count__', _matched_cn, pointee)
except Exception:
# 特化结构体尚未展开布局(opaque):_list.vp 的 list 依次为
# __data__(ptr) / __count__ / __capacity__ / __pool__ / ...
_count_off = 8
if _count_off is not None:
_i8p = Gen.builder.bitcast(val, ir.PointerType(ir.IntType(8)), name='list_i8p')
_count_addr = Gen.builder.gep(
_i8p, [ir.Constant(ir.IntType(64), _count_off)], name='list_count_addr')
_count_ptr = Gen.builder.bitcast(
_count_addr, ir.PointerType(ir.IntType(64)), name='list_count_ptr')
return Gen._load(_count_ptr, name='list_count')
self._FailFastLenUnsupported(arg_node, Gen, val, f"len() 参数类型 '{_matched_cn}' 没有 __len__ 方法")
else:
# 未在 Gen.structs 登记的 struct(opaque/匿名)→ fail-fast。
+1 -13
View File
@@ -178,7 +178,7 @@ class ExprUtils:
if not Gen._has_function(FullMethodName):
FullMethodName = f'{ClassName}.{op_name}__'
if not Gen._has_function(FullMethodName) and '[' in ClassName:
# 泛型特化方法尚未生成时,先补发特化,再回退裸模板类(如 list.__len__)
# 泛型特化方法尚未生成时,先补发特化
try:
self.Trans.ExprCallHandle._try_complete_generic_specialization(ClassName, op_name, Gen)
except Exception:
@@ -186,18 +186,6 @@ class ExprUtils:
FullMethodName = f'{ClassName}.{op_name}'
if not Gen._has_function(FullMethodName):
FullMethodName = f'{ClassName}.{op_name}__'
if not Gen._has_function(FullMethodName):
_SpecTy: Any = Gen.structs.get(ClassName)
_SpecName: str = getattr(_SpecTy, 'name', '') if _SpecTy is not None else ''
if _SpecName:
FullMethodName = f'{_SpecName}.{op_name}'
if not Gen._has_function(FullMethodName):
FullMethodName = f'{_SpecName}.{op_name}__'
if not Gen._has_function(FullMethodName):
_BareClass: str = ClassName.split('[', 1)[0]
FullMethodName = f'{_BareClass}.{op_name}'
if not Gen._has_function(FullMethodName):
FullMethodName = f'{_BareClass}.{op_name}__'
if not Gen._has_function(FullMethodName):
# 多继承:沿 MRO 祖先链查找 dunder 方法(先声明父类优先,与 VPC 一致)
for anc in Gen._get_mro_chain(ClassName)[1:]:
-6
View File
@@ -68,12 +68,6 @@ def _IsVoidOrDeclAnnotation(Annot: ast.AST) -> bool:
return Annot.value is None
if isinstance(Annot, ast.Subscript):
return _IsVoidOrDeclAnnotation(Annot.value)
if isinstance(Annot, ast.Attribute):
# 兼容旧 AST:t.CVoid / t.State / t.CExport / t.CExtern
return getattr(Annot, 'attr', '') in ('CVoid', 'State', 'CExport', 'CExtern')
if isinstance(Annot, ast.Name):
# 兼容旧 AST:裸标记名
return Annot.id in ('CVoid', 'State', 'CExport', 'CExtern')
return False
+1 -25
View File
@@ -1270,11 +1270,6 @@ class ImportHandle(BaseHandle):
"""
if annotation is None:
return None
if isinstance(annotation, ast.BinOp) and isinstance(annotation.op, ast.BitOr):
Line: int = getattr(annotation, 'lineno', 0) or 0
raise TypeError(
f"[TPC] 不支持类型联合 'T1 | T2'(第 {Line} 行);"
f"类型注解只能是单一类型:指针请写 CPtr[T],修饰符请用关键字")
if isinstance(annotation, ast.Subscript):
_base: ast.AST = annotation.value
_bn: str = ''
@@ -1418,11 +1413,6 @@ class ImportHandle(BaseHandle):
IsCDefine = True
elif 'typedef' in _GlobalMarkers:
IsCTypedef = True
elif isinstance(Node.annotation, ast.Name):
if Node.annotation.id == 'CDefine':
IsCDefine = True
elif Node.annotation.id == 'CTypedef':
IsCTypedef = True
if IsCTypedef:
# 检查符号是否已存在且已正确解析(OriginalType 已设置),避免覆盖
@@ -1627,11 +1617,6 @@ class ImportHandle(BaseHandle):
llvm_str = CTypeRegistry.NameToLLVM(value_node.id)
if llvm_str:
return llvm_str
elif isinstance(value_node, ast.BinOp) and isinstance(value_node.op, ast.BitOr):
Line: int = getattr(value_node, 'lineno', 0) or 0
raise TypeError(
f"[TPC] 不支持类型联合 'T1 | T2'(第 {Line} 行);"
f"类型注解只能是单一类型:指针请写 CPtr[T],修饰符请用关键字")
elif isinstance(value_node, ast.Subscript):
base_node: ast.AST = value_node.value
base_name: str | None = (
@@ -1667,16 +1652,7 @@ class ImportHandle(BaseHandle):
def _check_annotation_for_state(self, annotation: ast.AST) -> bool:
Markers: tuple[str, ...] = tuple(getattr(annotation, 'Markers', None) or ())
if 'export' in Markers or 'extern' in Markers or 'state' in Markers:
return True
# 兼容旧 AST:修饰词曾是联合注解的一翼 / 裸标记名。
if isinstance(annotation, ast.Subscript):
return (AnnotationContainsName(annotation.value, 'CExport') or
AnnotationContainsName(annotation.value, 'CExtern') or
AnnotationContainsName(annotation.value, 'State'))
return (AnnotationContainsName(annotation, 'CExport') or
AnnotationContainsName(annotation, 'CExtern') or
AnnotationContainsName(annotation, 'State'))
return 'export' in Markers or 'extern' in Markers or 'state' in Markers
def _check_decorators_for_state(self, Node: ast.FunctionDef) -> bool:
"""检查装饰器列表中是否包含 @CExport / @CExtern / @State(CExport 既能作返回值注解也能作装饰器)"""