Skip to content

Commit

Permalink
Fixing an issue when the symbol is in builtins or symbol is not imported
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtm98 committed Dec 14, 2024
1 parent 16f1a61 commit 8b0ba86
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
60 changes: 55 additions & 5 deletions jac/jaclang/compiler/passes/main/inheritance_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@

from __future__ import annotations

from typing import Optional

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


class InheritancePass(Pass):
"""Add inherited abilities in the target symbol tables."""

def __debug_print(self, msg: str) -> None:
if settings.inherit_pass_debug:
self.log_info("[PyImportPass] " + msg)

def __lookup(self, name: str, sym_table: SymbolTable) -> Optional[Symbol]:
symbol = sym_table.lookup(name)
if symbol is None:
# Check if the needed symbol in builtins
builtins_symtable = self.ir.sym_tab.find_scope("builtins")
assert builtins_symtable is not None
symbol = builtins_symtable.lookup(name)
return symbol

def enter_architype(self, node: ast.Architype) -> None:
"""Fill architype symbol tables with abilities from parent architypes."""
if node.base_classes is None:
Expand All @@ -24,23 +41,44 @@ def enter_architype(self, node: ast.Architype) -> None:
# symbol to get the symbol table of the base class
if isinstance(item, ast.Name):
assert node.sym_tab.parent is not None
base_class_symbol = node.sym_tab.parent.lookup(item.sym_name)
base_class_symbol = self.__lookup(item.sym_name, node.sym_tab.parent)
if base_class_symbol is None:
msg = "Missing symbol for base class "
msg += f"{ast.Module.get_href_path(item)}.{item.sym_name}"
msg += f" needed for {ast.Module.get_href_path(node)}"
self.__debug_print(msg)
continue
base_class_symbol_table = base_class_symbol.fetch_sym_tab
if (
base_class_symbol_table is None
and base_class_symbol.defn[0]
.parent_of_type(ast.Module)
.py_info.is_raised_from_py
):
msg = "Missing symbol table for python base class "
msg += f"{ast.Module.get_href_path(item)}.{item.sym_name}"
msg += f" needed for {ast.Module.get_href_path(node)}"
self.__debug_print(msg)
continue
assert base_class_symbol_table is not None
node.sym_tab.inherit_sym_tab(base_class_symbol_table)

# In case of atom trailer, unwind it and use each name node to
# as the code above to lookup for the base class
elif isinstance(item, ast.AtomTrailer):
current_sym_table = node.sym_tab.parent
not_found: bool = False
assert current_sym_table is not None
for name in item.as_attr_list:
sym = current_sym_table.lookup(name.sym_name)
assert sym is not None
sym = self.__lookup(name.sym_name, current_sym_table)
if sym is None:
msg = "Missing symbol for base class "
msg += f"{ast.Module.get_href_path(name)}.{name.sym_name}"
msg += f" needed for {ast.Module.get_href_path(node)}"
self.__debug_print(msg)
not_found = True
break
current_sym_table = sym.fetch_sym_tab
assert current_sym_table is not None

# In case of python nodes, the base class may not be
# raised so ignore these classes for now
Expand All @@ -49,5 +87,17 @@ def enter_architype(self, node: ast.Architype) -> None:
sym.defn[0].parent_of_type(ast.Module).py_info.is_raised_from_py
and current_sym_table is None
):
return
msg = "Missing symbol table for python base class "
msg += f"{ast.Module.get_href_path(name)}.{name.sym_name}"
msg += f" needed for {ast.Module.get_href_path(node)}"
self.__debug_print(msg)
not_found = True
break

assert current_sym_table is not None

if not_found:
continue

assert current_sym_table is not None
node.sym_tab.inherit_sym_tab(current_sym_table)
1 change: 1 addition & 0 deletions jac/jaclang/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Settings:
collect_py_dep_debug: bool = False
print_py_raised_ast: bool = False
py_import_pass_debug: bool = False
inherit_pass_debug: bool = False

# Compiler configuration
disable_mtllm: bool = False
Expand Down

0 comments on commit 8b0ba86

Please sign in to comment.