-
-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement "stateless" modules (#3663)
this commit implements support for "stateless" modules in vyper. this is the first major step in implementing vyper's module system. it redesigns the language's import system, allows calling internal functions from imported modules, allows for limited use of types from imported modules, and introduces support for `.vyi` interface files. note that the following features are left for future, follow-up work: - modules with variables (constants, immutables or storage variables) - full support for imported events (in that they do not get exported in the ABI) - a system for exporting imported functions in the external interface of a contract this commit first and foremost changes how imports are handled in vyper. previously, an imported file was assumed to be an interface file. some very limited validation was performed in `InterfaceT.from_ast`, but not fully typechecked, and no code was generated for it. now, when a file is imported, it is checked whether it is 1. a `.vy` file 2. a `.vyi` file 3. a `.json` file the `.json` pathway remains more or less unchanged. the `.vyi` pathway is new, but it is fairly straightforward and is basically a "simple" path through the `.vy` pathway which piggy-backs off the `.vy` analysis to produce an `InterfaceT` object. the `.vy` pathway now does full typechecking and analysis of the imported module. some changes were made to support this: - a new ImportGraph data structure tracks the position in the import graph and detects (and bands) import cycles - InputBundles now implement a `_normalize_path()` method. this method normalizes the path so that source IDs are stable no matter how a file is accessed in the filesystem (i.e., no matter what the search path was at the time `load_file()` was called). - CompilerInput now has a distinction between `resolved_path` and `path` (the original path that was asked for). this allows us to maintain UX considerations (showing unresolved paths etc) while still having a 1:1:1 correspondence between source id, filepath and filesystem. these changes were needed in order to stabilize notions like "which file are we looking at?" no matter the way the file was accessed or how it was imported. this is important so that types imported transitively can resolve as expected no matter how they are imported - for instance, `x.SomeType` and `a.x.SomeType` resolving to the same type. the other changes needed to support code generation and analysis for imported functions were fairly simple, and mostly involved generalizing the analysis/code generation to type-based dispatch instead of AST-based dispatch. other changes to the language and compiler API include: - import restrictions are more relaxed - `import x` is allowed now (previously, `import x as x` was required) - change function labels in IR function labels are changed to disambiguate functions of the same name (but whose parent module are different). this was done by computing a unique function_id for every function and using that function_id when constructing its IR identifier. - add compile_from_file_input which accepts a FileInput instead of a string. this is now the new preferred entry point into the compiler. its usage simplifies several internal APIs which expect to have `source_id` and `path` in addition to the raw source code. - change compile_code api from contract_name= to contract_path= additional changes to internal APIs and passes include: - remove `remove_unused_statements()` the "unused statements" are now important to keep around for imports! in general, it is planned to remove both the AST expansion and constant folding passes as copying around the AST results in both performance and correctness problems - abstract out a common exception rewriting pattern. instead of raising `exception.with_annotation(node)` -- just catch-all in the parent implementation and then don't have to worry about it at the exception site. - rename "type" metadata key on most top-level declarators to more specific names (e.g. "func_type", "getter_type", etc). - remove dead package pytest-rerunfailures use of `--reruns` was removed in c913b2d - refactor: move `parse_*` functions, remove vyper.ast.annotation move `parse_*` functions into new module vyper.ast.parse and merge in vyper.ast.annotation - rename the old `GlobalContext` class to `ModuleT` - refactor: move InterfaceT into `vyper/semantics/types/module.py` it makes more sense here since it is closely coupled with `ModuleT`.
- Loading branch information
1 parent
919080e
commit c6f457a
Showing
75 changed files
with
2,546 additions
and
1,397 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
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
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
Oops, something went wrong.