-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ASTGen] Several improvements to generalize node handling #69894
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
Conversation
`generate(choices:)` is not good because it expects "ASTNode". Not all `SyntaxChildChoices` can generate ASTNode. * `generate(codeBlockItem:)` to manually switch over the cases * switch on `IfExpr.ElseBody` insdie `makeIfStmt()`, now supports `else if` case
We already know the type of the node. There's no reason switch over again and wrap the result with ASTNode.
Generating ConditionElement using generate(_:) wouldn't work when we implemnt other condition kinds e.g. patterns because they are not ASTNode kinds. Remove generate(choices:) and generate(_:) as they weren't used anymore.
Now that ASTNode is used only for codeBlockItem generation. That align with C++ ASTNode where it represent code items. Since TypeRepr is not an ASTGen in C++, remove it from ASTGen too.
Defining `generate(_: XXXSyntax?) -> BridgedXXX` for each node kind is too much boilerplate. self.generate(optional: optionalXXXNode).asNullable is now: self.map(optionalXXXNode, generate(xxx:))
@swift-ci Please smoke test |
thrownType: self.generate(optional: node.effectSpecifiers?.thrownError?.type).asNullable, | ||
thrownType: self.map(node.effectSpecifiers?.thrownError?.type, generate(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’m not a fan of this. I think the old version read a lot easier and IMO it read so much easier that it’s worth defining the optional overloads.
@AnthonyLatsis and I had a whole conversation about this in #67111 (comment)
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 feel too strongly about this, though I think I do mildly prefer the old version
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.
OK, let's restore the specialized generate
functions.
private func _build<Node: SyntaxProtocol>( | ||
kind: Node.Type, | ||
private func _build<Node: SyntaxProtocol, Result>( | ||
generator: (ASTGenVisitor) -> (Node) -> Result, |
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 would name this generateFunction
. generator
sounds to much like an object to me. Also, I think a doc comment for this parameter wouldn’t hurt.
@swift-ci Please smoke test |
@@ -83,7 +83,7 @@ extension ASTGenVisitor { | |||
|
|||
let (secondName, secondNameLoc) = node.secondName.bridgedIdentifierAndSourceLoc(in: self) | |||
|
|||
var type = self.generate(optional: node.optionalType) | |||
var type = node.optionalType.map(generate(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.
Do you need to change this to also use generate(optional:)
?
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.
Now it returns BridgedNullableTypeRepr
instead of BridgedTypeRepr?
which is not convenient to convert back to BridgedTypeRepr
generate(choices:)
because it was expecting all the choices can generateASTNode
which is not the case.generate(_:) -> ASTNode
because, again, not all syntax kind can generateASTNode
generate(codeBlockItem:)
now returnsASTNode
andASTNode
is now.decl
,.stmt
, or.expr
only. That resembles C++swift::ASTNode
.Nullable
variation can now haveHasNullable
conformance, that teaches how to convertOptional<BridgedT>
toBridgedNullableT
ASTGenVisitor.map(_: T?, _: (T) -> BridgedU) -> BridgedNullableU
for optional node handling.ASTGenVisitor.map(_: TCollection?, _: (TCollection) -> BridgedArrayRef) -> BridgedArrayRef)
for optional collection node handling