Skip to content

Commit

Permalink
chore: rename VariableDef to VariableDecl (#2973)
Browse files Browse the repository at this point in the history
more standard terminology

cf. go, rust, C grammars
  • Loading branch information
charles-cooper authored Jul 20, 2022
1 parent 1d1ef5d commit c3e43d7
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion tests/parser/ast_utils/test_ast_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_basic_ast():
"node_id": 4,
"src": "4:6:0",
},
"ast_type": "VariableDef",
"ast_type": "VariableDecl",
"col_offset": 0,
"end_col_offset": 9,
"end_lineno": 2,
Expand Down
4 changes: 2 additions & 2 deletions vyper/ast/expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def generate_public_variable_getters(vyper_module: vy_ast.Module) -> None:
Top-level Vyper AST node.
"""

for node in vyper_module.get_children(vy_ast.VariableDef, {"annotation.func.id": "public"}):
for node in vyper_module.get_children(vy_ast.VariableDecl, {"annotation.func.id": "public"}):
func_type = node._metadata["func_type"]
input_types, return_type = func_type.get_signature()
input_nodes = []
Expand Down Expand Up @@ -100,7 +100,7 @@ def remove_unused_statements(vyper_module: vy_ast.Module) -> None:
"""

# constant declarations - values were substituted within the AST during folding
for node in vyper_module.get_children(vy_ast.VariableDef, {"annotation.func.id": "constant"}):
for node in vyper_module.get_children(vy_ast.VariableDecl, {"annotation.func.id": "constant"}):
vyper_module.remove_from_body(node)

# `implements: interface` statements - validated during type checking
Expand Down
4 changes: 2 additions & 2 deletions vyper/ast/folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def replace_user_defined_constants(vyper_module: vy_ast.Module) -> int:
"""
changed_nodes = 0

for node in vyper_module.get_children(vy_ast.VariableDef):
for node in vyper_module.get_children(vy_ast.VariableDecl):
if not isinstance(node.target, vy_ast.Name):
# left-hand-side of assignment is not a variable
continue
Expand Down Expand Up @@ -279,7 +279,7 @@ def replace_constant(
if not node.get_ancestor(vy_ast.Index):
# do not replace left-hand side of assignments
assign = node.get_ancestor(
(vy_ast.Assign, vy_ast.AnnAssign, vy_ast.AugAssign, vy_ast.VariableDef)
(vy_ast.Assign, vy_ast.AnnAssign, vy_ast.AugAssign, vy_ast.VariableDecl)
)

if assign and node in assign.target.get_descendants(include_self=True):
Expand Down
6 changes: 3 additions & 3 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def get_node(
ast_struct = copy.copy(ast_struct)
del ast_struct["parent"]

# Replace state and local variable declarations `AnnAssign` with `VariableDef`
# Replace state and local variable declarations `AnnAssign` with `VariableDecl`
# Parent node is required for context to determine whether replacement should happen.
if (
ast_struct["ast_type"] == "AnnAssign"
and isinstance(parent, Module)
and not getattr(ast_struct["target"], "id", None) in ("implements",)
):
ast_struct["ast_type"] = "VariableDef"
ast_struct["ast_type"] = "VariableDecl"

vy_class = getattr(sys.modules[__name__], ast_struct["ast_type"], None)
if not vy_class:
Expand Down Expand Up @@ -1248,7 +1248,7 @@ class AnnAssign(VyperNode):
__slots__ = ("target", "annotation", "value", "simple")


class VariableDef(VyperNode):
class VariableDecl(VyperNode):
"""
A contract variable declaration.
Expand Down
2 changes: 1 addition & 1 deletion vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class AnnAssign(VyperNode):
value: VyperNode = ...
annotation: VyperNode = ...

