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

Commit

Permalink
Uses of symbol are connected to NameAtom for most needed places elege…
Browse files Browse the repository at this point in the history
…ntly
  • Loading branch information
kugesan1105 committed Aug 30, 2024
1 parent 9da5f82 commit 89096c2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 82 deletions.
29 changes: 3 additions & 26 deletions jaclang/compiler/passes/main/fuse_typeinfo_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,31 +510,6 @@ def exit_atom_trailer(self, node: ast.AtomTrailer) -> None:
): # TODO check why IndexSlice produce an issue
right.name_spec.sym = left.type_sym_tab.lookup(right.sym_name)

def find_tab(tab: ast.SymbolTable, name: str) -> Optional[ast.SymbolTable]:
"""Recursively find symbol table from sym dotted name."""
tab_list = name.split(".")
if len(tab_list) == 1:
return tab
else:
if tab_list[0] == tab.name:
return find_tab(tab, ".".join(tab_list[1:]))
else:
for child in tab.kid:
if child.name == tab_list[0]:
return find_tab(child, ".".join(tab_list[1:]))
return None

for name_node in node.as_attr_list:
tab = (
find_tab(self.ir.sym_tab, name_node.sym.sym_dotted_name)
if name_node.sym
else None
)
if tab:
find_symbol = tab.lookup(name_node.sym_name)
if find_symbol and name_node not in find_symbol.uses:
find_symbol.add_use(name_node.name_spec)

# This function originally used `as_attr_list` in AtomTrailer
# but an issue happened when doing stuff like fool_me().CONST_VALUE2
# The issue was due to the way `as_attr_list` implemented so the fix
Expand All @@ -549,7 +524,7 @@ def find_tab(tab: ast.SymbolTable, name: str) -> Optional[ast.SymbolTable]:
iteration_count += 1
if iteration_count > 50:
break

# print(1234,'atom_trailer_unwind',atom_trailer_unwind)
for i in range(1, len(atom_trailer_unwind)):
left = atom_trailer_unwind[i - 1]
right = atom_trailer_unwind[i]
Expand Down Expand Up @@ -597,3 +572,5 @@ def find_tab(tab: ast.SymbolTable, name: str) -> Optional[ast.SymbolTable]:
else:
if left.type_sym_tab:
right.name_spec.sym = left.type_sym_tab.lookup(right.sym_name)
if right.name_spec.sym:
right.name_spec.sym.add_use(right.name_spec)
9 changes: 9 additions & 0 deletions jaclang/compiler/passes/main/pyjac_ast_link_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import jaclang.compiler.absyntree as ast
from jaclang.compiler.passes import Pass
from jaclang.compiler.symtable import Symbol


class PyJacAstLinkPass(Pass):
Expand Down Expand Up @@ -161,6 +162,14 @@ def exit_ability_def(self, node: ast.AbilityDef) -> None:
jac_node=node.signature.return_type,
py_nodes=node.parent.signature.return_type.gen.py_ast,
)
"""Enter ability definition."""

if isinstance(node.decl_link, ast.Ability) and isinstance(
node.target, ast.ArchRefChain
):
for arch in node.target.archs:
if arch.arch_name.sym and isinstance(arch.arch_name.sym, Symbol):
arch.arch_name.sym.add_use(arch.arch_name)

