将 EnumName/value/IsSigned 加入 FIELD_ROUTES,消除所有冗余 hasattr/getattr
This commit is contained in:
@@ -435,7 +435,7 @@ class ExprCallHandle(BaseHandle):
|
||||
if first_part in aliases:
|
||||
ModulePath = aliases[first_part] + ModulePath[len(first_part):]
|
||||
is_instance_var = isinstance(Node.func.value, ast.Name) and Node.func.value.id in Gen.var_struct_class and Node.func.value.id not in Gen.ModuleSha1Map
|
||||
is_class_name = isinstance(Node.func.value, ast.Name) and (Node.func.value.id in Gen.structs or (Node.func.value.id in self.Trans.SymbolTable and getattr(self.Trans.SymbolTable[Node.func.value.id], 'IsStruct', False)))
|
||||
is_class_name = isinstance(Node.func.value, ast.Name) and (Node.func.value.id in Gen.structs or (Node.func.value.id in self.Trans.SymbolTable and self.Trans.SymbolTable[Node.func.value.id].IsStruct))
|
||||
if is_instance_var and not is_class_name:
|
||||
return self._HandleMethodCallLlvm(Node)
|
||||
FuncAttr = Node.func.attr
|
||||
@@ -454,11 +454,11 @@ class ExprCallHandle(BaseHandle):
|
||||
result = self._HandleClassNewLlvm(Node, FuncAttr)
|
||||
if result is not None:
|
||||
return result
|
||||
if getattr(SymInfo, 'IsEnumMember', False) and getattr(SymInfo, 'EnumName', None):
|
||||
if SymInfo.IsEnumMember and SymInfo.EnumName:
|
||||
EnumName = SymInfo.EnumName
|
||||
if EnumName in self.Trans.SymbolTable:
|
||||
EnumInfo = self.Trans.SymbolTable[EnumName]
|
||||
if getattr(EnumInfo, 'IsRenum', False):
|
||||
if EnumInfo.IsRenum:
|
||||
result = self._HandleREnumConstructLlvm(Node, EnumName, FuncAttr, SymInfo.value)
|
||||
if result is not None:
|
||||
return result
|
||||
@@ -480,9 +480,9 @@ class ExprCallHandle(BaseHandle):
|
||||
FullAttrKey = f"{ModulePath}.{FuncAttr}"
|
||||
if FullAttrKey in self.Trans.SymbolTable:
|
||||
SymInfo = self.Trans.SymbolTable[FullAttrKey]
|
||||
if getattr(SymInfo, 'IsTypedef', False):
|
||||
if SymInfo.IsTypedef:
|
||||
return ir.Constant(ir.IntType(32), 0)
|
||||
if getattr(SymInfo, 'IsStruct', False):
|
||||
if SymInfo.IsStruct:
|
||||
if FuncAttr not in Gen.structs:
|
||||
self._ensure_struct_declared(FuncAttr)
|
||||
if FuncAttr in Gen.structs:
|
||||
@@ -533,7 +533,7 @@ class ExprCallHandle(BaseHandle):
|
||||
raise Exception(f"Undefined method: 't.{FuncAttr}'")
|
||||
if isinstance(Node.func.value, ast.Attribute):
|
||||
inner_attr = Node.func.value.attr
|
||||
if inner_attr in Gen.structs or (inner_attr in self.Trans.SymbolTable and getattr(self.Trans.SymbolTable[inner_attr], 'IsStruct', False)):
|
||||
if inner_attr in Gen.structs or (inner_attr in self.Trans.SymbolTable and self.Trans.SymbolTable[inner_attr].IsStruct):
|
||||
static_result = self._HandleStaticMethodCallLlvm(Node, inner_attr, FuncAttr)
|
||||
if static_result is not None:
|
||||
return static_result
|
||||
@@ -569,11 +569,11 @@ class ExprCallHandle(BaseHandle):
|
||||
if FuncAttr and ModulePath and ModulePath not in {'t', 'c'}:
|
||||
# 先检查 FuncAttr 是否是函数/变量,如果是则跳过 struct 声明
|
||||
_attr_sym = self.Trans.SymbolTable.get(FuncAttr)
|
||||
_is_func_or_var = _attr_sym and (getattr(_attr_sym, 'IsFunction', False) or getattr(_attr_sym, 'IsVariable', False))
|
||||
_is_func_or_var = _attr_sym and (_attr_sym.IsFunction or _attr_sym.IsVariable)
|
||||
if not _is_func_or_var and ModulePath:
|
||||
_full_key = f"{ModulePath}.{FuncAttr}"
|
||||
_full_sym = self.Trans.SymbolTable.get(_full_key)
|
||||
if _full_sym and (getattr(_full_sym, 'IsFunction', False) or getattr(_full_sym, 'IsVariable', False)):
|
||||
if _full_sym and (_full_sym.IsFunction or _full_sym.IsVariable):
|
||||
_is_func_or_var = True
|
||||
# 优先检查 FuncAttr 是否为 struct/类构造器
|
||||
if not _is_func_or_var and FuncAttr not in Gen.structs:
|
||||
@@ -587,7 +587,7 @@ class ExprCallHandle(BaseHandle):
|
||||
FullAttrKey = f"{ModulePath}.{FuncAttr}"
|
||||
if FullAttrKey in self.Trans.SymbolTable:
|
||||
FullSymInfo = self.Trans.SymbolTable[FullAttrKey]
|
||||
if getattr(FullSymInfo, 'IsStruct', False) or getattr(FullSymInfo, 'IsCpythonObject', False):
|
||||
if FullSymInfo.IsStruct or FullSymInfo.IsCpythonObject:
|
||||
if FuncAttr not in Gen.structs:
|
||||
self._ensure_struct_declared(FuncAttr)
|
||||
if FuncAttr in Gen.structs:
|
||||
@@ -649,7 +649,7 @@ class ExprCallHandle(BaseHandle):
|
||||
Gen = self.Trans.LlvmGen
|
||||
# 如果名称是函数或变量,不应创建 struct
|
||||
SymInfo = self.Trans.SymbolTable.get(class_name)
|
||||
if SymInfo and (getattr(SymInfo, 'IsFunction', False) or getattr(SymInfo, 'IsVariable', False)):
|
||||
if SymInfo and (SymInfo.IsFunction or SymInfo.IsVariable):
|
||||
return
|
||||
if class_name in Gen.structs:
|
||||
existing = Gen.structs[class_name]
|
||||
@@ -721,16 +721,16 @@ class ExprCallHandle(BaseHandle):
|
||||
SymKey = f'{ClassName}.{MethodName}'
|
||||
SymInfo = self.Trans.SymbolTable.get(SymKey) or self.Trans.SymbolTable.get(MethodName)
|
||||
if SymInfo and SymInfo.IsFunction:
|
||||
ret_type_info = getattr(SymInfo, 'FuncPtrReturn', None)
|
||||
ret_type_info = SymInfo.FuncPtrReturn
|
||||
if isinstance(ret_type_info, CTypeInfo) and ret_type_info.BaseType:
|
||||
ret_type = ret_type_info.ToLLVM(Gen)
|
||||
else:
|
||||
ret_type = Gen._CType2LLVM('i32', False)
|
||||
param_type_infos = [pt for _, pt in (getattr(SymInfo, 'FuncPtrParams', None) or [])]
|
||||
param_type_infos = [pt for _, pt in (SymInfo.FuncPtrParams or [])]
|
||||
if isinstance(ret_type, ir.VoidType):
|
||||
ret_type = ir.IntType(32)
|
||||
llvm_param_types = []
|
||||
is_static = hasattr(SymInfo, 'MetaList') and FuncMeta.STATIC_METHOD in SymInfo.MetaList
|
||||
is_static = FuncMeta.STATIC_METHOD in SymInfo.MetaList
|
||||
if ClassName in Gen.structs and not is_static:
|
||||
llvm_param_types.append(ir.PointerType(Gen.structs[ClassName]))
|
||||
for pt in param_type_infos:
|
||||
@@ -762,8 +762,8 @@ class ExprCallHandle(BaseHandle):
|
||||
CallArgs = []
|
||||
SymKey = f'{ClassName}.{MethodName}'
|
||||
SymInfo = self.Trans.SymbolTable.get(SymKey) or self.Trans.SymbolTable.get(MethodName)
|
||||
is_static_call = SymInfo is not None and hasattr(SymInfo, 'MetaList') and FuncMeta.STATIC_METHOD in SymInfo.MetaList
|
||||
is_classmethod_call = SymInfo is not None and hasattr(SymInfo, 'MetaList') and FuncMeta.CLASS_METHOD in SymInfo.MetaList
|
||||
is_static_call = SymInfo is not None and FuncMeta.STATIC_METHOD in SymInfo.MetaList
|
||||
is_classmethod_call = SymInfo is not None and FuncMeta.CLASS_METHOD in SymInfo.MetaList
|
||||
# @classmethod: 在参数列表前插入 cls(栈上分配的类实例指针)
|
||||
if is_classmethod_call and ClassName in Gen.structs:
|
||||
ClsPtr = Gen._alloca(Gen.structs[ClassName], name=f"cls_{ClassName}")
|
||||
@@ -1230,13 +1230,13 @@ class ExprCallHandle(BaseHandle):
|
||||
mangled_name = Gen._mangle_func_name(func_name, ModulePath)
|
||||
sym_key = f'{ModulePath}.{func_name}'
|
||||
sym_info = self.Trans.SymbolTable.get(sym_key) or self.Trans.SymbolTable.get(func_name)
|
||||
is_inline = sym_info and (getattr(sym_info, 'IsInline', False) or isinstance(getattr(sym_info, 'Storage', None), t.CInline))
|
||||
if is_inline and getattr(sym_info, 'InlineBody', None):
|
||||
is_inline = sym_info and (sym_info.IsInline or isinstance(sym_info.Storage, t.CInline))
|
||||
if is_inline and sym_info.InlineBody:
|
||||
self._HandleInlineExpandLlvm(Node, sym_info)
|
||||
return ir.Constant(ir.IntType(32), 1)
|
||||
sym_info_exact = self.Trans.SymbolTable.get(sym_key)
|
||||
if sym_info_exact:
|
||||
exact_params = getattr(sym_info_exact, 'FuncPtrParams', [])
|
||||
exact_params = sym_info_exact.FuncPtrParams
|
||||
exact_param_names = [pn for pn, _ in exact_params]
|
||||
if exact_param_names:
|
||||
provided = len(Node.args)
|
||||
@@ -1280,15 +1280,15 @@ class ExprCallHandle(BaseHandle):
|
||||
if not sym_info:
|
||||
sym_info = self.Trans.SymbolTable.get(func_name)
|
||||
if sym_info:
|
||||
is_func = getattr(sym_info, 'IsFunction', False)
|
||||
sym_is_variadic = getattr(sym_info, 'IsVariadic', False)
|
||||
is_func = sym_info.IsFunction
|
||||
sym_is_variadic = sym_info.IsVariadic
|
||||
if is_func:
|
||||
ret_type_info = getattr(sym_info, 'FuncPtrReturn', None)
|
||||
ret_type_info = sym_info.FuncPtrReturn
|
||||
if isinstance(ret_type_info, CTypeInfo) and ret_type_info.BaseType:
|
||||
ret_type = ret_type_info.ToLLVM(Gen)
|
||||
else:
|
||||
ret_type = Gen._CType2LLVM('i32', False)
|
||||
sym_params = getattr(sym_info, 'FuncPtrParams', [])
|
||||
sym_params = sym_info.FuncPtrParams
|
||||
param_type_infos = [pt for _, pt in sym_params]
|
||||
if isinstance(ret_type, ir.VoidType):
|
||||
ret_type = ir.IntType(32)
|
||||
@@ -1411,15 +1411,15 @@ class ExprCallHandle(BaseHandle):
|
||||
if not sym_info:
|
||||
sym_info = self.Trans.SymbolTable.get(func_name)
|
||||
if sym_info:
|
||||
is_func = getattr(sym_info, 'IsFunction', False)
|
||||
sym_is_variadic = getattr(sym_info, 'IsVariadic', False)
|
||||
is_func = sym_info.IsFunction
|
||||
sym_is_variadic = sym_info.IsVariadic
|
||||
if is_func:
|
||||
ret_type_info = getattr(sym_info, 'FuncPtrReturn', None)
|
||||
ret_type_info = sym_info.FuncPtrReturn
|
||||
if isinstance(ret_type_info, CTypeInfo) and ret_type_info.BaseType:
|
||||
ret_type = ret_type_info.ToLLVM(Gen)
|
||||
else:
|
||||
ret_type = Gen._CType2LLVM('i32', False)
|
||||
param_type_infos = [pt for _, pt in getattr(sym_info, 'FuncPtrParams', [])]
|
||||
param_type_infos = [pt for _, pt in sym_info.FuncPtrParams]
|
||||
if isinstance(ret_type, ir.VoidType):
|
||||
ret_type = ir.IntType(32)
|
||||
llvm_param_types = []
|
||||
@@ -1830,7 +1830,7 @@ class ExprCallHandle(BaseHandle):
|
||||
if ClassName:
|
||||
SymKey = f'{ClassName}.{MethodName}'
|
||||
SymInfo = self.Trans.SymbolTable.get(SymKey)
|
||||
if SymInfo and hasattr(SymInfo, 'MetaList'):
|
||||
if SymInfo and SymInfo.MetaList:
|
||||
if FuncMeta.STATIC_METHOD in SymInfo.MetaList:
|
||||
return self._HandleStaticMethodCallLlvm(Node, ClassName, MethodName)
|
||||
if FuncMeta.CLASS_METHOD in SymInfo.MetaList:
|
||||
|
||||
Reference in New Issue
Block a user