diff --git a/lib/StubGen/Converter.py b/lib/StubGen/Converter.py index b2d1080..3daccb9 100644 --- a/lib/StubGen/Converter.py +++ b/lib/StubGen/Converter.py @@ -116,18 +116,8 @@ class PythonToStubConverter: if not HasImportT or not HasImportC: StubLines.append('') - # 添加 c.CPragma("once") - # StubLines.append('c.CPragma("once")') StubLines.append('') - # 生成文件级别宏守卫名称 - #FileGuardName = f'__{ModuleName.upper()}_DEFINE__' - - # 添加文件开头宏守卫 - #StubLines.append(f'c.CIfndef({FileGuardName})') - #StubLines.append(f'{FileGuardName}: t.CDefine') - #StubLines.append('') - # 解析 Python 代码,提取类型定义(复用上方的 parse 结果) try: if tree is None: @@ -220,7 +210,7 @@ class PythonToStubConverter: StubLines.append('') StubLines.append(ImportStr) elif isinstance(node, ast.Expr): - # 处理模块级别的宏调用,如 c.CEndif() + # 处理模块级别的宏调用,如 c.CUndef(...) / c.CPragma(...) ExprStr: str = PythonToStubConverter._GetExprString(node.value) if ExprStr: CurrentType = 'macro' @@ -237,10 +227,6 @@ class PythonToStubConverter: if CurrentType: PrevType = CurrentType - # 添加文件结尾宏守卫 - #StubLines.append('') - #StubLines.append('c.CEndif()') - except SyntaxError as e: # 语法错误直接终止(fail-fast),不允许生成残缺 stub 被全局缓存命中。 # 残缺 vpi 只含 import 头,函数/类型声明全缺,会在后续阶段以 @@ -440,7 +426,7 @@ class PythonToStubConverter: FuncStub: list[str] = PythonToStubConverter._ConvertFunction(item, indent=indent+1, SourceLines=SourceLines, class_name=node.name) lines.extend(FuncStub) elif isinstance(item, ast.Expr): - # 处理宏调用,如 c.CEndif() + # 处理宏调用,如 c.CUndef(...) / c.CPragma(...) ExprStr: str = PythonToStubConverter._GetExprString(item.value) if ExprStr: lines.append(f'{IndentStr} {ExprStr}') @@ -747,8 +733,8 @@ class PythonToStubConverter: return f'{LeftStr} / {RightStr}' elif isinstance(annotation.op, ast.Mod): return f'{LeftStr} % {RightStr}' - # 位与/右移:条件宏实参(如 c.CIf(MASK_BYTE & 0x0F)、 - # c.CIf((MAX_SIZE >> SHIFT_BITS) > 0))。两运算符必须与 TPV 的 + # 位与/右移:条件编译实参(如 cif MASK_BYTE & 0x0F、 + # cif (MAX_SIZE >> SHIFT_BITS) > 0)。两运算符必须与 TPV 的 # _PyiBinOpOpText 保持一致,否则同一份源码两侧写出的 vpi 文本不同。 elif isinstance(annotation.op, ast.BitAnd): return f'{LeftStr} & {RightStr}' @@ -791,7 +777,7 @@ class PythonToStubConverter: result += f' {op} {comparators[i]}' return result elif isinstance(annotation, ast.BoolOp): - # 布尔运算:条件宏实参(如 c.CIf(ENABLED and MAX_SIZE > 0))。 + # 布尔运算:条件编译实参(如 cif ENABLED and MAX_SIZE > 0)。 # 文本与 TPV 的 BoolOp 分支一致:and → ' and '、or → ' or '。 Joiner: str = ' or ' if isinstance(annotation.op, ast.Or) else ' and ' return Joiner.join([PythonToStubConverter._GetTypeString(v) for v in annotation.values]) @@ -988,7 +974,7 @@ class PythonToStubConverter: def _GetExprString(expr: ast.AST) -> str: """获取表达式字符串(用于宏调用)""" if isinstance(expr, ast.Call): - # 处理函数调用,如 c.CEndif(), c.CUndef(...), c.CPragma(...) + # 处理函数调用,如 c.CUndef(...), c.CPragma(...) # (旧的 c.CIf/c.CElif/c.CElse 已删除:cif/celif/celse 由 _ProcessIfMacro 渲染) FuncStr: str = PythonToStubConverter._GetDecoratorString(expr.func) if FuncStr and ('CEndif' in FuncStr or 'CUndef' in FuncStr or 'CPragma' in FuncStr): @@ -998,7 +984,7 @@ class PythonToStubConverter: @staticmethod def _RestoreDefinedMarkers(node: ast.AST) -> ast.AST: - """把内部标记 c.CIfdef(N) 递归还原为 defined(N) 调用(供 stub 文本渲染)。 + """把内部标记 c.@CIfdef(N) 递归还原为 defined(N) 调用(供 stub 文本渲染)。 cif/celif 条件的 desugar 会把 defined(NAME) 变成 c.@CIfdef(NAME) 标记调用; 生成 .vpi 文本时必须还原为用户可写的 defined(NAME),否则存根不可再解析。 diff --git a/lib/core/ConstEvaluator.py b/lib/core/ConstEvaluator.py index d1da49e..602f07a 100644 --- a/lib/core/ConstEvaluator.py +++ b/lib/core/ConstEvaluator.py @@ -566,7 +566,7 @@ class EvalContext: return None def is_macro_defined(self, name: str) -> bool: - """判定宏 NAME 是否已定义(供 defined(NAME) / c.CIfdef 标记使用)。 + """判定宏 NAME 是否已定义(供 defined(NAME) / c.@CIfdef 标记使用)。 来源与 HandlesIf._is_macro_defined 同口径:Gen._define_constants / _DefineConstants / SymbolTable.is_define / 平台宏。 diff --git a/lib/core/Handles/HandlesExprCall.py b/lib/core/Handles/HandlesExprCall.py index 43d8c46..32a352c 100644 --- a/lib/core/Handles/HandlesExprCall.py +++ b/lib/core/Handles/HandlesExprCall.py @@ -31,8 +31,8 @@ _LEN_SUPPORTED_HINT: str = ( # 用户再写即在调用点 FailFast,指向原生 cif/celif/celse 与 defined(NAME)(英文报文, # 与 TransPyV CallGenDispatch.vp 同口径)。内部标记 c.@CIf 族不受影响。 _REMOVED_CIF_CALL_MSG: str = ( - "c.CIf / c.CIfdef / c.CIfndef were removed; use the cif / celif / celse statements " - "together with the defined(NAME) predicate instead " + "c.CIf / c.CIfdef / c.CIfndef / c.CElif / c.CElse / c.CEndif were removed; use the cif / " + "celif / celse statements together with the defined(NAME) predicate instead " "(e.g. `cif defined(NAME):` ... `celif ...:` ... `celse:` ...)") diff --git a/lib/core/Handles/HandlesIf.py b/lib/core/Handles/HandlesIf.py index ff2798a..2e92297 100644 --- a/lib/core/Handles/HandlesIf.py +++ b/lib/core/Handles/HandlesIf.py @@ -147,7 +147,7 @@ class IfHandle(BaseHandle): ctx: EvalContext = EvalContext(Gen=Gen, symtab=self.Trans.SymbolTable, translator=self.Trans) result: int | float | bool | None = ConstEvaluator.eval_full(node, ctx) if result is None: - # 保留 CIf 嵌套调用处理(HandlesIf 特有逻辑) + # 保留 cif 标记的嵌套调用处理(HandlesIf 特有逻辑) if isinstance(node, ast.Call) and self._is_cif_call(node): return self._evaluate_cif_condition(node) return None diff --git a/lib/core/Handles/HandlesSpecialCall.py b/lib/core/Handles/HandlesSpecialCall.py index 4a9ef68..cbc40aa 100644 --- a/lib/core/Handles/HandlesSpecialCall.py +++ b/lib/core/Handles/HandlesSpecialCall.py @@ -13,18 +13,6 @@ from lib.constants.config import mode as _config_mode class CSpecialCallHandle(BaseHandle): - def _HandleCIfLlvm(self, Node: ast.Call) -> ir.Value: - Gen: LlvmGeneratorMixin = self.Trans.LlvmGen - if Node.args: - ArgVal: ir.Value | None = self.HandleExprLlvm(Node.args[0]) - if ArgVal: - if isinstance(ArgVal, ir.Constant) and isinstance(ArgVal.type, ir.IntType): - return ir.Constant(ir.IntType(1), 1 if ArgVal.constant != 0 else 0) - if isinstance(ArgVal.type, ir.IntType): - return Gen.builder.icmp_signed('!=', ArgVal, Gen._ZeroConst(ArgVal.type), name="cif_cond") - if isinstance(ArgVal.type, ir.PointerType): - return Gen.builder.icmp_signed('!=', ArgVal, Gen._ZeroConst(ArgVal.type), name="cif_cond") - return ir.Constant(ir.IntType(1), 0) def _HandleCAddrLlvm(self, Node: ast.Call) -> ir.Value: Gen: LlvmGeneratorMixin = self.Trans.LlvmGen if not Node.args: diff --git a/lib/includes/c.py b/lib/includes/c.py index d2c86ec..bb2b46f 100644 --- a/lib/includes/c.py +++ b/lib/includes/c.py @@ -510,42 +510,6 @@ class TokenPast: return [''] -class CElif: - """#elif 条件编译""" - def __init__(self, condition: Any) -> None: - self.condition: Any = condition - - @staticmethod - def HandleCall(translator: Any, args: Any, keywords: Any) -> list[str]: - """处理 c.CElif() 调用""" - if args: - condition: Any = translator.HandleExpr(args[0])[0] - return ['#elif ' + _to_str(condition)] - return ['#else'] - - -class CElse: - """#else 条件编译""" - def __init__(self) -> None: - pass - - @staticmethod - def HandleCall(translator: Any, args: Any, keywords: Any) -> list[str]: - """处理 c.CElse() 调用""" - return ['#else'] - - -class CEndif: - """#endif 条件编译""" - def __init__(self) -> None: - pass - - @staticmethod - def HandleCall(translator: Any, args: Any, keywords: Any) -> list[str]: - """处理 c.CEndif() 调用""" - return ['#endif'] - - class CUndef: """#undef 取消宏定义""" def __init__(self, name: Any) -> None: @@ -648,9 +612,6 @@ Library_C = { 'AsmInp': AsmInp, 'AsmOut': AsmOut, 'CDefine': CDefine, - 'CElif': CElif, - 'CElse': CElse, - 'CEndif': CEndif, 'CUndef': CUndef, 'CError': CError, 'TokenPast': TokenPast, diff --git a/lib/vast/ExprParser.py b/lib/vast/ExprParser.py index d13caac..a6dee14 100644 --- a/lib/vast/ExprParser.py +++ b/lib/vast/ExprParser.py @@ -601,7 +601,7 @@ class ExprParserMixin(TokenStream): """调用点的特殊 desugar / fail-fast。 - `defined(NAME)`:仅 cif/celif 条件内合法(`self._InCIfCond`), - desugar 为内部标记调用 `c.CIfdef(NAME)`;其余位置 FailFast。 + desugar 为内部标记调用 `c.@CIfdef(NAME)`;其余位置 FailFast。 - `c.CIf` / `c.CIfdef` / `c.CIfndef`(及裸名):已删除,FailFast 指向新语法。 """ callee: str = self._CallCalleeName(node.func)