Skip to content

[ASTGen] Move logic in BridgedASTContext.getIdentifier() to ASTGen #70220

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 3 commits into from
Dec 6, 2023

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Dec 5, 2023

getIdentifier() has some logic includning _ -> Identifier() and stripping escaped identifier.
Ideally, ASTBridging should only do the necessary work for bridging. All non-trivial logic should be in ASTGen.

Also, move-back getBridgedIdentifier(), getBridgedSourceLoc(), etc to ASTGenVisitor. Now that getIdentifier() has some logic, so it's more natural in ASTGenVisitor. As for getBridgedSourceLoc() this provides syntactic consistency with generate(xxx:) functions, e.g.

    return.createParsed(
      self.ctx,
      leftParenLoc: self.generateSourceLoc(node.leftParen),
      parameters: self.generate(functionParameterList: node.parameters),
      rightParenLoc: self.generateSourceLoc(node.rightParen)
    )

@rintaro
Copy link
Member Author

rintaro commented Dec 5, 2023

swiftlang/swift-syntax#2383
@swift-ci Please smoke test

@rintaro rintaro marked this pull request as draft December 5, 2023 00:51
@rintaro rintaro force-pushed the astgen-getidentifier branch from 10c067f to 1f46f64 Compare December 5, 2023 04:33
@rintaro
Copy link
Member Author

rintaro commented Dec 5, 2023

@swift-ci Please smoke test

@rintaro rintaro marked this pull request as ready for review December 5, 2023 14:40
Comment on lines 171 to 173
if rawText == "_" {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this now check for tokenKind == .wildcard instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokenKind instantiate String. Let's expose TokenSyntax.tokenView.rawKind as TokenSyntax.rawTokenKind as a SPI, in followups.

Comment on lines 160 to 172
func bridgedIdentifier(token: TokenSyntax?) -> BridgedIdentifier {
token.map(bridgedIdentifier(token:)) ?? nil
}

/// Obtains the start location of the node excluding leading trivia in the
/// source buffer.
func bridgedSourceLoc(syntax node: some SyntaxProtocol) -> BridgedSourceLoc {
BridgedSourceLoc(at: node.positionAfterSkippingLeadingTrivia, in: self.base)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the token: and syntax: argument labels aren't adding much useful information, could they be replaced by for:?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the old variants of all these methods were marked @inline(__always), does that need to be preserved?

path: node.path.lazy.map {
$0.name.bridgedIdentifierAndSourceLoc(in: self) as BridgedIdentifierAndSourceLoc
self.bridgedIdentifierAndSourceLoc(token: $0.name) as BridgedIdentifierAndSourceLoc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize this was already the case, but the return type overloading here makes me sad, maybe we should rename BridgedIdentifierAndSourceLoc to BridgedLocatedIdentifier or something and rename this overload of the method too

Comment on lines 174 to 175
if rawText.count > 2 && rawText.hasPrefix("`") && rawText.hasSuffix("`") {
text = .init(rebasing: text.dropFirst().dropLast())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess SwiftSyntax doesn't have a better way of doing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like .trim("`")? No.
This is basically just slicing RandomAccessCollection, and I think this is the simplest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just meant a utility for getting the actual identifier for an escaped identifier (e.g we have getText vs getRawText on Token in the old parser)

@rintaro rintaro force-pushed the astgen-getidentifier branch from 1f46f64 to 6adbd18 Compare December 5, 2023 19:54
@rintaro
Copy link
Member Author

rintaro commented Dec 5, 2023

@swift-ci Please smoke test

Copy link
Member Author

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per offline discussion, renamed:
bridgedIdentifier(token:) -> generateIdentifier(_:)
bridgedSourceLoc(syntax:) -> generateSourceLoc(_:)
etc.

@rintaro rintaro force-pushed the astgen-getidentifier branch from 6adbd18 to d695726 Compare December 5, 2023 21:06
@rintaro
Copy link
Member Author

rintaro commented Dec 5, 2023

@swift-ci Please smoke test

Ideally ASTBriding should only do the neccessary work for briding. All
non-trivial logic should be in ASTGen.
Now that `getIdentifier()` has some logic in it. It's not a simple
bridging.
To avoid conflicts while migration. When all usage are migrated, this
commit should be reverted.
@rintaro rintaro force-pushed the astgen-getidentifier branch from d695726 to 2f46e6f Compare December 5, 2023 22:33
@rintaro
Copy link
Member Author

rintaro commented Dec 5, 2023

@swift-ci Please smoke test

@rintaro rintaro enabled auto-merge December 5, 2023 22:33
@rintaro rintaro merged commit 7b4e58a into swiftlang:main Dec 6, 2023
Comment on lines +159 to 161
@available(*, deprecated, message: "use ASTContext.bridgedIdentifier(token:)")
@inline(__always)
func bridgedIdentifier(in astgen: ASTGenVisitor) -> BridgedIdentifier {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant "use ASTGenVisitor.generateIdentifier(_:)". Ditto other deprecations. Do we even need them though? It looks like you already audited all the calls.

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