refactor(types): 移除 list[T, N] 的定长数组遗留判据,统一走 _list.vp

- 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 形态表述
This commit is contained in:
TermiNexus committed 2026-10-02 22:16:30 +08:00
1 parent 0efdc9e111
commit 9ff301d747
5 files changed
+22 -24

No files matched your search

+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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]])解
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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)
+16 -18
View File
@@ -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*)。