将 EnumName/value/IsSigned 加入 FIELD_ROUTES,消除所有冗余 hasattr/getattr
This commit is contained in:
@@ -73,7 +73,7 @@ class IfHandle(BaseHandle):
|
||||
return True
|
||||
if name in self.Trans.SymbolTable:
|
||||
info = self.Trans.SymbolTable[name]
|
||||
if getattr(info, 'IsDefine', False):
|
||||
if info.IsDefine:
|
||||
return True
|
||||
platform_macros = self._get_platform_macros()
|
||||
return name in platform_macros
|
||||
@@ -86,8 +86,8 @@ class IfHandle(BaseHandle):
|
||||
return val
|
||||
if name in self.Trans.SymbolTable:
|
||||
info = self.Trans.SymbolTable[name]
|
||||
if getattr(info, 'IsDefine', False):
|
||||
val = getattr(info, 'DefineValue', 0)
|
||||
if info.IsDefine:
|
||||
val = info.DefineValue
|
||||
if isinstance(val, (int, float)):
|
||||
return val
|
||||
platform_macros = self._get_platform_macros()
|
||||
@@ -129,118 +129,21 @@ class IfHandle(BaseHandle):
|
||||
return macros
|
||||
|
||||
def _eval_const_expr(self, node):
|
||||
if isinstance(node, ast.Constant):
|
||||
val = node.value
|
||||
if isinstance(val, bool):
|
||||
return 1 if val else 0
|
||||
if isinstance(val, int):
|
||||
return val
|
||||
if isinstance(val, float):
|
||||
return 1 if val != 0.0 else 0
|
||||
return 0
|
||||
if isinstance(node, ast.Name):
|
||||
name = node.id
|
||||
val = self._get_macro_value(name)
|
||||
if val is not None:
|
||||
return val
|
||||
return None
|
||||
if isinstance(node, ast.Attribute):
|
||||
full_key = self._get_attr_full_name(node)
|
||||
if full_key:
|
||||
val = self._get_macro_value(full_key)
|
||||
if val is not None:
|
||||
return val
|
||||
short_name = full_key.split('.')[-1] if '.' in full_key else full_key
|
||||
val = self._get_macro_value(short_name)
|
||||
if val is not None:
|
||||
return val
|
||||
return None
|
||||
if isinstance(node, ast.UnaryOp):
|
||||
operand = self._eval_const_expr(node.operand)
|
||||
if operand is None:
|
||||
return None
|
||||
if isinstance(node.op, ast.Not):
|
||||
return 1 if not operand else 0
|
||||
if isinstance(node.op, ast.USub):
|
||||
return -operand
|
||||
if isinstance(node.op, ast.Invert):
|
||||
return ~operand
|
||||
return None
|
||||
if isinstance(node, ast.BinOp):
|
||||
left = self._eval_const_expr(node.left)
|
||||
right = self._eval_const_expr(node.right)
|
||||
if left is None or right is None:
|
||||
return None
|
||||
try:
|
||||
if isinstance(node.op, ast.Add):
|
||||
return left + right
|
||||
elif isinstance(node.op, ast.Sub):
|
||||
return left - right
|
||||
elif isinstance(node.op, ast.Mult):
|
||||
return left * right
|
||||
elif isinstance(node.op, ast.FloorDiv):
|
||||
return left // right if right != 0 else 0
|
||||
elif isinstance(node.op, ast.Mod):
|
||||
return left % right if right != 0 else 0
|
||||
elif isinstance(node.op, ast.LShift):
|
||||
return left << right
|
||||
elif isinstance(node.op, ast.RShift):
|
||||
return left >> right
|
||||
elif isinstance(node.op, ast.BitOr):
|
||||
return left | right
|
||||
elif isinstance(node.op, ast.BitAnd):
|
||||
return left & right
|
||||
elif isinstance(node.op, ast.BitXor):
|
||||
return left ^ right
|
||||
except Exception:
|
||||
return None
|
||||
return None
|
||||
if isinstance(node, ast.BoolOp):
|
||||
if isinstance(node.op, ast.And):
|
||||
for val in node.values:
|
||||
result = self._eval_const_expr(val)
|
||||
if result is None:
|
||||
return None
|
||||
if not result:
|
||||
return 0
|
||||
return 1
|
||||
elif isinstance(node.op, ast.Or):
|
||||
for val in node.values:
|
||||
result = self._eval_const_expr(val)
|
||||
if result is None:
|
||||
return None
|
||||
if result:
|
||||
return 1
|
||||
return 0
|
||||
if isinstance(node, ast.Compare):
|
||||
left = self._eval_const_expr(node.left)
|
||||
if left is None:
|
||||
return None
|
||||
for op, comparator in zip(node.ops, node.comparators):
|
||||
right = self._eval_const_expr(comparator)
|
||||
if right is None:
|
||||
return None
|
||||
if isinstance(op, ast.Eq):
|
||||
result = left == right
|
||||
elif isinstance(op, ast.NotEq):
|
||||
result = left != right
|
||||
elif isinstance(op, ast.Lt):
|
||||
result = left < right
|
||||
elif isinstance(op, ast.LtE):
|
||||
result = left <= right
|
||||
elif isinstance(op, ast.Gt):
|
||||
result = left > right
|
||||
elif isinstance(op, ast.GtE):
|
||||
result = left >= right
|
||||
else:
|
||||
return None
|
||||
if not result:
|
||||
return 0
|
||||
return 1
|
||||
if isinstance(node, ast.Call):
|
||||
if self._is_cif_call(node):
|
||||
from lib.core.ConstEvaluator import ConstEvaluator, EvalContext
|
||||
Gen = self.Trans.LlvmGen
|
||||
ctx = EvalContext(Gen=Gen, symtab=self.Trans.SymbolTable)
|
||||
result = ConstEvaluator.eval_full(node, ctx)
|
||||
if result is None:
|
||||
# 保留 CIf 嵌套调用处理(HandlesIf 特有逻辑)
|
||||
if isinstance(node, ast.Call) and self._is_cif_call(node):
|
||||
return self._evaluate_cif_condition(node)
|
||||
return None
|
||||
return None
|
||||
# HandlesIf 需要 bool/float → int 转换(#if 条件编译语义)
|
||||
if isinstance(result, bool):
|
||||
return 1 if result else 0
|
||||
if isinstance(result, float):
|
||||
return 1 if result != 0.0 else 0
|
||||
return result
|
||||
|
||||
def _HandleIfLlvm(self, Node):
|
||||
Gen = self.Trans.LlvmGen
|
||||
|
||||
Reference in New Issue
Block a user