From da13e7b40525105ca877d8c1ba894a7ec4ec9e67 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 18 Oct 2026 16:41:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(imports):=20typedef/define=20=E5=A3=B0?= =?UTF-8?q?=E6=98=8E=E8=B7=B3=E8=BF=87=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typedef/define 在类型解析前先按 Markers 分流,避免 void/宏类型触发全局变量 void 断言。 --- lib/core/Handles/HandlesImports.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/core/Handles/HandlesImports.py b/lib/core/Handles/HandlesImports.py index 74fd7bb..64678b4 100644 --- a/lib/core/Handles/HandlesImports.py +++ b/lib/core/Handles/HandlesImports.py @@ -1365,16 +1365,24 @@ class ImportHandle(BaseHandle): var_type = Gen._ctype_to_llvm(ElemTypeInfo) IsPtr = False else: - TypeInfo = CTypeInfo.FromNode(Node.annotation, self.Trans.SymbolTable) - if TypeInfo: - var_type = Gen._ctype_to_llvm(TypeInfo) - if isinstance(var_type, ir.VoidType): - raise TypeError( - f"[TPC] 全局变量 '{VarName}' 的类型注解 " - f"'{getattr(Node.annotation, 'id', type(Node.annotation).__name__)}' " - f"解析为 void(第 {getattr(Node, 'lineno', 0)} 行);" - f"请检查该类型是否已定义或声明顺序是否正确。") - IsPtr = TypeInfo.IsPtr + _AnnMarkers: tuple[str, ...] = (tuple(getattr(Node, 'Markers', None) or ()) + + tuple(getattr(Node.annotation, 'Markers', None) or ())) + if 'typedef' in _AnnMarkers or 'define' in _AnnMarkers: + # typedef / define 走下面的注册分支,不发射全局变量 + TypeInfo = None + var_type = ir.IntType(32) + IsPtr = False + else: + TypeInfo = CTypeInfo.FromNode(Node.annotation, self.Trans.SymbolTable) + if TypeInfo: + var_type = Gen._ctype_to_llvm(TypeInfo) + if isinstance(var_type, ir.VoidType): + raise TypeError( + f"[TPC] 全局变量 '{VarName}' 的类型注解 " + f"'{getattr(Node.annotation, 'id', type(Node.annotation).__name__)}' " + f"解析为 void(第 {getattr(Node, 'lineno', 0)} 行);" + f"请检查该类型是否已定义或声明顺序是否正确。") + IsPtr = TypeInfo.IsPtr if isinstance(var_type, ir.IdentifiedStructType) and (var_type.elements is None or len(var_type.elements) == 0): var_type = ir.IntType(32) if Node.value and ModulePath and IsPtr and isinstance(Node.value, ast.Constant) and isinstance(Node.value.value, str):