Skip to content

[ASTGen] Generalize findSyntaxNodeInSourceFile(wantOutermost:true) #69724

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,11 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
var currentSyntax = Syntax(token)
var resultSyntax: Node? = nil
while let parentSyntax = currentSyntax.parent {
if let typedParent = parentSyntax.as(type) {
currentSyntax = parentSyntax
if let typedParent = currentSyntax.as(type) {
resultSyntax = typedParent
break
}

currentSyntax = parentSyntax
}

// If we didn't find anything, complain and fail.
Expand All @@ -651,26 +650,16 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
}

// If we want the outermost node, keep looking.
// FIXME: This is VERY SPECIFIC to handling of types. We must be able to
// do better.
// E.g. for 'foo.bar' we want the member ref expression instead of the
// identifier expression.
if wantOutermost {
while let parentSyntax = resultSyntax.parent {
// Look through type compositions.
if let compositionElement = parentSyntax.as(CompositionTypeElementSyntax.self),
let compositionList = compositionElement.parent?.as(CompositionTypeElementListSyntax.self),
let typedParent = compositionList.parent?.as(type)
{
while
let parentSyntax = currentSyntax.parent,
parentSyntax.position == resultSyntax.position {
Copy link
Member Author

Choose a reason for hiding this comment

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

It is a restriction that the outer nodes must stay in the same position, and it's inconsistent with the innermost node discovery. But in our use cases, this should be enough.

Copy link
Member Author

@rintaro rintaro Nov 8, 2023

Choose a reason for hiding this comment

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

Maybe we should have two separate methods like

// For macro expansion node discovery:
findTokenInSourceFile(in: exporetedSourceFile, at: sourceLoc)?
  .innermostNode(ofType: AttributeSyntax.self)

// For partial ASTGen:
findTokenInSourceFile(in: exporetedSourceFile, at: sourceLoc)?
  .outermostNodeAtSamePosition(ofType: ExprSyntax.self)

currentSyntax = parentSyntax
if let typedParent = currentSyntax.as(type) {
resultSyntax = typedParent
continue
}

guard let typedParent = parentSyntax.as(type),
typedParent.position == resultSyntax.position
else {
break
}

resultSyntax = typedParent
}
}

Expand Down