修正了部分代码的表达方式,最后可用版本,正在为 mlirlite 做准备
This commit is contained in:
1 parent
a5ceb3d4eb
commit
951b89be36
69 files changed
+8833
-8590
No files matched your search
@@ -20,22 +20,22 @@ import sys
|
||||
# 用于 .vpi 存根文件生成时计算 BinOp 表达式(如 A | B, A + B)的整数值。
|
||||
# 支持 BitOr/BitAnd/Add/Sub 以及嵌套的 Name(引用已注册的 CDefine 常量)。
|
||||
# ============================================================
|
||||
def _eval_binop_valid(node: rawptr[ast.AST]) -> int:
|
||||
def _eval_binop_valid(node: ast.AST*) -> int:
|
||||
"""检查 BinOp 节点是否可计算(所有叶子节点都是常量或已注册的 CDefine)"""
|
||||
if node is None: return 0
|
||||
k: int = node.kind()
|
||||
if k == ast.ASTKind.Constant:
|
||||
cn: rawptr[ast.Constant] = rawptr[ast.Constant](node)
|
||||
cn: ast.Constant* = ast.Constant*(node)
|
||||
if cn.const_kind == ast.CONST_INT:
|
||||
return 1
|
||||
return 0
|
||||
if k == ast.ASTKind.UnaryOp:
|
||||
uop: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](node)
|
||||
uop: ast.UnaryOp* = ast.UnaryOp*(node)
|
||||
if uop.operand is not None:
|
||||
return _eval_binop_valid(uop.operand)
|
||||
return 0
|
||||
if k == ast.ASTKind.BinOp:
|
||||
bop: rawptr[ast.BinOp] = rawptr[ast.BinOp](node)
|
||||
bop: ast.BinOp* = ast.BinOp*(node)
|
||||
if bop.left is not None and bop.right is not None:
|
||||
if _eval_binop_valid(bop.left) != 0:
|
||||
if _eval_binop_valid(bop.right) != 0:
|
||||
@@ -43,7 +43,7 @@ def _eval_binop_valid(node: rawptr[ast.AST]) -> int:
|
||||
return 0
|
||||
if k == ast.ASTKind.Name:
|
||||
# Name 引用:检查是否是已注册的 CDefine 常量
|
||||
nm: rawptr[ast.Name] = rawptr[ast.Name](node)
|
||||
nm: ast.Name* = ast.Name*(node)
|
||||
if nm.id is not None:
|
||||
# 从本地 CDefine 表查找
|
||||
val: int = _lookup_cdefine_for_pyi(nm.id)
|
||||
@@ -53,18 +53,18 @@ def _eval_binop_valid(node: rawptr[ast.AST]) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def _eval_binop_int(node: rawptr[ast.AST]) -> int64:
|
||||
def _eval_binop_int(node: ast.AST*) -> int64:
|
||||
"""递归计算 BinOp 节点的整数值(64 位,避免大整数 CDefine 截断)"""
|
||||
if node is None:
|
||||
return 0
|
||||
k: int = node.kind()
|
||||
if k == ast.ASTKind.Constant:
|
||||
cn: rawptr[ast.Constant] = rawptr[ast.Constant](node)
|
||||
cn: ast.Constant* = ast.Constant*(node)
|
||||
if cn.const_kind == ast.CONST_INT:
|
||||
return cn.int_val
|
||||
return 0
|
||||
if k == ast.ASTKind.UnaryOp:
|
||||
uop: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](node)
|
||||
uop: ast.UnaryOp* = ast.UnaryOp*(node)
|
||||
if uop.op == ast.OpKind.USub and uop.operand is not None:
|
||||
return -_eval_binop_int(uop.operand)
|
||||
if uop.op == ast.OpKind.UAdd and uop.operand is not None:
|
||||
@@ -73,7 +73,7 @@ def _eval_binop_int(node: rawptr[ast.AST]) -> int64:
|
||||
return ~_eval_binop_int(uop.operand)
|
||||
return 0
|
||||
if k == ast.ASTKind.BinOp:
|
||||
bop: rawptr[ast.BinOp] = rawptr[ast.BinOp](node)
|
||||
bop: ast.BinOp* = ast.BinOp*(node)
|
||||
if bop.left is None or bop.right is None:
|
||||
return 0
|
||||
lv: int64 = _eval_binop_int(bop.left)
|
||||
@@ -91,7 +91,7 @@ def _eval_binop_int(node: rawptr[ast.AST]) -> int64:
|
||||
if bop.op == ast.OpKind.RShift: return lv >> rv
|
||||
return 0
|
||||
if k == ast.ASTKind.Name:
|
||||
nm: rawptr[ast.Name] = rawptr[ast.Name](node)
|
||||
nm: ast.Name* = ast.Name*(node)
|
||||
if nm.id is not None:
|
||||
return _lookup_cdefine_for_pyi(nm.id)
|
||||
return 0
|
||||
@@ -130,13 +130,13 @@ def _lookup_cdefine_for_pyi(name: str) -> int64:
|
||||
return 0
|
||||
|
||||
|
||||
def _eval_cdefine_value_cache(node: rawptr[ast.AST]) -> int64:
|
||||
def _eval_cdefine_value_cache(node: ast.AST*) -> int64:
|
||||
"""求值 CDefine 值(Constant/UnaryOp/BinOp/Call/Name),供 vpi 预处理注册缓存"""
|
||||
if node is None:
|
||||
return 0
|
||||
k: int = node.kind()
|
||||
if k == ast.ASTKind.Constant:
|
||||
cn_c: rawptr[ast.Constant] = rawptr[ast.Constant](node)
|
||||
cn_c: ast.Constant* = ast.Constant*(node)
|
||||
if cn_c.const_kind == ast.CONST_INT:
|
||||
return cn_c.int_val
|
||||
if cn_c.const_kind == ast.CONST_BOOL:
|
||||
@@ -148,12 +148,12 @@ def _eval_cdefine_value_cache(node: rawptr[ast.AST]) -> int64:
|
||||
if k == ast.ASTKind.BinOp:
|
||||
return _eval_binop_int(node)
|
||||
if k == ast.ASTKind.Call:
|
||||
cl_c: rawptr[ast.Call] = rawptr[ast.Call](node)
|
||||
cl_c: ast.Call* = ast.Call*(node)
|
||||
if cl_c.args is not None and len(cl_c.args) > 0:
|
||||
return _eval_cdefine_value_cache(cl_c.args.get(0))
|
||||
return 0
|
||||
if k == ast.ASTKind.Name:
|
||||
nm_c: rawptr[ast.Name] = rawptr[ast.Name](node)
|
||||
nm_c: ast.Name* = ast.Name*(node)
|
||||
if nm_c.id is not None:
|
||||
return _lookup_cdefine_for_pyi(nm_c.id)
|
||||
return 0
|
||||
@@ -214,7 +214,7 @@ def _PyiIndent(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# 让下游按错误结果解析(与 TPC 落到未知节点 fail-fast 同语义)。
|
||||
# ============================================================
|
||||
def _PyiBinOpOpText(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
op: int, node: rawptr[ast.AST]) -> sizet:
|
||||
op: int, node: ast.AST*) -> sizet:
|
||||
"""按运算符种类输出 " OP " 文本,表外运算符 fail-fast"""
|
||||
if op == ast.OpKind.BitOr: return _PyiEmit(buf, buf_size, pos, " | ")
|
||||
if op == ast.OpKind.Add: return _PyiEmit(buf, buf_size, pos, " + ")
|
||||
@@ -239,7 +239,7 @@ def _PyiBinOpOpText(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# TPC 额外有 Index 分支(vast 遗留类,解析器不产出该节点)。
|
||||
# ============================================================
|
||||
def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
annot: rawptr[ast.AST],
|
||||
annot: ast.AST*,
|
||||
depth: int = 0) -> sizet:
|
||||
"""递归生成类型注解字符串,返回新的 pos"""
|
||||
# 递归深度守卫:AST 损坏成自引用环(Attribute.value/Subscript.slice/
|
||||
@@ -255,14 +255,14 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# Name: 简单类型名 (如 int, str, MyClass)
|
||||
if k == ast.ASTKind.Name:
|
||||
nm: rawptr[ast.Name] = rawptr[ast.Name](annot)
|
||||
nm: ast.Name* = ast.Name*(annot)
|
||||
if nm.id is not None:
|
||||
return _PyiEmit(buf, buf_size, pos, nm.id)
|
||||
return _PyiEmit(buf, buf_size, pos, "int")
|
||||
|
||||
# Attribute: 属性引用 (如 int, memhub.MemBuddy)
|
||||
if k == ast.ASTKind.Attribute:
|
||||
at: rawptr[ast.Attribute] = rawptr[ast.Attribute](annot)
|
||||
at: ast.Attribute* = ast.Attribute*(annot)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, at.value, depth + 1)
|
||||
pos = _PyiEmit(buf, buf_size, pos, ".")
|
||||
if at.attr is not None:
|
||||
@@ -271,7 +271,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# Subscript: 下标类型 (如 list[int], t.CArray[elem, count])
|
||||
if k == ast.ASTKind.Subscript:
|
||||
sub: rawptr[ast.Subscript] = rawptr[ast.Subscript](annot)
|
||||
sub: ast.Subscript* = ast.Subscript*(annot)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, sub.value, depth + 1)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "[")
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, sub.slice, depth + 1)
|
||||
@@ -282,7 +282,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# 2 * ZHUFF_MAX_CODES;条件宏实参 MASK_BYTE & 0x0F)。
|
||||
# 运算符文本按 _PyiBinOpOpText 输出,与 TPC 的运算符表逐字节一致。
|
||||
if k == ast.ASTKind.BinOp:
|
||||
bop: rawptr[ast.BinOp] = rawptr[ast.BinOp](annot)
|
||||
bop: ast.BinOp* = ast.BinOp*(annot)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, bop.left, depth + 1)
|
||||
pos = _PyiBinOpOpText(buf, buf_size, pos, bop.op, annot)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, bop.right, depth + 1)
|
||||
@@ -292,7 +292,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# 输出与 TPC 的 UnaryOp 分支逐字节一致:Not → "not "、Invert → "~"、
|
||||
# USub → "-";其余一元运算符两侧都不支持 → fail-fast(不静默写错符号)。
|
||||
if k == ast.ASTKind.UnaryOp:
|
||||
uop_t: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](annot)
|
||||
uop_t: ast.UnaryOp* = ast.UnaryOp*(annot)
|
||||
if uop_t.op == ast.OpKind.Not:
|
||||
pos = _PyiEmit(buf, buf_size, pos, "not ")
|
||||
elif uop_t.op == ast.OpKind.Invert:
|
||||
@@ -351,7 +351,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# Constant: 字符串前向引用 (如 'MyClass')、None 或数字常量(CArray count)
|
||||
if k == ast.ASTKind.Constant:
|
||||
cn: rawptr[ast.Constant] = rawptr[ast.Constant](annot)
|
||||
cn: ast.Constant* = ast.Constant*(annot)
|
||||
if cn.const_kind == ast.CONST_STR:
|
||||
pos = _PyiEmit(buf, buf_size, pos, "'")
|
||||
if cn.str_val is not None:
|
||||
@@ -378,7 +378,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# 的参数表成型为 List(TypeGenResolve 的类型名提取同样按 List 递归),
|
||||
# 故必须输出 "[a, b]" 形式——缺失该分支时该节点落入未知类型 fail-fast。
|
||||
if k == ast.ASTKind.List:
|
||||
lst: rawptr[ast.List] = rawptr[ast.List](annot)
|
||||
lst: ast.List* = ast.List*(annot)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "[")
|
||||
if lst.elts is not None:
|
||||
lst_elts: list[ast.AST*]* = lst.elts
|
||||
@@ -387,7 +387,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
while li < ln:
|
||||
if li > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
le: rawptr[ast.AST] = lst_elts.get(li)
|
||||
le: ast.AST* = lst_elts.get(li)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, le, depth + 1)
|
||||
li += 1
|
||||
pos = _PyiEmit(buf, buf_size, pos, "]")
|
||||
@@ -395,7 +395,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# Tuple: 多参数下标 (如 Dict[str, int])
|
||||
if k == ast.ASTKind.Tuple:
|
||||
tup: rawptr[ast.Tuple] = rawptr[ast.Tuple](annot)
|
||||
tup: ast.Tuple* = ast.Tuple*(annot)
|
||||
if tup.elts is not None:
|
||||
tup_elts: list[ast.AST*]* = tup.elts
|
||||
tn: sizet = len(tup_elts)
|
||||
@@ -403,14 +403,14 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
while ti < tn:
|
||||
if ti > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
el: rawptr[ast.AST] = tup_elts.get(ti)
|
||||
el: ast.AST* = tup_elts.get(ti)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, el, depth + 1)
|
||||
ti += 1
|
||||
return pos
|
||||
|
||||
# Call: 函数调用表达式 (用于装饰器如 @t.NoVTable())
|
||||
if k == ast.ASTKind.Call:
|
||||
cl: rawptr[ast.Call] = rawptr[ast.Call](annot)
|
||||
cl: ast.Call* = ast.Call*(annot)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, cl.func, depth + 1)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "(")
|
||||
if cl.args is not None:
|
||||
@@ -420,7 +420,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
while cai < can:
|
||||
if cai > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
ca: rawptr[ast.AST] = cl_args.get(cai)
|
||||
ca: ast.AST* = cl_args.get(cai)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, ca, depth + 1)
|
||||
cai += 1
|
||||
pos = _PyiEmit(buf, buf_size, pos, ")")
|
||||
@@ -441,7 +441,7 @@ def _PyiTypeStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# 支持: Constant(int/str/bool/None), Name(CDefine 常量), UnaryOp。
|
||||
# ============================================================
|
||||
def _PyiDefaultStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
node: rawptr[ast.AST],
|
||||
node: ast.AST*,
|
||||
depth: int = 0) -> sizet:
|
||||
"""生成默认值表达式文本到 buf,返回新的 pos"""
|
||||
# 递归深度守卫:AST 损坏成自引用环时 fail-fast 终止(同 _PyiTypeStr)
|
||||
@@ -454,7 +454,7 @@ def _PyiDefaultStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
k: int = node.kind()
|
||||
# Constant: 数字/字符串/bool/None 直接输出字面量
|
||||
if k == ast.ASTKind.Constant:
|
||||
cn: rawptr[ast.Constant] = rawptr[ast.Constant](node)
|
||||
cn: ast.Constant* = ast.Constant*(node)
|
||||
if cn.const_kind == ast.CONST_INT or cn.const_kind == ast.CONST_CHAR:
|
||||
num_buf_dv: str = stdlib.malloc(24)
|
||||
if num_buf_dv is not None:
|
||||
@@ -477,14 +477,14 @@ def _PyiDefaultStr(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# Name: CDefine 常量名(如 share: ULONG = SHARE_READ)
|
||||
if k == ast.ASTKind.Name:
|
||||
nm: rawptr[ast.Name] = rawptr[ast.Name](node)
|
||||
nm: ast.Name* = ast.Name*(node)
|
||||
if nm.id is not None:
|
||||
return _PyiEmit(buf, buf_size, pos, nm.id)
|
||||
return pos
|
||||
|
||||
# UnaryOp: 一元运算(如 -1, ~0)
|
||||
if k == ast.ASTKind.UnaryOp:
|
||||
uop: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](node)
|
||||
uop: ast.UnaryOp* = ast.UnaryOp*(node)
|
||||
if uop.op == ast.OpKind.USub:
|
||||
pos = _PyiEmit(buf, buf_size, pos, "-")
|
||||
elif uop.op == ast.OpKind.UAdd:
|
||||
@@ -515,14 +515,14 @@ def _PyiCreturnArgs(decorators: list[ast.AST*]*) -> list[ast.AST*]*:
|
||||
dn: sizet = len(decorators)
|
||||
di: sizet = 0
|
||||
while di < dn:
|
||||
dnode: rawptr[ast.AST] = decorators.get(di)
|
||||
dnode: ast.AST* = decorators.get(di)
|
||||
di += 1
|
||||
if dnode is None or dnode.kind() != ast.ASTKind.Call:
|
||||
continue
|
||||
dcall: rawptr[ast.Call] = rawptr[ast.Call](dnode)
|
||||
dcall: ast.Call* = ast.Call*(dnode)
|
||||
if dcall.func is None or dcall.func.kind() != ast.ASTKind.Attribute:
|
||||
continue
|
||||
dat: rawptr[ast.Attribute] = rawptr[ast.Attribute](dcall.func)
|
||||
dat: ast.Attribute* = ast.Attribute*(dcall.func)
|
||||
if dat.attr is not None and string.strcmp(dat.attr, "CReturn") == 0:
|
||||
return dcall.args
|
||||
return None
|
||||
@@ -534,7 +534,7 @@ def _PyiCreturnArgs(decorators: list[ast.AST*]*) -> list[ast.AST*]*:
|
||||
# 格式: def name(param: Type, ...) -> ReturnType: pass
|
||||
# ============================================================
|
||||
def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
fn: rawptr[ast.FunctionDef],
|
||||
fn: ast.FunctionDef*,
|
||||
indent_level: int, class_name: str) -> sizet:
|
||||
"""生成函数签名到 buf,返回新的 pos"""
|
||||
# 装饰器
|
||||
@@ -543,7 +543,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
dn: sizet = len(fn_decorator_list)
|
||||
di: sizet = 0
|
||||
while di < dn:
|
||||
dec: rawptr[ast.AST] = fn_decorator_list.get(di)
|
||||
dec: ast.AST* = fn_decorator_list.get(di)
|
||||
if dec is not None:
|
||||
pos = _PyiIndent(buf, buf_size, pos, indent_level)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "@")
|
||||
@@ -558,9 +558,9 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
pos = _PyiEmit(buf, buf_size, pos, "(")
|
||||
|
||||
# 参数列表
|
||||
args_node: rawptr[ast.AST] = fn.args
|
||||
args_node: ast.AST* = fn.args
|
||||
if args_node is not None:
|
||||
args: rawptr[ast.Arguments] = rawptr[ast.Arguments](args_node)
|
||||
args: ast.Arguments* = ast.Arguments*(args_node)
|
||||
first: int = 1
|
||||
|
||||
# 位置参数
|
||||
@@ -579,7 +579,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
if first == 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
first = 0
|
||||
ag: rawptr[ast.Arg] = rawptr[ast.Arg](args_args.get(ai))
|
||||
ag: ast.Arg* = ast.Arg*(args_args.get(ai))
|
||||
if ag.arg is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ag.arg)
|
||||
if ag.annotation is not None:
|
||||
@@ -587,7 +587,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, ag.annotation)
|
||||
if arg_defaults is not None and an - dn <= ai:
|
||||
di: sizet = ai - (an - dn)
|
||||
dnode: rawptr[ast.AST] = arg_defaults.get(di)
|
||||
dnode: ast.AST* = arg_defaults.get(di)
|
||||
if dnode is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = ")
|
||||
pos = _PyiDefaultStr(buf, buf_size, pos, dnode)
|
||||
@@ -599,7 +599,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
first = 0
|
||||
pos = _PyiEmit(buf, buf_size, pos, "*")
|
||||
va: rawptr[ast.Arg] = rawptr[ast.Arg](args.vararg)
|
||||
va: ast.Arg* = ast.Arg*(args.vararg)
|
||||
if va.arg is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, va.arg)
|
||||
|
||||
@@ -609,7 +609,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
first = 0
|
||||
pos = _PyiEmit(buf, buf_size, pos, "**")
|
||||
kw: rawptr[ast.Arg] = rawptr[ast.Arg](args.kwarg)
|
||||
kw: ast.Arg* = ast.Arg*(args.kwarg)
|
||||
if kw.arg is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, kw.arg)
|
||||
|
||||
@@ -647,7 +647,7 @@ def _PyiFunction(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# def method(self: Name, ...) -> RetType: pass
|
||||
# ============================================================
|
||||
def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
cls: rawptr[ast.ClassDef], indent_level: int) -> sizet:
|
||||
cls: ast.ClassDef*, indent_level: int) -> sizet:
|
||||
"""生成类定义到 buf,返回新的 pos"""
|
||||
# 装饰器
|
||||
if cls.decorator_list is not None:
|
||||
@@ -655,7 +655,7 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
dcn: sizet = len(cls_decorator_list)
|
||||
dci: sizet = 0
|
||||
while dci < dcn:
|
||||
ddec: rawptr[ast.AST] = cls_decorator_list.get(dci)
|
||||
ddec: ast.AST* = cls_decorator_list.get(dci)
|
||||
if ddec is not None:
|
||||
pos = _PyiIndent(buf, buf_size, pos, indent_level)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "@")
|
||||
@@ -694,7 +694,7 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
while bi < bn:
|
||||
if bi > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
base: rawptr[ast.AST] = cls_bases.get(bi)
|
||||
base: ast.AST* = cls_bases.get(bi)
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, base)
|
||||
bi += 1
|
||||
pos = _PyiEmit(buf, buf_size, pos, ")")
|
||||
@@ -709,13 +709,13 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
cn: sizet = len(cls_children)
|
||||
ci: sizet = 0
|
||||
while ci < cn:
|
||||
stmt: rawptr[ast.AST] = cls_children.get(ci)
|
||||
stmt: ast.AST* = cls_children.get(ci)
|
||||
if stmt is not None:
|
||||
sk: int = stmt.kind()
|
||||
|
||||
# 方法
|
||||
if sk == ast.ASTKind.FunctionDef:
|
||||
mfn: rawptr[ast.FunctionDef] = rawptr[ast.FunctionDef](stmt)
|
||||
mfn: ast.FunctionDef* = ast.FunctionDef*(stmt)
|
||||
# __init__ 方法:先提取 self.x: Type 属性
|
||||
if mfn.name is not None and string.strcmp(mfn.name, "__init__") == 0:
|
||||
if mfn.children is not None:
|
||||
@@ -723,14 +723,14 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
icn: sizet = len(mfn_children)
|
||||
ici: sizet = 0
|
||||
while ici < icn:
|
||||
ibody: rawptr[ast.AST] = mfn_children.get(ici)
|
||||
ibody: ast.AST* = mfn_children.get(ici)
|
||||
if ibody is not None and ibody.kind() == ast.ASTKind.AnnAssign:
|
||||
iaa: rawptr[ast.AnnAssign] = rawptr[ast.AnnAssign](ibody)
|
||||
iaa: ast.AnnAssign* = ast.AnnAssign*(ibody)
|
||||
if iaa.target is not None and iaa.target.kind() == ast.ASTKind.Attribute:
|
||||
iat: rawptr[ast.Attribute] = rawptr[ast.Attribute](iaa.target)
|
||||
iat: ast.Attribute* = ast.Attribute*(iaa.target)
|
||||
# 检查 target.value 是否为 Name("self")
|
||||
if iat.value is not None and iat.value.kind() == ast.ASTKind.Name:
|
||||
iavn: rawptr[ast.Name] = rawptr[ast.Name](iat.value)
|
||||
iavn: ast.Name* = ast.Name*(iat.value)
|
||||
if iavn.id is None or string.strcmp(iavn.id, "self") != 0:
|
||||
ici += 1
|
||||
continue
|
||||
@@ -747,9 +747,9 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# 带注解的属性
|
||||
elif sk == ast.ASTKind.AnnAssign:
|
||||
aa: rawptr[ast.AnnAssign] = rawptr[ast.AnnAssign](stmt)
|
||||
aa: ast.AnnAssign* = ast.AnnAssign*(stmt)
|
||||
if aa.target is not None and aa.target.kind() == ast.ASTKind.Name:
|
||||
tn: rawptr[ast.Name] = rawptr[ast.Name](aa.target)
|
||||
tn: ast.Name* = ast.Name*(aa.target)
|
||||
pos = _PyiIndent(buf, buf_size, pos, indent_level + 1)
|
||||
if tn.id is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, tn.id)
|
||||
@@ -760,7 +760,7 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
|
||||
# 嵌套类
|
||||
elif sk == ast.ASTKind.ClassDef:
|
||||
ncls: rawptr[ast.ClassDef] = rawptr[ast.ClassDef](stmt)
|
||||
ncls: ast.ClassDef* = ast.ClassDef*(stmt)
|
||||
pos = _PyiClass(buf, buf_size, pos, ncls, indent_level + 1)
|
||||
has_member = 1
|
||||
ci += 1
|
||||
@@ -779,7 +779,7 @@ def _PyiClass(buf: bytes, buf_size: sizet, pos: sizet,
|
||||
# Args: mb 内存池(保留以符合调用约定,实际未使用);tree AST Module 根节点;rel_path 源文件路径(仅用于报错归因的"当前文件",应传绝对路径以便 fatal_error 读取源码行);buf 输出缓冲区;buf_size 缓冲区大小
|
||||
# Returns: 写入的字节数(0 表示失败)
|
||||
# ============================================================
|
||||
def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*,
|
||||
rel_path: str, buf: bytes,
|
||||
buf_size: sizet) -> sizet:
|
||||
"""从 AST 生成 .vpi 存根文件内容到 buf"""
|
||||
@@ -809,14 +809,14 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
# 第一遍:数本文件 t.CDefine 声明数(C 数组需先定长分配)
|
||||
_g_pyi_cdef_count = 0
|
||||
for pci0 in range(pcn):
|
||||
stmt_pc0: rawptr[ast.AST] = pre_children.get(pci0)
|
||||
stmt_pc0: ast.AST* = pre_children.get(pci0)
|
||||
if stmt_pc0 is not None and stmt_pc0.kind() == ast.ASTKind.AnnAssign:
|
||||
aa_pc0: rawptr[ast.AnnAssign] = rawptr[ast.AnnAssign](stmt_pc0)
|
||||
aa_pc0: ast.AnnAssign* = ast.AnnAssign*(stmt_pc0)
|
||||
if aa_pc0.target is not None and aa_pc0.target.kind() == ast.ASTKind.Name \
|
||||
and aa_pc0.annotation is not None:
|
||||
tgt_pc0: rawptr[ast.Name] = rawptr[ast.Name](aa_pc0.target)
|
||||
tgt_pc0: ast.Name* = ast.Name*(aa_pc0.target)
|
||||
if tgt_pc0.id is not None and aa_pc0.annotation.kind() == ast.ASTKind.Attribute:
|
||||
ann_pc0: rawptr[ast.Attribute] = rawptr[ast.Attribute](aa_pc0.annotation)
|
||||
ann_pc0: ast.Attribute* = ast.Attribute*(aa_pc0.annotation)
|
||||
if ann_pc0.attr is not None and string.strcmp(ann_pc0.attr, "CDefine") == 0:
|
||||
_g_pyi_cdef_count += 1
|
||||
if _g_pyi_cdef_count > 0:
|
||||
@@ -826,14 +826,14 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
pci: sizet = 0
|
||||
cdi: int = 0
|
||||
for pci in range(pcn):
|
||||
stmt_pc: rawptr[ast.AST] = pre_children.get(pci)
|
||||
stmt_pc: ast.AST* = pre_children.get(pci)
|
||||
if stmt_pc is not None and stmt_pc.kind() == ast.ASTKind.AnnAssign:
|
||||
aa_pc: rawptr[ast.AnnAssign] = rawptr[ast.AnnAssign](stmt_pc)
|
||||
aa_pc: ast.AnnAssign* = ast.AnnAssign*(stmt_pc)
|
||||
if aa_pc.target is not None and aa_pc.target.kind() == ast.ASTKind.Name \
|
||||
and aa_pc.annotation is not None:
|
||||
tgt_pc: rawptr[ast.Name] = rawptr[ast.Name](aa_pc.target)
|
||||
tgt_pc: ast.Name* = ast.Name*(aa_pc.target)
|
||||
if tgt_pc.id is not None and aa_pc.annotation.kind() == ast.ASTKind.Attribute:
|
||||
ann_pc: rawptr[ast.Attribute] = rawptr[ast.Attribute](aa_pc.annotation)
|
||||
ann_pc: ast.Attribute* = ast.Attribute*(aa_pc.annotation)
|
||||
if ann_pc.attr is not None and string.strcmp(ann_pc.attr, "CDefine") == 0:
|
||||
if _g_pyi_cdef_names is not None and _g_pyi_cdef_vals is not None:
|
||||
slot_pc: str | t.CPtr = t.CPtr(uint64(_g_pyi_cdef_names) + cdi * 8)
|
||||
@@ -856,14 +856,14 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
has_import_t: int = 0
|
||||
has_import_c: int = 0
|
||||
for i in range(n):
|
||||
stmt: rawptr[ast.AST] = tree_children.get(i)
|
||||
stmt: ast.AST* = tree_children.get(i)
|
||||
if stmt is not None and stmt.kind() == ast.ASTKind.Import:
|
||||
imp: rawptr[ast.Import] = rawptr[ast.Import](stmt)
|
||||
imp: ast.Import* = ast.Import*(stmt)
|
||||
if imp.names is not None:
|
||||
imp_names: list[ast.AST*]* = imp.names
|
||||
an: sizet = len(imp_names)
|
||||
for ai in range(an):
|
||||
al: rawptr[ast.Alias] = rawptr[ast.Alias](imp_names.get(ai))
|
||||
al: ast.Alias* = ast.Alias*(imp_names.get(ai))
|
||||
if al is not None and al.name is not None:
|
||||
if string.strcmp(al.name, "t") == 0:
|
||||
has_import_t = 1
|
||||
@@ -888,7 +888,7 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
|
||||
# Import 语句
|
||||
if k == ast.ASTKind.Import:
|
||||
imp2: rawptr[ast.Import] = rawptr[ast.Import](stmt)
|
||||
imp2: ast.Import* = ast.Import*(stmt)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "import ")
|
||||
if imp2.names is not None:
|
||||
imp2_names: list[ast.AST*]* = imp2.names
|
||||
@@ -897,7 +897,7 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
while ai2 < an2:
|
||||
if ai2 > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
al2: rawptr[ast.Alias] = rawptr[ast.Alias](imp2_names.get(ai2))
|
||||
al2: ast.Alias* = ast.Alias*(imp2_names.get(ai2))
|
||||
if al2 is not None and al2.name is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, al2.name)
|
||||
if al2.asname is not None and string.strlen(al2.asname) > 0:
|
||||
@@ -908,7 +908,7 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
|
||||
# ImportFrom 语句
|
||||
elif k == ast.ASTKind.ImportFrom:
|
||||
ifr: rawptr[ast.ImportFrom] = rawptr[ast.ImportFrom](stmt)
|
||||
ifr: ast.ImportFrom* = ast.ImportFrom*(stmt)
|
||||
pos = _PyiEmit(buf, buf_size, pos, "from ")
|
||||
lvl: int = ifr.level
|
||||
while lvl > 0:
|
||||
@@ -924,7 +924,7 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
while ai3 < an3:
|
||||
if ai3 > 0:
|
||||
pos = _PyiEmit(buf, buf_size, pos, ", ")
|
||||
al3: rawptr[ast.Alias] = rawptr[ast.Alias](ifr_names.get(ai3))
|
||||
al3: ast.Alias* = ast.Alias*(ifr_names.get(ai3))
|
||||
if al3 is not None and al3.name is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, al3.name)
|
||||
if al3.asname is not None and string.strlen(al3.asname) > 0:
|
||||
@@ -935,19 +935,19 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
|
||||
# 函数定义(_PyiFunction 已以 "\n" 收尾,不再补空行)
|
||||
elif k == ast.ASTKind.FunctionDef:
|
||||
fn: rawptr[ast.FunctionDef] = rawptr[ast.FunctionDef](stmt)
|
||||
fn: ast.FunctionDef* = ast.FunctionDef*(stmt)
|
||||
pos = _PyiFunction(buf, buf_size, pos, fn, 0, "")
|
||||
|
||||
# 类定义
|
||||
elif k == ast.ASTKind.ClassDef:
|
||||
cls: rawptr[ast.ClassDef] = rawptr[ast.ClassDef](stmt)
|
||||
cls: ast.ClassDef* = ast.ClassDef*(stmt)
|
||||
pos = _PyiClass(buf, buf_size, pos, cls, 0)
|
||||
|
||||
# 带注解的变量声明
|
||||
elif k == ast.ASTKind.AnnAssign:
|
||||
aa: rawptr[ast.AnnAssign] = rawptr[ast.AnnAssign](stmt)
|
||||
aa: ast.AnnAssign* = ast.AnnAssign*(stmt)
|
||||
if aa.target is not None and aa.target.kind() == ast.ASTKind.Name:
|
||||
nm: rawptr[ast.Name] = rawptr[ast.Name](aa.target)
|
||||
nm: ast.Name* = ast.Name*(aa.target)
|
||||
if nm.id is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, nm.id)
|
||||
pos = _PyiEmit(buf, buf_size, pos, ": ")
|
||||
@@ -957,17 +957,17 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
# 不是整数,用 _PyiTypeStr 输出类型字符串
|
||||
is_ctypedef_ann: int = 0
|
||||
if aa.annotation is not None and aa.annotation.kind() == ast.ASTKind.Attribute:
|
||||
ctd_at: rawptr[ast.Attribute] = rawptr[ast.Attribute](aa.annotation)
|
||||
ctd_at: ast.Attribute* = ast.Attribute*(aa.annotation)
|
||||
if ctd_at.attr is not None and string.strcmp(ctd_at.attr, "CTypedef") == 0:
|
||||
if ctd_at.value is not None and ctd_at.value.kind() == ast.ASTKind.Name:
|
||||
ctd_mod: rawptr[ast.Name] = rawptr[ast.Name](ctd_at.value)
|
||||
ctd_mod: ast.Name* = ast.Name*(ctd_at.value)
|
||||
if ctd_mod.id is not None and string.strcmp(ctd_mod.id, "t") == 0:
|
||||
is_ctypedef_ann = 1
|
||||
if is_ctypedef_ann != 0 and aa.value is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = ")
|
||||
pos = _PyiTypeStr(buf, buf_size, pos, aa.value)
|
||||
elif aa.value is not None and aa.value.kind() == ast.ASTKind.Constant:
|
||||
cv: rawptr[ast.Constant] = rawptr[ast.Constant](aa.value)
|
||||
cv: ast.Constant* = ast.Constant*(aa.value)
|
||||
num_buf: bytes = mb.alloc(32)
|
||||
if cv.const_kind == ast.CONST_INT and num_buf is not None:
|
||||
viperlib.snprintf(num_buf, 32, " = %lld", cv.int_val)
|
||||
@@ -995,11 +995,11 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
elif aa.value is not None and aa.value.kind() == ast.ASTKind.UnaryOp:
|
||||
# 处理负值常量(如 INVALID_HANDLE_VALUE = -1)
|
||||
# UnaryOp(USub, Constant(1)) → -1
|
||||
uop_cv: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](aa.value)
|
||||
uop_cv: ast.UnaryOp* = ast.UnaryOp*(aa.value)
|
||||
num_buf_u: bytes = mb.alloc(32)
|
||||
if uop_cv.op == ast.OpKind.USub and uop_cv.operand is not None \
|
||||
and uop_cv.operand.kind() == ast.ASTKind.Constant:
|
||||
uop_const: rawptr[ast.Constant] = rawptr[ast.Constant](uop_cv.operand)
|
||||
uop_const: ast.Constant* = ast.Constant*(uop_cv.operand)
|
||||
if uop_const.const_kind == ast.CONST_INT and num_buf_u is not None:
|
||||
neg_val: int64 = -uop_const.int_val
|
||||
viperlib.snprintf(num_buf_u, 32, " = %lld", neg_val)
|
||||
@@ -1007,15 +1007,15 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
elif aa.value is not None and aa.value.kind() == ast.ASTKind.Call:
|
||||
# 处理类型构造函数常量(如 t.CUnsignedLong(-11))
|
||||
# 提取第一个参数的整数值并输出
|
||||
call_cv: rawptr[ast.Call] = rawptr[ast.Call](aa.value)
|
||||
call_cv: ast.Call* = ast.Call*(aa.value)
|
||||
if call_cv.args is not None:
|
||||
call_args_cv: list[ast.AST*]* = call_cv.args
|
||||
if len(call_args_cv) > 0:
|
||||
first_arg: rawptr[ast.AST] = call_args_cv.get(0)
|
||||
first_arg: ast.AST* = call_args_cv.get(0)
|
||||
if first_arg is not None:
|
||||
arg_kind: int = first_arg.kind()
|
||||
if arg_kind == ast.ASTKind.Constant:
|
||||
ca_cv: rawptr[ast.Constant] = rawptr[ast.Constant](first_arg)
|
||||
ca_cv: ast.Constant* = ast.Constant*(first_arg)
|
||||
num_buf_c: bytes = mb.alloc(32)
|
||||
if ca_cv.const_kind == ast.CONST_INT and num_buf_c is not None:
|
||||
viperlib.snprintf(num_buf_c, 32, " = %lld", ca_cv.int_val)
|
||||
@@ -1029,11 +1029,11 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
viperlib.snprintf(fbuf_cc, 64, " = %.17g", ca_cv.float_val)
|
||||
pos = _PyiEmit(buf, buf_size, pos, fbuf_cc)
|
||||
elif arg_kind == ast.ASTKind.UnaryOp:
|
||||
uop_ca: rawptr[ast.UnaryOp] = rawptr[ast.UnaryOp](first_arg)
|
||||
uop_ca: ast.UnaryOp* = ast.UnaryOp*(first_arg)
|
||||
num_buf_cu: bytes = mb.alloc(32)
|
||||
if uop_ca.op == ast.OpKind.USub and uop_ca.operand is not None \
|
||||
and uop_ca.operand.kind() == ast.ASTKind.Constant:
|
||||
uoc: rawptr[ast.Constant] = rawptr[ast.Constant](uop_ca.operand)
|
||||
uoc: ast.Constant* = ast.Constant*(uop_ca.operand)
|
||||
if uoc.const_kind == ast.CONST_INT and num_buf_cu is not None:
|
||||
neg_c_val: int64 = -uoc.int_val
|
||||
viperlib.snprintf(num_buf_cu, 32, " = %lld", neg_c_val)
|
||||
@@ -1067,7 +1067,7 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
elif aa.value is not None and aa.value.kind() == ast.ASTKind.Name:
|
||||
# 处理 Name 引用常量(如 ALIAS = OTHER_CONST)
|
||||
# 输出引用的名字,让消费方在本地查找
|
||||
name_val: rawptr[ast.Name] = rawptr[ast.Name](aa.value)
|
||||
name_val: ast.Name* = ast.Name*(aa.value)
|
||||
if name_val.id is not None:
|
||||
pos = _PyiEmit(buf, buf_size, pos, " = ")
|
||||
pos = _PyiEmit(buf, buf_size, pos, name_val.id)
|
||||
@@ -1075,19 +1075,19 @@ def _GeneratePyiFromAst(mb: rawptr[memhub.MemBuddy], tree: rawptr[ast.AST],
|
||||
|
||||
# 全局赋值(无注解)— 从 Constant value 推断类型
|
||||
elif k == ast.ASTKind.Assign:
|
||||
asg: rawptr[ast.Assign] = rawptr[ast.Assign](stmt)
|
||||
asg: ast.Assign* = ast.Assign*(stmt)
|
||||
if asg.targets is not None and asg.value is not None:
|
||||
asg_targets: list[ast.AST*]* = asg.targets
|
||||
atn: sizet = len(asg_targets)
|
||||
if atn == 1:
|
||||
atg: rawptr[ast.AST] = asg_targets.get(0)
|
||||
atg: ast.AST* = asg_targets.get(0)
|
||||
if atg is not None and atg.kind() == ast.ASTKind.Name:
|
||||
agn: rawptr[ast.Name] = rawptr[ast.Name](atg)
|
||||
aval: rawptr[ast.AST] = asg.value
|
||||
agn: ast.Name* = ast.Name*(atg)
|
||||
aval: ast.AST* = asg.value
|
||||
avk: int = aval.kind()
|
||||
inferred: int = 0
|
||||
if avk == ast.ASTKind.Constant:
|
||||
acv: rawptr[ast.Constant] = rawptr[ast.Constant](aval)
|
||||
acv: ast.Constant* = ast.Constant*(aval)
|
||||
if acv.const_kind == ast.CONST_INT:
|
||||
inferred = 1
|
||||
elif acv.const_kind == ast.CONST_FLOAT:
|
||||
|
||||
Reference in new issue
Block a user