Skip to content

Commit

Permalink
wip - more threading, update add_import helper
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Oct 20, 2023
1 parent 5b22274 commit d8801c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 4 additions & 2 deletions vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class CompilerData:
def __init__(
self,
source_code: str,
input_bundle: InputBundle,
contract_name: str = "VyperContract",
source_id: int = 0,
settings: Settings = None,
Expand All @@ -76,6 +77,7 @@ def __init__(
Do not add metadata to bytecode. Defaults to False
"""
self.contract_name = contract_name
self.input_bundle = input_bundle
self.source_code = source_code
self.source_id = source_id
self.storage_layout_override = storage_layout
Expand Down Expand Up @@ -128,11 +130,11 @@ def vyper_module_unfolded(self) -> vy_ast.Module:
# This phase is intended to generate an AST for tooling use, and is not
# used in the compilation process.

return generate_unfolded_ast(self.vyper_module)
return generate_unfolded_ast(self.vyper_module, self.input_bundle)

@cached_property
def _folded_module(self):
return generate_folded_ast(self.vyper_module, self.storage_layout_override)
return generate_folded_ast(self.vyper_module, self.input_bundle, self.storage_layout_override)

@property
def vyper_module_folded(self) -> vy_ast.Module:
Expand Down
36 changes: 29 additions & 7 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import importlib
import pkgutil
from typing import Optional, Union
from pathlib import Path, PurePath

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'Path' is not used.

from vyper.compiler.input_bundle import CompilerInput, InputBundle
import vyper.builtins.interfaces
from vyper import ast as vy_ast
from vyper.evm.opcodes import version_check
Expand Down Expand Up @@ -31,14 +33,14 @@
from vyper.typing import InterfaceDict


def add_module_namespace(vy_module: vy_ast.Module, interface_codes: InterfaceDict) -> None:
def add_module_namespace(vy_module: vy_ast.Module, input_bundle: InputBundle) -> None:
"""
Analyze a Vyper module AST node, add all module-level objects to the
namespace and validate top-level correctness
"""

namespace = get_namespace()
ModuleAnalyzer(vy_module, interface_codes, namespace)
ModuleAnalyzer(vy_module, input_bundle, namespace)


def _find_cyclic_call(fn_names: list, self_members: dict) -> Optional[list]:
Expand All @@ -58,10 +60,10 @@ class ModuleAnalyzer(VyperNodeVisitorBase):
scope_name = "module"

def __init__(
self, module_node: vy_ast.Module, interface_codes: InterfaceDict, namespace: Namespace
self, module_node: vy_ast.Module, input_bundle: InputBundle, namespace: Namespace
) -> None:
self.ast = module_node
self.interface_codes = interface_codes or {}
self.input_bundle = input_bundle
self.namespace = namespace

# TODO: Move computation out of constructor
Expand Down Expand Up @@ -287,10 +289,13 @@ def visit_FunctionDef(self, node):
def visit_Import(self, node):
if not node.alias:
raise StructureException("Import requires an accompanying `as` statement", node)
self._add_import(node, ".", node.name, node.alias)
# import x.y[name] as y[alias]
file = self._resolve_import(0, node.name)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable file is not used.


def visit_ImportFrom(self, node):
self._add_import(node, node.module, node.name, node.alias or node.name)
# from m.n[module] import x[name] as y[alias]
file = self._resolve_import(node.level, node.module or "" + "." + node.name)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable file is not used.

def visit_InterfaceDef(self, node):
obj = InterfaceT.from_ast(node)
Expand All @@ -307,7 +312,7 @@ def visit_StructDef(self, node):
raise exc.with_annotation(node) from None

def _add_import(
self, node: Union[vy_ast.Import, vy_ast.ImportFrom], module: str, name: str, alias: str
self, level: int, module: str, name: str, alias: str
) -> None:
if module == "vyper.interfaces":
interface_codes = _get_builtin_interfaces()
Expand Down Expand Up @@ -335,6 +340,23 @@ def _add_import(
except VyperException as exc:
raise exc.with_annotation(node) from None

def _resolve_import(self, level: int, module: str) -> CompilerInput:
path = _import_to_path(level, module)
try:
return self.input_bundle.load_file(path.with_suffix(".vy"))
except FileNotFoundError:
return self.input_bundle.load_file(path.with_suffix(".json"))


# convert an import to a path (without suffix)
def _import_to_path(level: int, module: str) -> PurePath:
base_path = ""
if level > 1:
base_path = "../" * (level - 1)
elif level == 1:
base_path = "./"
return PurePath(f"{base_path}{module.replace('.','/')}/")


def _get_builtin_interfaces():
interface_names = [i.name for i in pkgutil.iter_modules(vyper.builtins.interfaces.__path__)]
Expand Down

0 comments on commit d8801c5

Please sign in to comment.