diff --git a/vyper/ast/README.md b/vyper/ast/README.md index 9979b60cab..7400091993 100644 --- a/vyper/ast/README.md +++ b/vyper/ast/README.md @@ -12,8 +12,6 @@ and parsing NatSpec docstrings. * [`annotation.py`](annotation.py): Contains the `AnnotatingVisitor` class, used to annotate and modify the Python AST prior to converting it to a Vyper AST. -* [`folding.py`](folding.py): Functions for evaluating and replacing literal -nodes within the Vyper AST. * [`natspec.py`](natspec.py): Functions for parsing NatSpec docstrings within the source. * [`nodes.py`](nodes.py): Contains the Vyper node classes, and the `get_node` @@ -70,24 +68,6 @@ or parents that match a desired pattern. To learn more about these methods, read their docstrings in the `VyperNode` class in [`nodes.py`](nodes.py). -### Modifying the AST - -[`folding.py`](folding.py) contains the `fold` function, a high-level method called -to evaluating and replacing literal nodes within the AST. Some examples of literal -folding include: - -* arithmetic operations (`3+2` becomes `5`) -* references to literal arrays (`["foo", "bar"][1]` becomes `"bar"`) -* builtin functions applied to literals (`min(1,2)` becomes `1`) - -The process of literal folding includes: - -1. Foldable node classes are evaluated via their `fold` method, which attempts to create a new `Constant` from the content of the given node. -2. Replacement nodes are generated using the `from_node` class method within the new -node class. -3. The modification of the tree is handled by `Module.replace_in_tree`, which locates -the existing node and replaces it with a new one. - ## Design ### `__slots__` diff --git a/vyper/compiler/README.md b/vyper/compiler/README.md index eb70750a2b..abb8c6ee91 100644 --- a/vyper/compiler/README.md +++ b/vyper/compiler/README.md @@ -25,8 +25,6 @@ The compilation process includes the following broad phases: 1. In [`vyper.ast`](../ast), the source code is parsed and converted to an abstract syntax tree. -1. In [`vyper.ast.folding`](../ast/folding.py), literal Vyper AST nodes are -evaluated and replaced with the resulting values. 1. The [`GlobalContext`](../codegen/global_context.py) object is generated from the Vyper AST, analyzing and organizing the nodes prior to IR generation. 1. In [`vyper.codegen.module`](../codegen/module.py), the contextualized nodes are diff --git a/vyper/semantics/README.md b/vyper/semantics/README.md index 1d81a0979b..36519bba29 100644 --- a/vyper/semantics/README.md +++ b/vyper/semantics/README.md @@ -25,6 +25,7 @@ Vyper abstract syntax tree (AST). * [`data_positions`](analysis/data_positions.py): Functions for tracking storage variables and allocating storage slots * [`levenhtein_utils.py`](analysis/levenshtein_utils.py): Helper for better error messages * [`local.py`](analysis/local.py): Validates the local namespace of each function within a contract + * [`pre_typecheck.py`](analysis/pre_typecheck.py): Evaluate foldable nodes and populate their metadata with the replacement nodes. * [`module.py`](analysis/module.py): Validates the module namespace of a contract. * [`utils.py`](analysis/utils.py): Functions for comparing and validating types * [`data_locations.py`](data_locations.py): `DataLocation` object for type location information @@ -35,13 +36,23 @@ Vyper abstract syntax tree (AST). The [`analysis`](analysis) subpackage contains the top-level `validate_semantics` function. This function is used to verify and type-check a contract. The process -consists of three steps: +consists of four steps: -1. Preparing the builtin namespace -2. Validating the module-level scope -3. Annotating and validating local scopes +1. Populating the metadata of foldable nodes with their replacement nodes +2. Preparing the builtin namespace +3. Validating the module-level scope +4. Annotating and validating local scopes -### 1. Preparing the builtin namespace +### 1. Populating the metadata of foldable nodes with their replacement nodes + +[`analysis/pre_typecheck.py`](analysis/pre_typecheck.py) populates the metadata of foldable nodes with their replacement nodes. + +This process includes: +1. Foldable node classes and builtin functions are evaluated via their `fold` method, which attempts to create a new `Constant` from the content of the given node. +2. Replacement nodes are generated using the `from_node` class method within the new +node class. + +### 2. Preparing the builtin namespace The [`Namespace`](namespace.py) object represents the namespace for a contract. Builtins are added upon initialization of the object. This includes: @@ -51,9 +62,9 @@ Builtins are added upon initialization of the object. This includes: * Adding builtin functions from the [`functions`](../builtins/functions.py) package * Adding / resetting `self` and `log` -### 2. Validating the Module Scope +### 3. Validating the Module Scope -[`validation/module.py`](validation/module.py) validates the module-level scope +[`analysis/module.py`](analysis/module.py) validates the module-level scope of a contract. This includes: * Generating user-defined types (e.g. structs and interfaces) @@ -61,9 +72,9 @@ of a contract. This includes: and functions * Validating import statements and function signatures -### 3. Annotating and validating the Local Scopes +### 4. Annotating and validating the Local Scopes -[`validation/local.py`](validation/local.py) validates the local scope within each +[`analysis/local.py`](analysis/local.py) validates the local scope within each function in a contract. `FunctionNodeVisitor` is used to iterate over the statement nodes in each function body, annotate them and apply appropriate checks.