-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move validation within IR, and minor refactoring thereof
- Loading branch information
Showing
7 changed files
with
80 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
src/puya/ir_validation/main.py → src/puya/ir/validation/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from puya.context import CompileContext | ||
from puya.ir.models import ModuleArtifact | ||
from puya.ir_validation.min_avm_version_validator import MinAvmVersionValidator | ||
from puya.ir_validation.op_run_mode_validator import OpRunModeValidator | ||
from puya.ir.validation.min_avm_version_validator import MinAvmVersionValidator | ||
from puya.ir.validation.op_run_mode_validator import OpRunModeValidator | ||
|
||
|
||
def validate_module_artifact(context: CompileContext, artifact: ModuleArtifact) -> None: | ||
OpRunModeValidator.validate(context, artifact) | ||
OpRunModeValidator.validate(artifact) | ||
MinAvmVersionValidator.validate(context, artifact) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import typing | ||
|
||
import structlog | ||
|
||
from puya.ir.avm_ops_models import RunMode | ||
from puya.ir.models import Contract, Intrinsic, LogicSignature, ModuleArtifact | ||
from puya.ir.visitor import IRTraverser | ||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
class OpRunModeValidator(IRTraverser): | ||
def __init__(self, run_mode: typing.Literal[RunMode.app, RunMode.lsig]) -> None: | ||
self._current_run_mode = run_mode | ||
|
||
@classmethod | ||
def validate(cls, artifact: ModuleArtifact) -> None: | ||
match artifact: | ||
case LogicSignature() as l_sig: | ||
cls.validate_logic_sig(l_sig) | ||
case Contract() as contract: | ||
cls.validate_contract(contract) | ||
case _: | ||
typing.assert_never(artifact) | ||
|
||
@classmethod | ||
def validate_logic_sig(cls, logic_sig: LogicSignature) -> None: | ||
validator = cls(RunMode.lsig) | ||
for sub in logic_sig.program.all_subroutines: | ||
validator.visit_all_blocks(sub.body) | ||
|
||
@classmethod | ||
def validate_contract(cls, contract: Contract) -> None: | ||
validator = cls(RunMode.app) | ||
subs = (sub for program in contract.all_programs() for sub in program.all_subroutines) | ||
for sub in subs: | ||
validator.visit_all_blocks(sub.body) | ||
|
||
def visit_intrinsic_op(self, intrinsic: Intrinsic) -> None: | ||
match intrinsic.op_variant.supported_modes: | ||
case RunMode.any: | ||
pass | ||
case RunMode.app: | ||
if self._current_run_mode != RunMode.app: | ||
logger.warning( | ||
f"The operation {intrinsic} is only allowed in smart contracts", | ||
location=intrinsic.source_location, | ||
) | ||
case RunMode.lsig: | ||
if self._current_run_mode != RunMode.lsig: | ||
logger.warning( | ||
f"The operation {intrinsic} is only allowed in logic signatures", | ||
location=intrinsic.source_location, | ||
) | ||
case _: | ||
typing.assert_never(intrinsic.op_variant.supported_modes) |
This file was deleted.
Oops, something went wrong.