From 9ff301d7471bf7d476904204b842a54d4c66e6d2 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 2 Oct 2026 22:16:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(types):=20=E7=A7=BB=E9=99=A4=20list[T,?= =?UTF-8?q?=20N]=20=E7=9A=84=E5=AE=9A=E9=95=BF=E6=95=B0=E7=BB=84=E9=81=97?= =?UTF-8?q?=E7=95=99=E5=88=A4=E6=8D=AE=EF=BC=8C=E7=BB=9F=E4=B8=80=E8=B5=B0?= =?UTF-8?q?=20=5Flist.vp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SymbolUtils.IsListAnnotation 不再把双参 list[type, count] 判为栈上定长数组: list 是 includes/_list.vp 的堆上 STL 容器泛型类,list[T] / list[T, N] 都走泛型特化 - ParseListAnnotation / ListAnnotationParseResult / ExtractListFromBinOp 相应收窄为 只处理 t.CArray / rawarray / array,并同步文档串 - HandlesAnnAssign / HandlesBase / HandlesFunctions / HandlesImports / SymbolUtils 的 注释改按 CArray 形态表述 --- lib/core/Handles/HandlesAnnAssign.py | 2 +- lib/core/Handles/HandlesBase.py | 2 +- lib/core/Handles/HandlesFunctions.py | 4 ++-- lib/core/Handles/HandlesImports.py | 4 ++-- lib/core/SymbolUtils.py | 34 +++++++++++++--------------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/core/Handles/HandlesAnnAssign.py b/lib/core/Handles/HandlesAnnAssign.py index 8014caf..0adf5b9 100644 --- a/lib/core/Handles/HandlesAnnAssign.py +++ b/lib/core/Handles/HandlesAnnAssign.py @@ -587,7 +587,7 @@ class AnnAssignHandle(BaseHandle): ) if parse_result.is_pointer: # 单参数 list[type] → 指针 - # 但如果有列表字面量初始化值 [a, b, c],则按数组 list[type, N] 处理 + # 但如果有列表字面量初始化值 [a, b, c],则按数组 t.CArray[type, N] 处理 if not (Node.value and isinstance(Node.value, ast.List)): VarType: ir.Type = ir.PointerType(ElemType) var: ir.AllocaInstr = Gen._allocaEntry(VarType, name=VarName) diff --git a/lib/core/Handles/HandlesBase.py b/lib/core/Handles/HandlesBase.py index 327a4df..c177a58 100644 --- a/lib/core/Handles/HandlesBase.py +++ b/lib/core/Handles/HandlesBase.py @@ -1025,7 +1025,7 @@ class BaseHandle: def _ResolveStackArrayElementFallback( self, annotation: ast.expr, Fallback: CTypeInfo | None) -> CTypeInfo | None: - """固定数组注解(`t.CArray[T, N]` / `list[T, N]`)元素类型兜底解析。 + """固定数组注解(`t.CArray[T, N]` / `[N]T`)元素类型兜底解析。 CTypeInfo.FromNode(TypeAnnotationResolver 的 IsListAnnotation 分支)只 会用自身解析元素类型:元素命中泛型模板时(list[X] / list[rawptr[X]])解 diff --git a/lib/core/Handles/HandlesFunctions.py b/lib/core/Handles/HandlesFunctions.py index ceb3873..ae1c400 100644 --- a/lib/core/Handles/HandlesFunctions.py +++ b/lib/core/Handles/HandlesFunctions.py @@ -724,7 +724,7 @@ class FunctionHandle(BaseHandle): ParamTypeInfo.BaseType = t.CChar() ParamTypeInfo.PtrCount = 1 IsPtr = True - # C 语言中数组参数退化为指针:list[type, N] → type* + # C 语言中数组参数退化为指针:t.CArray[type, N] → type* if ParamTypeInfo.ArrayDims: ParamTypeInfo.ArrayDims = [] ParamTypeInfo.PtrCount = max(ParamTypeInfo.PtrCount, 1) @@ -1638,7 +1638,7 @@ class FunctionHandle(BaseHandle): ParamTypeInfo.BaseType = t.CChar() ParamTypeInfo.PtrCount = 1 IsPtr = True - # C 语言中数组参数退化为指针:list[type, N] → type* + # C 语言中数组参数退化为指针:t.CArray[type, N] → type* if ParamTypeInfo.ArrayDims: ParamTypeInfo.ArrayDims = [] ParamTypeInfo.PtrCount = max(ParamTypeInfo.PtrCount, 1) diff --git a/lib/core/Handles/HandlesImports.py b/lib/core/Handles/HandlesImports.py index 179a275..b3668c7 100644 --- a/lib/core/Handles/HandlesImports.py +++ b/lib/core/Handles/HandlesImports.py @@ -1358,7 +1358,7 @@ class ImportHandle(BaseHandle): if Node.annotation: parse_result = ParseListAnnotation(Node.annotation) if parse_result and not parse_result.is_pointer: - # list[type, count] 数组声明 + # t.CArray[type, count] 数组声明 elem_type_node = parse_result.elem_type_node count_node = parse_result.count_node ElemTypeInfo = CTypeInfo.FromNode(elem_type_node, self.Trans.SymbolTable) @@ -1821,7 +1821,7 @@ class ImportHandle(BaseHandle): ParamTypeInfo.BaseType = t.CChar() ParamTypeInfo.PtrCount = 1 ParamIsPtr = True - # C 语言中数组参数退化为指针:list[type, N] → type* + # C 语言中数组参数退化为指针:t.CArray[type, N] → type* if ParamTypeInfo.ArrayDims: ParamTypeInfo.ArrayDims = [] ParamTypeInfo.PtrCount = max(ParamTypeInfo.PtrCount, 1) diff --git a/lib/core/SymbolUtils.py b/lib/core/SymbolUtils.py index 25f937f..c1198e7 100644 --- a/lib/core/SymbolUtils.py +++ b/lib/core/SymbolUtils.py @@ -2063,7 +2063,11 @@ def IsFixedArrayBareName(Name: str) -> bool: def IsListAnnotation(annotation: ast.expr) -> bool: - """检查注解是否为栈上固定数组类型(t.CArray[...] / 内建 array[...] / list[type, count])""" + """检查注解是否为栈上固定数组类型(t.CArray[...] / 内建 array[...]) + + **不含 `list[...]`**:`list` 是 includes/_list.vp 定义的堆上 STL 容器泛型类 + (`list[T]` / `list[T, N]` 都走泛型特化),与定长数组无关。 + """ if not isinstance(annotation, ast.Subscript): return False value = annotation.value @@ -2078,18 +2082,11 @@ def IsListAnnotation(annotation: ast.expr) -> bool: if (isinstance(value, ast.Name) and value.id in _FIXED_ARRAY_BARE_NAMES): return True - # list[type, count] — 仅匹配双参数 Tuple 切片(固定数组), - # 单参数 list[type] 不匹配(走泛型类 list[T] 路径) - if (isinstance(value, ast.Name) - and value.id == 'list' - and isinstance(annotation.slice, ast.Tuple) - and len(annotation.slice.elts) == 2): - return True return False def ExtractListFromBinOp(annotation: ast.expr) -> ast.Subscript | None: - """从 BinOp(BitOr) 注解中提取 list[...] 部分,如 list[i32] | t.CPtr""" + """从 BinOp(BitOr) 注解中提取定长数组部分,如 t.CArray[i32] | t.CPtr""" if isinstance(annotation, ast.BinOp) and isinstance(annotation.op, ast.BitOr): for side in (annotation.left, annotation.right): if IsListAnnotation(side): @@ -2098,10 +2095,10 @@ def ExtractListFromBinOp(annotation: ast.expr) -> ast.Subscript | None: class ListAnnotationParseResult: - """list[...] 注解解析结果。 + """定长数组注解(t.CArray / rawarray / array)解析结果。 - is_pointer=True 表示单参数 list[type] → 指针模式; - is_pointer=False 表示 Tuple(2) 模式 list[type, count]。 + is_pointer=True 表示单参数 CArray[type](等价 CArray[type, None])→ 指针模式; + is_pointer=False 表示 Tuple(2) 模式 CArray[type, count]。 """ __slots__ = ('elem_type_node', 'count_node', 'is_pointer') @@ -2112,10 +2109,11 @@ class ListAnnotationParseResult: def ParseListAnnotation(annotation: ast.expr) -> ListAnnotationParseResult | None: - """解析 list[...] 注解,统一处理 list[type, count] / list[type] / BinOp(BitOr) 形式。 + """解析定长数组注解(t.CArray / rawarray / array),处理 CArray[type, count] / + CArray[type] / BinOp(BitOr) 形式。 - 自动处理 BinOp(BitOr) 包裹(如 list[i32, N] | t.CPtr)。 - 返回 None 表示不是 list 注解。 + 自动处理 BinOp(BitOr) 包裹(如 CArray[i32, N] | t.CPtr)。 + 返回 None 表示不是定长数组注解(`list[...]` 也返回 None —— 它是堆上 STL 容器)。 """ list_node: ast.expr = annotation if isinstance(annotation, ast.BinOp) and isinstance(annotation.op, ast.BitOr): @@ -2767,8 +2765,8 @@ def IsGenericClassSubscriptBase(Node: ast.AST, Symtab: object | None = None, 就是类(实测符号表 `list` 条目 IsStruct=True),故本判据等价覆盖声明端的 `non_struct_types ∪ _generic_class_names`;用户泛型类(Box[T])同样命中。 - 只认 Name 形态:`t.CPtr[...]` / `t.CArray[...]` / 裸 `array[...]` / 定长 - `list[T, N]` / `rawptr[...]` 等由调用方各自分支先行处理,不走这里。 + 只认 Name 形态:`t.CPtr[...]` / `t.CArray[...]` / 裸 `array[...]` / + `rawptr[...]` 等由调用方各自分支先行处理,不走这里。 """ if not isinstance(Node, ast.Name) or not Node.id: return False @@ -2806,7 +2804,7 @@ def ResolveGenericClassSpecName(Node: ast.Subscript, Symtab: object | None = Non 既有分支处理(调用点必须放在那些分支**之后**)。 必要性:单参泛型下标此前在两个注解解析器里都 miss(TypeAnnotationResolver 只认 - 双参定长 list[T, N];LLVMTypeMapper 亦然)⇒ 落到"未知类型"兜底 i32 / 空 CTypeInfo, + 双参定长 t.CArray[T, N];LLVMTypeMapper 亦然)⇒ 落到"未知类型"兜底 i32 / 空 CTypeInfo, 形参与成员类型整体错位(现场:ClassGenGeneric._specialize_generic_class 的 形参 `type_args: list[str]*` 在符号表里成 i32;字段 `TypeGenBase.CTypedefNames: list[str] | t.CPtr` 成 i8*)。