fix(imports): typedef/define 声明跳过全局变量类型解析
typedef/define 在类型解析前先按 Markers 分流,避免 void/宏类型触发全局变量 void 断言。
This commit is contained in:
1 parent
77a97303b7
commit
da13e7b405
1 file changed
+18
-10
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user