Skip to content

Commit

Permalink
#2585 Add an error_if_not_found: bool argument to `SymbolTable().lo…
Browse files Browse the repository at this point in the history
…okup`.
  • Loading branch information
JulienRemy committed Jun 21, 2024
1 parent 1e94cd6 commit f03f8dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/psyclone/psyir/symbols/symbol_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,13 @@ def specify_argument_list(self, argument_symbols):
self._validate_arg_list(argument_symbols)
self._argument_list = argument_symbols[:]

def lookup(self, name, visibility=None, scope_limit=None):
'''Look up a symbol in the symbol table. The lookup can be limited
by visibility (e.g. just show public methods) or by scope_limit (e.g.
def lookup(self, name, visibility=None, scope_limit=None,
error_if_not_found=True):
'''Look up a symbol in the symbol table. The lookup can be limited \
by visibility (e.g. just show public methods) or by scope_limit (e.g. \
just show symbols up to a certain scope).
Returns None if error_if_not_found is True and no symbol with this \
name exists in the symbol table.
:param str name: name of the symbol.
:param visibilty: the visibility or list of visibilities that the \
Expand All @@ -949,15 +952,22 @@ def lookup(self, name, visibility=None, scope_limit=None):
searched.
:type scope_limit: :py:class:`psyclone.psyir.nodes.Node` or \
`NoneType`
:param error_if_not_found: whether to raise an error if a symbol \
with this name cannot be found. \
Defaults to True.
:type error_if_not_found: Optional[bool]
:returns: the symbol with the given name and, if specified, visibility.
:rtype: :py:class:`psyclone.psyir.symbols.Symbol`
:returns: the symbol with the given name and, if specified, \
visibility, or None if the symbol cannot be found and \
error_if_not_found is True.
:rtype: Union[:py:class:`psyclone.psyir.symbols.Symbol`, None]
:raises TypeError: if the name argument is not a string.
:raises SymbolError: if the name exists in the Symbol Table but does \
not have the specified visibility.
:raises TypeError: if the visibility argument has the wrong type.
:raises KeyError: if the given name is not in the Symbol Table.
:raises KeyError: if the given name is not in the Symbol Table and \
error_if_not_found is True.
'''
if not isinstance(name, str):
Expand Down Expand Up @@ -991,8 +1001,11 @@ def lookup(self, name, visibility=None, scope_limit=None):
f" match with the requested visibility: {vis_names}")
return symbol
except KeyError as err:
raise KeyError(f"Could not find '{name}' in the Symbol Table.") \
from err
if error_if_not_found:
raise KeyError(f"Could not find '{name}' in the Symbol "
f"Table.") from err

return None

def lookup_with_tag(self, tag, scope_limit=None):
'''Look up a symbol by its tag. The lookup can be limited by
Expand Down
2 changes: 2 additions & 0 deletions src/psyclone/tests/psyir/symbols/symbol_table_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,8 @@ def test_lookup_1():
assert "Could not find 'notdeclared' in the Symbol Table." in \
str(error.value)

assert sym_table.lookup("notdeclared", error_if_not_found=False) is None


def test_lookup_2():
'''Test the visibility argument filtering functionality of the
Expand Down

0 comments on commit f03f8dc

Please sign in to comment.