2022-01-24 04:07:52 +00:00
|
|
|
import typing as t
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from . import nodes
|
|
|
|
from .visitor import NodeVisitor
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
VAR_LOAD_PARAMETER = "param"
|
|
|
|
VAR_LOAD_RESOLVE = "resolve"
|
|
|
|
VAR_LOAD_ALIAS = "alias"
|
|
|
|
VAR_LOAD_UNDEFINED = "undefined"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def find_symbols(
|
|
|
|
nodes: t.Iterable[nodes.Node], parent_symbols: t.Optional["Symbols"] = None
|
|
|
|
) -> "Symbols":
|
2019-12-02 21:46:54 +00:00
|
|
|
sym = Symbols(parent=parent_symbols)
|
|
|
|
visitor = FrameSymbolVisitor(sym)
|
|
|
|
for node in nodes:
|
|
|
|
visitor.visit(node)
|
|
|
|
return sym
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def symbols_for_node(
|
|
|
|
node: nodes.Node, parent_symbols: t.Optional["Symbols"] = None
|
|
|
|
) -> "Symbols":
|
2019-12-02 21:46:54 +00:00
|
|
|
sym = Symbols(parent=parent_symbols)
|
|
|
|
sym.analyze_node(node)
|
|
|
|
return sym
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class Symbols:
|
|
|
|
def __init__(
|
|
|
|
self, parent: t.Optional["Symbols"] = None, level: t.Optional[int] = None
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
if level is None:
|
|
|
|
if parent is None:
|
|
|
|
level = 0
|
|
|
|
else:
|
|
|
|
level = parent.level + 1
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
self.level: int = level
|
2019-12-02 21:46:54 +00:00
|
|
|
self.parent = parent
|
2022-01-24 04:07:52 +00:00
|
|
|
self.refs: t.Dict[str, str] = {}
|
|
|
|
self.loads: t.Dict[str, t.Any] = {}
|
|
|
|
self.stores: t.Set[str] = set()
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def analyze_node(self, node: nodes.Node, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
visitor = RootVisitor(self)
|
|
|
|
visitor.visit(node, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _define_ref(
|
|
|
|
self, name: str, load: t.Optional[t.Tuple[str, t.Optional[str]]] = None
|
|
|
|
) -> str:
|
|
|
|
ident = f"l_{self.level}_{name}"
|
2019-12-02 21:46:54 +00:00
|
|
|
self.refs[name] = ident
|
|
|
|
if load is not None:
|
|
|
|
self.loads[ident] = load
|
|
|
|
return ident
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def find_load(self, target: str) -> t.Optional[t.Any]:
|
2019-12-02 21:46:54 +00:00
|
|
|
if target in self.loads:
|
|
|
|
return self.loads[target]
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.parent is not None:
|
|
|
|
return self.parent.find_load(target)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def find_ref(self, name: str) -> t.Optional[str]:
|
2019-12-02 21:46:54 +00:00
|
|
|
if name in self.refs:
|
|
|
|
return self.refs[name]
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
if self.parent is not None:
|
|
|
|
return self.parent.find_ref(name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def ref(self, name: str) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
rv = self.find_ref(name)
|
|
|
|
if rv is None:
|
2022-01-24 04:07:52 +00:00
|
|
|
raise AssertionError(
|
|
|
|
"Tried to resolve a name to a reference that was"
|
|
|
|
f" unknown to the frame ({name!r})"
|
|
|
|
)
|
2019-12-02 21:46:54 +00:00
|
|
|
return rv
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def copy(self) -> "Symbols":
|
2022-11-07 18:06:49 +00:00
|
|
|
rv = object.__new__(self.__class__)
|
2019-12-02 21:46:54 +00:00
|
|
|
rv.__dict__.update(self.__dict__)
|
|
|
|
rv.refs = self.refs.copy()
|
|
|
|
rv.loads = self.loads.copy()
|
|
|
|
rv.stores = self.stores.copy()
|
|
|
|
return rv
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def store(self, name: str) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.stores.add(name)
|
|
|
|
|
|
|
|
# If we have not see the name referenced yet, we need to figure
|
|
|
|
# out what to set it to.
|
|
|
|
if name not in self.refs:
|
|
|
|
# If there is a parent scope we check if the name has a
|
|
|
|
# reference there. If it does it means we might have to alias
|
|
|
|
# to a variable there.
|
|
|
|
if self.parent is not None:
|
|
|
|
outer_ref = self.parent.find_ref(name)
|
|
|
|
if outer_ref is not None:
|
|
|
|
self._define_ref(name, load=(VAR_LOAD_ALIAS, outer_ref))
|
|
|
|
return
|
|
|
|
|
|
|
|
# Otherwise we can just set it to undefined.
|
|
|
|
self._define_ref(name, load=(VAR_LOAD_UNDEFINED, None))
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def declare_parameter(self, name: str) -> str:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.stores.add(name)
|
|
|
|
return self._define_ref(name, load=(VAR_LOAD_PARAMETER, None))
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def load(self, name: str) -> None:
|
|
|
|
if self.find_ref(name) is None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self._define_ref(name, load=(VAR_LOAD_RESOLVE, name))
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def branch_update(self, branch_symbols: t.Sequence["Symbols"]) -> None:
|
|
|
|
stores: t.Dict[str, int] = {}
|
2019-12-02 21:46:54 +00:00
|
|
|
for branch in branch_symbols:
|
|
|
|
for target in branch.stores:
|
|
|
|
if target in self.stores:
|
|
|
|
continue
|
|
|
|
stores[target] = stores.get(target, 0) + 1
|
|
|
|
|
|
|
|
for sym in branch_symbols:
|
|
|
|
self.refs.update(sym.refs)
|
|
|
|
self.loads.update(sym.loads)
|
|
|
|
self.stores.update(sym.stores)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
for name, branch_count in stores.items():
|
2019-12-02 21:46:54 +00:00
|
|
|
if branch_count == len(branch_symbols):
|
|
|
|
continue
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
target = self.find_ref(name) # type: ignore
|
|
|
|
assert target is not None, "should not happen"
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
if self.parent is not None:
|
|
|
|
outer_target = self.parent.find_ref(name)
|
|
|
|
if outer_target is not None:
|
|
|
|
self.loads[target] = (VAR_LOAD_ALIAS, outer_target)
|
|
|
|
continue
|
|
|
|
self.loads[target] = (VAR_LOAD_RESOLVE, name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def dump_stores(self) -> t.Dict[str, str]:
|
|
|
|
rv: t.Dict[str, str] = {}
|
|
|
|
node: t.Optional["Symbols"] = self
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
while node is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
for name in sorted(node.stores):
|
2019-12-02 21:46:54 +00:00
|
|
|
if name not in rv:
|
2022-01-24 04:07:52 +00:00
|
|
|
rv[name] = self.find_ref(name) # type: ignore
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
node = node.parent
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
return rv
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def dump_param_targets(self) -> t.Set[str]:
|
2019-12-02 21:46:54 +00:00
|
|
|
rv = set()
|
2022-01-24 04:07:52 +00:00
|
|
|
node: t.Optional["Symbols"] = self
|
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
while node is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
for target, (instr, _) in self.loads.items():
|
2019-12-02 21:46:54 +00:00
|
|
|
if instr == VAR_LOAD_PARAMETER:
|
|
|
|
rv.add(target)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
node = node.parent
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
|
|
class RootVisitor(NodeVisitor):
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self, symbols: "Symbols") -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.sym_visitor = FrameSymbolVisitor(symbols)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _simple_visit(self, node: nodes.Node, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for child in node.iter_child_nodes():
|
|
|
|
self.sym_visitor.visit(child)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
visit_Template = _simple_visit
|
|
|
|
visit_Block = _simple_visit
|
|
|
|
visit_Macro = _simple_visit
|
|
|
|
visit_FilterBlock = _simple_visit
|
|
|
|
visit_Scope = _simple_visit
|
|
|
|
visit_If = _simple_visit
|
|
|
|
visit_ScopedEvalContextModifier = _simple_visit
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for child in node.body:
|
|
|
|
self.sym_visitor.visit(child)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:
|
|
|
|
for child in node.iter_child_nodes(exclude=("call",)):
|
2019-12-02 21:46:54 +00:00
|
|
|
self.sym_visitor.visit(child)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for child in node.body:
|
|
|
|
self.sym_visitor.visit(child)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_For(
|
|
|
|
self, node: nodes.For, for_branch: str = "body", **kwargs: t.Any
|
|
|
|
) -> None:
|
|
|
|
if for_branch == "body":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.sym_visitor.visit(node.target, store_as_param=True)
|
|
|
|
branch = node.body
|
2022-01-24 04:07:52 +00:00
|
|
|
elif for_branch == "else":
|
2019-12-02 21:46:54 +00:00
|
|
|
branch = node.else_
|
2022-01-24 04:07:52 +00:00
|
|
|
elif for_branch == "test":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.sym_visitor.visit(node.target, store_as_param=True)
|
|
|
|
if node.test is not None:
|
|
|
|
self.sym_visitor.visit(node.test)
|
|
|
|
return
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
raise RuntimeError("Unknown for branch")
|
|
|
|
|
|
|
|
if branch:
|
|
|
|
for item in branch:
|
|
|
|
self.sym_visitor.visit(item)
|
2019-12-02 21:46:54 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for target in node.targets:
|
|
|
|
self.sym_visitor.visit(target)
|
|
|
|
for child in node.body:
|
|
|
|
self.sym_visitor.visit(child)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def generic_visit(self, node: nodes.Node, *args: t.Any, **kwargs: t.Any) -> None:
|
|
|
|
raise NotImplementedError(f"Cannot find symbols for {type(node).__name__!r}")
|
2019-12-02 21:46:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FrameSymbolVisitor(NodeVisitor):
|
|
|
|
"""A visitor for `Frame.inspect`."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self, symbols: "Symbols") -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols = symbols
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Name(
|
|
|
|
self, node: nodes.Name, store_as_param: bool = False, **kwargs: t.Any
|
|
|
|
) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""All assignments to names go through this function."""
|
2022-01-24 04:07:52 +00:00
|
|
|
if store_as_param or node.ctx == "param":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols.declare_parameter(node.name)
|
2022-01-24 04:07:52 +00:00
|
|
|
elif node.ctx == "store":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols.store(node.name)
|
2022-01-24 04:07:52 +00:00
|
|
|
elif node.ctx == "load":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols.load(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_NSRef(self, node: nodes.NSRef, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols.load(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_If(self, node: nodes.If, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.test, **kwargs)
|
|
|
|
original_symbols = self.symbols
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def inner_visit(nodes: t.Iterable[nodes.Node]) -> "Symbols":
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols = rv = original_symbols.copy()
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
for subnode in nodes:
|
|
|
|
self.visit(subnode, **kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols = original_symbols
|
|
|
|
return rv
|
|
|
|
|
|
|
|
body_symbols = inner_visit(node.body)
|
|
|
|
elif_symbols = inner_visit(node.elif_)
|
|
|
|
else_symbols = inner_visit(node.else_ or ())
|
|
|
|
self.symbols.branch_update([body_symbols, elif_symbols, else_symbols])
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Macro(self, node: nodes.Macro, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.symbols.store(node.name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Import(self, node: nodes.Import, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.generic_visit(node, **kwargs)
|
|
|
|
self.symbols.store(node.target)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_FromImport(self, node: nodes.FromImport, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.generic_visit(node, **kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2019-12-02 21:46:54 +00:00
|
|
|
for name in node.names:
|
|
|
|
if isinstance(name, tuple):
|
|
|
|
self.symbols.store(name[1])
|
|
|
|
else:
|
|
|
|
self.symbols.store(name)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Assign(self, node: nodes.Assign, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Visit assignments in the correct order."""
|
|
|
|
self.visit(node.node, **kwargs)
|
|
|
|
self.visit(node.target, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_For(self, node: nodes.For, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Visiting stops at for blocks. However the block sequence
|
|
|
|
is visited as part of the outer scope.
|
|
|
|
"""
|
|
|
|
self.visit(node.iter, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.call, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_FilterBlock(self, node: nodes.FilterBlock, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
self.visit(node.filter, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
for target in node.values:
|
|
|
|
self.visit(target)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Stop visiting at block assigns."""
|
|
|
|
self.visit(node.target, **kwargs)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Scope(self, node: nodes.Scope, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Stop visiting at scopes."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_Block(self, node: nodes.Block, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Stop visiting at blocks."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:
|
2019-12-02 21:46:54 +00:00
|
|
|
"""Do not visit into overlay scopes."""
|