Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Refactor: Avoid Duplicate Symbols in SymbolTable #605

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion jaclang/compiler/passes/main/import_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from jaclang.compiler.passes import Pass
from jaclang.compiler.passes.main import SubNodeTabPass, SymTabBuildPass
from jaclang.utils.log import logging

from jaclang.compiler.constant import SymbolType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -287,6 +287,14 @@ def import_py_module(
self.import_table[file_to_raise] = mod
self.attach_mod_to_node(parent_node, mod)
SymTabBuildPass(input_ir=mod, prior=self)
parent_node.sym_tab.insert(
ast.AstSymbolNode(
sym_name=imported_mod_name,
name_spec=mod.kid[0].name_spec,
sym_category=SymbolType.MOD_VAR,
),
access_spec=None,
)
return mod
else:
raise self.ice(f"Failed to import python module {mod_path}")
Expand Down
17 changes: 16 additions & 1 deletion jaclang/compiler/passes/main/sym_tab_build_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,20 @@ def exit_import(self, node: ast.Import) -> None:
"""
if not node.is_absorb:
for i in node.items.items:
i.sym_tab.def_insert(i, single_decl="import item")
if not isinstance(i, ast.ModuleItem):
continue
if not i.from_mod_path.sub_module:
continue
lookup = i.from_mod_path.sub_module.sym_tab.lookup(i.name.value)
if lookup:
lookup.add_use(i.name_spec)
node.sym_tab.insert(lookup.decl, lookup.access)
else:
i.sym_tab.def_insert(i, single_decl="import item")

# if not node.is_absorb:
# for i in node.items.items:
# i.sym_tab.def_insert(i, single_decl="import item")
elif node.is_absorb and node.is_jac:
source = node.items.items[0]
if not isinstance(source, ast.ModulePath) or not source.sub_module:
Expand All @@ -206,6 +219,8 @@ def exit_import(self, node: ast.Import) -> None:
)
else:
node.sym_tab.inherit_sym_tab(source.sub_module.sym_tab)
else:
print("elseeeeeee")

def enter_module_path(self, node: ast.ModulePath) -> None:
"""Sub objects.
Expand Down
2 changes: 1 addition & 1 deletion jaclang/utils/treeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _build_symbol_tree_common(

if sym.decl and sym.decl.loc.first_line > 0:
SymbolTree(
node_name=f"decl: line {sym.decl.loc.first_line}, col {sym.decl.loc.col_start}",
node_name=f"decl: line {sym.decl.loc.first_line}, col {sym.decl.loc.col_start} {sym.decl.loc.mod_path}",
parent=symbol_node,
)
defn = SymbolTree(node_name="defn", parent=symbol_node)
Expand Down
Loading