feat(ast): 加 Name/Attribute/Constant/BinOp 字段访问器,绕开跨模块 stub 字段访问陷阱
This commit is contained in:
1 parent
4f95867209
commit
802377e9ab
1 file changed
+49
@@ -53,6 +53,34 @@ class Name(ExprNode):
|
||||
return pos
|
||||
|
||||
|
||||
def name_get_id(node: Name | t.CPtr) -> str:
|
||||
"""获取 Name.id 字段(跨模块访问器,stub 字段访问陷阱防护)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.id
|
||||
|
||||
|
||||
def constant_get_str_val(node: Constant | t.CPtr) -> str:
|
||||
"""获取 Constant.str_val 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.str_val
|
||||
|
||||
|
||||
def binop_get_left(node: BinOp | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 BinOp.left 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.left
|
||||
|
||||
|
||||
def binop_get_right(node: BinOp | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 BinOp.right 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.right
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Constant nodes (subtype marked by const_kind)
|
||||
# ============================================================
|
||||
@@ -943,6 +971,27 @@ class Attribute(ExprNode):
|
||||
return pos
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 字段访问器(跨模块 stub 字段访问陷阱防护)
|
||||
#
|
||||
# 其他模块(如 TypeGenCType)直接访问 ast.Attribute 的字段时,
|
||||
# TPC 可能按 stub 类型生成错误 GEP(读垃圾地址 → 0xC0000005 崩溃)。
|
||||
# 本模块是完整类型,字段偏移正确;跨模块调用方用这些访问器。
|
||||
# ============================================================
|
||||
def attribute_get_value(node: Attribute | t.CPtr) -> AST | t.CPtr:
|
||||
"""获取 Attribute.value 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.value
|
||||
|
||||
|
||||
def attribute_get_attr(node: Attribute | t.CPtr) -> str:
|
||||
"""获取 Attribute.attr 字段(跨模块访问器)"""
|
||||
if node is None:
|
||||
return None
|
||||
return node.attr
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Subscript Node
|
||||
# ============================================================
|
||||
|
||||
Reference in new issue
Block a user