-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ASTGen] Add experimental feature to use ASTGen in lieu of parsing types #66033
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
Changes from all commits
dba94bd
4031d7c
1340124
b50d5fe
3c1867f
3dad575
e935ebb
4188dca
fb8f71e
17f5a6e
7224b81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,10 +586,11 @@ func expandFreestandingMacroInProcess( | |
} | ||
|
||
/// Retrieve a syntax node in the given source file, with the given type. | ||
private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>( | ||
func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>( | ||
sourceFilePtr: UnsafeRawPointer, | ||
sourceLocationPtr: UnsafePointer<UInt8>?, | ||
type: Node.Type | ||
type: Node.Type, | ||
wantOutermost: Bool = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment what, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will. I should really move this API somewhere else (e.g., on |
||
) -> Node? { | ||
guard let sourceLocationPtr = sourceLocationPtr else { | ||
return nil | ||
|
@@ -615,16 +616,45 @@ private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>( | |
} | ||
|
||
var currentSyntax = Syntax(token) | ||
var resultSyntax: Node? = nil | ||
while let parentSyntax = currentSyntax.parent { | ||
if let typedParent = parentSyntax.as(type) { | ||
return typedParent | ||
resultSyntax = typedParent | ||
break | ||
} | ||
|
||
currentSyntax = parentSyntax | ||
} | ||
|
||
print("unable to find node: \(token.debugDescription)") | ||
return nil | ||
// If we didn't find anything, complain and fail. | ||
guard var resultSyntax else { | ||
print("unable to find node: \(token.debugDescription)") | ||
return nil | ||
} | ||
|
||
// If we want the outermost node, keep looking. | ||
// FIXME: This is VERY SPECIFIC to handling of types. We must be able to | ||
// do better. | ||
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) { | ||
resultSyntax = typedParent | ||
continue | ||
} | ||
|
||
guard let typedParent = parentSyntax.as(type), | ||
typedParent.position == resultSyntax.position else { | ||
break | ||
} | ||
|
||
resultSyntax = typedParent | ||
} | ||
} | ||
|
||
return resultSyntax | ||
} | ||
|
||
@_cdecl("swift_ASTGen_expandAttachedMacro") | ||
|
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.
We really need to figure out a better way to pass things around than using
void *
.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.
Yes, this is pretty dreadful. Typedefs would improve on things a little bit. @bnbarham keeps threatening to add them
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.
#66044 for the common types (though this would be the first
SourceFile
I think?). Decls/exprs/typerefs/etc are a little more difficult since the visitor just returns aASTNode
for eachvisit
at the moment. Even disregarding that I'm not sure we want to bridge every single AST node type that we need.But it would be nice to do something to avoid:
.rawValue
name
was not aBridgedIdentifier
but rather aUnresolvedDeclRefExpr
. Finding that involves looking at the visit forVariableDeclSyntax
-> visits itspattern
-> visitsIdentifierPatternSyntax
-> callsSwiftIdentifierExpr_create
-> creates aUnresolvedDeclRefExpr
.