Files
TransPyC/lib/core/SymbolReexporter.py

77 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Pass 4: PackageReexporter — 将符号重新导出到命名空间前缀下"""
from __future__ import annotations
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
from lib.core.SymbolTable import SymbolTable
from lib.core.SymbolData import ModuleSymbols
class PackageReexporter:
"""将已解析的符号重新导出到命名空间前缀下(如 mpool.MPool"""
def __init__(self, symbol_table: SymbolTable):
self._symtab = symbol_table
def reexport(self, module_symbols: ModuleSymbols, prefixes: List[str], lineno: int = 0) -> List[str]:
"""将符号重新导出到所有命名空间前缀下,返回新增的符号名列表"""
loaded = []
file_path = module_symbols.file_path
primary_prefix = prefixes[0] if prefixes else None
for prefix in prefixes:
# 插入类符号(前缀.类名)
for cls in module_symbols.classes:
full_name = f"{prefix}.{cls.name}"
self._symtab._InsertClassSymbol(
full_name, cls.type_kind, cls.lineno, file_path,
cls.members, cls.is_cpython_object, cls.is_packed
)
loaded.append(full_name)
# 插入枚举成员(前缀.成员名)
if cls.type_kind == 'enum':
for member_name, _member_value, member_lineno in cls.enum_members:
full_member_name = f"{prefix}.{member_name}"
self._symtab._InsertEnumMemberSymbol(full_member_name, cls.name, member_lineno, file_path)
loaded.append(full_member_name)
# 插入 typedef 符号(前缀.名称)
for td in module_symbols.typedefs:
full_name = f"{prefix}.{td.name}"
self._symtab._InsertTypedefSymbol(
full_name, td.original_type_kind, td.original_class,
td.lineno, file_path, td.members
)
loaded.append(full_name)
# 插入函数符号(前缀.名称)
for func in module_symbols.functions:
full_name = f"{prefix}.{func.name}"
self._symtab._InsertFuncSymbol(
full_name, func.ret_type, func.param_types,
func.lineno, file_path, func.is_variadic, func.is_inline
)
loaded.append(full_name)
# 插入 define 符号(前缀.名称)
for define in module_symbols.defines:
full_name = f"{prefix}.{define.name}"
self._symtab._InsertDefineSymbol(
full_name, define.value, define.lineno, file_path
)
loaded.append(full_name)
# 插入模块别名
self._symtab._InsertModuleSymbol(prefix, lineno, file_path)
# 插入匿名类型符号(前缀.名称)
for anon_name, anon_data in module_symbols.anonymous_types.items():
full_type_name = f"{prefix}.{anon_name}"
self._symtab._InsertAnonymousSymbol(
full_type_name, anon_data.is_union, anon_data.members,
anon_data.lineno, file_path
)
return loaded