-
-
Notifications
You must be signed in to change notification settings - Fork 836
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: improve ast output #2824
feat: improve ast output #2824
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2824 +/- ##
==========================================
+ Coverage 87.32% 87.40% +0.08%
==========================================
Files 94 94
Lines 10053 10102 +49
Branches 2490 2497 +7
==========================================
+ Hits 8779 8830 +51
+ Misses 805 803 -2
Partials 469 469
Continue to review full report at Codecov.
|
@@ -65,11 +66,13 @@ def visit_Assert(self, node): | |||
|
|||
def visit_Assign(self, node): | |||
type_ = get_exact_type_from_node(node.target) | |||
node._metadata["type"] = type_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why would Assign
have a type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it as an extension to the request in 2276 for more information on the type of AnnAssign
nodes (variable declarations) since they are similar in that the variable type is usually derived from the child nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think 2276 was literally asking for vyper type info on AnnAssign
nodes :) i think they were more asking "what kind of declaration is this". it doesn't make really make sense for statements to have types attached to them -- types generally go on expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay got it, I will revert the changes made to the various *Assign
nodes.
vyper/ast/nodes.py
Outdated
@@ -141,7 +141,34 @@ def _to_dict(value): | |||
# if value is a Vyper node, convert to a dict | |||
if isinstance(value, VyperNode): | |||
return value.to_dict() | |||
return value | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kind of weird to import type modules here. is there some way we can rethink this structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, will look into this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the real issue here is that the representation of the _metadata["type"]
field is not implemented. i think it will be cleaner if we add a method to all vyper types which is like "serialize_for_ast" or something (i didn't think very hard about the name, feel free to come up with alternatives).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that the ast
output should be replaced with the new ast_unfolded
functionality. people who are looking for the AST will generally want the new "unfolded" AST.
This reverts commit 3ddd716.
vyper/compiler/output.py
Outdated
@@ -23,6 +23,14 @@ def build_ast_dict(compiler_data: CompilerData) -> dict: | |||
return ast_dict |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think now build_ast_dict
is dead code? can we just replace the old build_ast_dict
body with the new build_unfolded_ast_dict
body?
vy_ast.validation.validate_literal_nodes(vyper_module) | ||
vy_ast.folding.replace_builtin_constants(vyper_module) | ||
vy_ast.folding.replace_builtin_functions(vyper_module) | ||
validate_semantics(vyper_module, interface_codes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate_semantics(vyper_module, interface_codes) | |
# note: validate_semantics does type inference on the AST | |
validate_semantics(vyper_module, interface_codes) |
@@ -184,6 +193,19 @@ def generate_ast(source_code: str, source_id: int, contract_name: str) -> vy_ast | |||
return vy_ast.parse_to_ast(source_code, source_id, contract_name) | |||
|
|||
|
|||
def generate_unfolded_ast( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add a comment describing that the purpose of this phase is to generate an AST for tooling use?
now that i think about it, i wonder if it breaks other downstream tools. is it possible to check, e.g. whether brownie still works with the unfolded AST?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice work. thanks!
What I did
Partial resolution for #2276, by adding type to the AST
Include more relevant information in the AST output under a new AST format that is partially unfolded (only performs folding of
builtin constants and builtin functions) and after type annotation and validation
How I did it
unfolded_ast
.type
key in metadata type for more nodes.__repr__()
for Event and ContractFunction to show the argument types and return type (for ContractFunction only).to_dict
function._metadata
fields when outputting the AST in a new formatast_extended
, which is after constant folding and annotation.Some limitations
How to verify it
Existing tests pass.
Commit message
Description for the changelog
Add new AST output format after constant folding and type annotation with additional information.
Cute Animal Picture