class VariableDef(VyperNode):
class VariableDecl(VyperNode):
target: Name = ...
value: VyperNode = ...
annotation: VyperNode = ...
Expand Down
2 changes: 1 addition & 1 deletion vyper/ast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_to_ast(
raise SyntaxException(str(e), source_code, e.lineno, e.offset) from e

# Add dummy function node to ensure local variables are treated as `AnnAssign`
# instead of state variables (`VariableDef`)
# instead of state variables (`VariableDecl`)
if add_fn_node:
fn_node = python_ast.FunctionDef(add_fn_node, py_ast.body, [], [])
fn_node.body = py_ast.body
Expand Down
2 changes: 1 addition & 1 deletion vyper/codegen/global_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_global_context(

# Statements of the form:
# variable_name: type
elif isinstance(item, vy_ast.VariableDef):
elif isinstance(item, vy_ast.VariableDecl):
global_ctx.add_globals_and_events(item)
# Function definitions
elif isinstance(item, vy_ast.FunctionDef):
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/types/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ def from_annotation(
is_immutable: bool = False,
) -> "BaseTypeDefinition":
"""
Generate a `BaseTypeDefinition` instance of this type from `VariableDef.annotation`
Generate a `BaseTypeDefinition` instance of this type from `VariableDecl.annotation`
or `AnnAssign.annotation`
Arguments
---------
node : VyperNode
Vyper ast node from the `annotation` member of a `VariableDef` or `AnnAssign` node.
Vyper ast node from the `annotation` member of a `VariableDecl` or `AnnAssign` node.
Returns
-------
Expand Down
6 changes: 3 additions & 3 deletions vyper/semantics/types/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,15 @@ def set_reentrancy_key_position(self, position: StorageSlot) -> None:
self.reentrancy_key_position = position

@classmethod
def getter_from_VariableDef(cls, node: vy_ast.VariableDef) -> "ContractFunction":
def getter_from_VariableDecl(cls, node: vy_ast.VariableDecl) -> "ContractFunction":
"""
Generate a `ContractFunction` object from an `VariableDef` node.
Generate a `ContractFunction` object from an `VariableDecl` node.
Used to create getter functions for public variables.
Arguments
---------
node : VariableDef
node : VariableDecl
Vyper ast node to generate the function definition from.
Returns
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/types/user/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ def _get_module_definitions(base_node: vy_ast.Module) -> Tuple[OrderedDict, Dict
# only keep the `ContractFunction` with the longest set of input args
continue
functions[node.name] = func
for node in base_node.get_children(vy_ast.VariableDef, {"annotation.func.id": "public"}):
for node in base_node.get_children(vy_ast.VariableDecl, {"annotation.func.id": "public"}):
name = node.target.id
if name in functions:
raise NamespaceCollision(
f"Interface contains multiple functions named '{name}'", base_node
)
functions[name] = ContractFunction.getter_from_VariableDef(node)
functions[name] = ContractFunction.getter_from_VariableDecl(node)
for node in base_node.get_children(vy_ast.EventDef):
name = node.name
if name in functions or name in events:
Expand Down
6 changes: 3 additions & 3 deletions vyper/semantics/validation/data_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def set_storage_slots_with_overrides(
)

# Iterate through variables
for node in vyper_module.get_children(vy_ast.VariableDef):
for node in vyper_module.get_children(vy_ast.VariableDecl):

# Ignore immutable parameters
if node.get("annotation.func.id") == "immutable":
Expand Down Expand Up @@ -177,7 +177,7 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:
# location in memory at entrance
storage_slot += 1

for node in vyper_module.get_children(vy_ast.VariableDef):
for node in vyper_module.get_children(vy_ast.VariableDecl):

if node.get("annotation.func.id") == "immutable":
continue
Expand Down Expand Up @@ -211,7 +211,7 @@ def set_code_offsets(vyper_module: vy_ast.Module) -> Dict:
ret = {}
offset = 0
for node in vyper_module.get_children(
vy_ast.VariableDef, filters={"annotation.func.id": "immutable"}
vy_ast.VariableDecl, filters={"annotation.func.id": "immutable"}
):
type_ = node._metadata["type"]
type_.set_position(CodeOffset(offset))
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/validation/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def visit_AnnAssign(self, node):
self.namespace[interface_name].validate_implements(node)
return

def visit_VariableDef(self, node):
def visit_VariableDecl(self, node):
name = node.get("target.id")
if name is None:
raise VariableDeclarationException("Invalid module-level assignment", node)
Expand All @@ -162,7 +162,7 @@ def visit_VariableDef(self, node):
if node.is_public:
# generate function type and add to metadata
# we need this when builing the public getter
node._metadata["func_type"] = ContractFunction.getter_from_VariableDef(node)
node._metadata["func_type"] = ContractFunction.getter_from_VariableDecl(node)

elif node.is_immutable:
# mutability is checked automatically preventing assignment
Expand Down

0 comments on commit c3e43d7

Please sign in to comment.