def exit_param_var(self, node: ast.ParamVar) -> None:
"""Sub objects.
Expand Down
26 changes: 18 additions & 8 deletions jaclang/langserve/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
from jaclang.compiler.passes import Pass
from jaclang.compiler.passes.main.schedules import py_code_gen_typed
from jaclang.compiler.passes.tool import FuseCommentsPass, JacFormatPass
from jaclang.compiler.symtable import Symbol
from jaclang.langserve.sem_manager import SemTokManager
from jaclang.langserve.utils import (
add_unique_text_edit,
collect_all_symbols_in_scope,
collect_impl_nodes,
collect_linkage_symbol,
create_range,
find_deepest_symbol_node_at_pos,
find_index,
Expand Down Expand Up @@ -366,7 +365,17 @@ def get_definition(
return None
elif isinstance(node_selected, (ast.ElementStmt, ast.BuiltinType)):
return None
decl_node = collect_linkage_symbol(node_selected)
decl_node = (
node_selected.parent.body.target
if node_selected.parent
and isinstance(node_selected.parent, ast.AstImplNeedingNode)
and isinstance(node_selected.parent.body, ast.AstImplOnlyNode)
else (
node_selected.sym.decl
if (node_selected.sym and node_selected.sym.decl)
else node_selected
)
)
decl_uri = uris.from_fs_path(decl_node.loc.mod_path)
try:
decl_range = create_range(decl_node.loc)
Expand Down Expand Up @@ -420,14 +429,15 @@ def rename_symbol(
if index1 is None:
return None
node_selected = self.modules[file_path].sem_manager.static_sem_tokens[index1][3]
if node_selected and node_selected.sym:
impl_node_uses, archi_name_uses = collect_impl_nodes(node_selected)
if (
node_selected
and node_selected.sym
and isinstance(node_selected.sym.decl.sym, Symbol)
):
changes: dict[str, list[lspt.TextEdit]] = {}
for node in [
*node_selected.sym.uses,
*node_selected.sym.decl.sym.uses,
node_selected.sym.defn[0],
*impl_node_uses,
*archi_name_uses,
]:
key = uris.from_fs_path(node.loc.mod_path)
new_edit = lspt.TextEdit(
Expand Down
2 changes: 1 addition & 1 deletion jaclang/langserve/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def test_rename_uses(self) -> None:
(4, 6, "func", "24:4-24:7", "0:4-0:7", "4:5-4:8",),
(24, 7, "func", "24:4-24:7", "0:4-0:7", "4:5-4:8",),
(10, 10, "canBar", "10:8-10:11", "15:13-15:16", ), # until merge fool me
(15, 15, "ubar", "15:13-15:16", "10:8-10:11", ), # until merge fool me
(15, 15, "ubar", "15:13-15:16", "10:8-10:11" ), # until merge fool me
# (10, 10, "canBar", "10:8-10:11", "14:13-14:16", "21....",), # True
# (14, 15, "ubar", "14:13-14:16", "10:8-10:11", "21......" ), # True
(9, 7, "result", "9:4-9:7", "25:4-25:7", "15:5-15:8", "15:5-15:8",),
Expand Down
47 changes: 0 additions & 47 deletions jaclang/langserve/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,50 +644,3 @@ def add_unique_text_edit(
):
return
changes[key].append(new_edit)


def collect_linkage_symbol(node: ast.AstSymbolNode) -> ast.AstNode:
"""Collect the symbol node that is linked to the symbol node."""
return (
node.parent.body.target
if node.parent
and isinstance(node.parent, ast.AstImplNeedingNode)
and isinstance(node.parent.body, ast.AstImplOnlyNode)
else (node.sym.decl if (node.sym and node.sym.decl) else node)
)


def collect_impl_nodes(
node: ast.AstSymbolNode,
) -> tuple[list[ast.AstNode], list[ast.NameAtom]]:
"""Collect nodes that are linked to impl node."""
if not node.sym:
return [], []
linkage_node = collect_linkage_symbol(node)
decl_linkage_node = collect_linkage_symbol(node.sym.decl)
symbol_type = node.sym.sym_type
referenced_nodes = []
if symbol_type == SymbolType.OBJECT_ARCH and isinstance(
node.sym.decl.sym_tab.owner, ast.Architype
):
archi_node: ast.Architype = node.sym.decl.sym_tab.owner
if archi_node.body and isinstance(archi_node.body, ast.SubNodeList):
for i in archi_node.body.items:
if (
isinstance(i, ast.Ability)
and i.body
and isinstance(i.body, ast.AstImplOnlyNode)
):
referenced_nodes.append(i.body.target.archs[0].arch_name)

decls = [linkage_node, decl_linkage_node]
for i, decl in enumerate(decls):
if isinstance(decl, ast.ArchRefChain):
if len(decl.archs) > 1:
is_ability = symbol_type == SymbolType.ABILITY
decls[i] = (
decl.archs[1].arch_name if is_ability else decl.archs[0].arch_name
)
else:
decls[i] = decl.archs[0].arch_name
return decls, referenced_nodes

0 comments on commit 89096c2

Please sign in to comment.