Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[tool]: separate import resolution pass #4229

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vyper/compiler/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def build_archive_b64(compiler_data: CompilerData) -> str:


def build_integrity(compiler_data: CompilerData) -> str:
return compiler_data.compilation_target._metadata["type"].integrity_sum
return compiler_data.resolved_imports.integrity_sum


def build_external_interface_output(compiler_data: CompilerData) -> str:
Expand Down
4 changes: 2 additions & 2 deletions vyper/compiler/output_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from vyper.compiler.phases import CompilerData
from vyper.compiler.settings import Settings
from vyper.exceptions import CompilerPanic
from vyper.semantics.analysis.module import _is_builtin
from vyper.semantics.analysis.imports import _is_builtin
from vyper.utils import get_long_version

# data structures and routines for constructing "output bundles",
Expand Down Expand Up @@ -159,7 +159,7 @@ def write(self):
self.write_compilation_target([self.bundle.compilation_target_path])
self.write_search_paths(self.bundle.used_search_paths)
self.write_settings(self.compiler_data.original_settings)
self.write_integrity(self.bundle.compilation_target.integrity_sum)
self.write_integrity(self.compiler_data.resolved_imports.integrity_sum)
self.write_sources(self.bundle.compiler_inputs)


Expand Down
36 changes: 13 additions & 23 deletions vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from vyper.ir import compile_ir, optimizer
from vyper.semantics import analyze_module, set_data_positions, validate_compilation_target
from vyper.semantics.analysis.data_positions import generate_layout_export
from vyper.semantics.analysis.imports import resolve_imports
from vyper.semantics.types.function import ContractFunctionT
from vyper.semantics.types.module import ModuleT
from vyper.typing import StorageLayout
Expand Down Expand Up @@ -145,9 +146,20 @@ def vyper_module(self):
_, ast = self._generate_ast
return ast

@cached_property
def _resolve_imports(self):
vyper_module = copy.deepcopy(self.vyper_module)
cyberthirst marked this conversation as resolved.
Show resolved Hide resolved
with self.input_bundle.search_path(Path(vyper_module.resolved_path).parent):
return vyper_module, resolve_imports(vyper_module, self.input_bundle)

@cached_property
def resolved_imports(self):
return self._resolve_imports[1]

@cached_property
def _annotate(self) -> tuple[natspec.NatspecOutput, vy_ast.Module]:
module = generate_annotated_ast(self.vyper_module, self.input_bundle)
module = self._resolve_imports[0]
analyze_module(module)
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
nspec = natspec.parse_natspec(module)
return nspec, module

Expand Down Expand Up @@ -267,28 +279,6 @@ def blueprint_bytecode(self) -> bytes:
return deploy_bytecode + blueprint_bytecode


def generate_annotated_ast(vyper_module: vy_ast.Module, input_bundle: InputBundle) -> vy_ast.Module:
"""
Validates and annotates the Vyper AST.

Arguments
---------
vyper_module : vy_ast.Module
Top-level Vyper AST node

Returns
-------
vy_ast.Module
Annotated Vyper AST
"""
vyper_module = copy.deepcopy(vyper_module)
with input_bundle.search_path(Path(vyper_module.resolved_path).parent):
# note: analyze_module does type inference on the AST
analyze_module(vyper_module, input_bundle)

return vyper_module


def generate_ir_nodes(global_ctx: ModuleT, settings: Settings) -> tuple[IRnode, IRnode]:
"""
Generate the intermediate representation (IR) from the contextualized AST.
Expand Down
16 changes: 11 additions & 5 deletions vyper/semantics/analysis/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import enum
from dataclasses import dataclass
from functools import cached_property
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional

from vyper import ast as vy_ast
from vyper.compiler.input_bundle import CompilerInput
Expand All @@ -13,7 +13,7 @@

if TYPE_CHECKING:
from vyper.semantics.types.function import ContractFunctionT
from vyper.semantics.types.module import InterfaceT, ModuleT
from vyper.semantics.types.module import ModuleT


class FunctionVisibility(StringEnum):
Expand Down Expand Up @@ -119,13 +119,19 @@ def __hash__(self):
return hash(id(self.module_t))


@dataclass(frozen=True)
@dataclass
class ImportInfo(AnalysisResult):
typ: Union[ModuleInfo, "InterfaceT"]
alias: str # the name in the namespace
qualified_module_name: str # for error messages
compiler_input: CompilerInput # to recover file info for ast export
node: vy_ast.VyperNode
parsed: Any # (json) abi | AST
_typ: Any = None # type to be filled in during analysis

@property
def typ(self):
if self._typ is None: # pragma: nocover
raise CompilerPanic("unreachable!")
return self._typ

def to_dict(self):
ret = {"alias": self.alias, "qualified_module_name": self.qualified_module_name}
Expand Down
37 changes: 0 additions & 37 deletions vyper/semantics/analysis/import_graph.py

This file was deleted.

Loading
Loading