Skip to content

[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

Merged
merged 6 commits into from
Nov 17, 2023

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Nov 15, 2023

  • Remove generate(choices:) because it was expecting all the choices can generate ASTNode which is not the case.
  • Remove generate(_:) -> ASTNode because, again, not all syntax kind can generate ASTNode
  • Only generate(codeBlockItem:) now returns ASTNode and ASTNode is now .decl, .stmt, or .expr only. That resembles C++ swift::ASTNode.
  • Generalize optional node handling.
    • Bridged types that has Nullable variation can now have HasNullable conformance, that teaches how to convert Optional<BridgedT> to BridgedNullableT
    • ASTGenVisitor.map(_: T?, _: (T) -> BridgedU) -> BridgedNullableU for optional node handling.
    • ASTGenVisitor.map(_: TCollection?, _: (TCollection) -> BridgedArrayRef) -> BridgedArrayRef) for optional collection node handling

`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:))
@rintaro
Copy link
Member Author

rintaro commented Nov 15, 2023

@swift-ci Please smoke test

thrownType: self.generate(optional: node.effectSpecifiers?.thrownError?.type).asNullable,
thrownType: self.map(node.effectSpecifiers?.thrownError?.type, generate(type:)),
Copy link
Member

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)

Copy link
Contributor

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

Copy link
Member Author

@rintaro rintaro Nov 15, 2023

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,
Copy link
Member

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.

@rintaro
Copy link
Member Author

rintaro commented Nov 15, 2023

@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:))
Copy link
Member

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:)?

Copy link
Member Author

@rintaro rintaro Nov 15, 2023

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

@rintaro rintaro merged commit 49f28c8 into swiftlang:main Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants