Skip to content
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

[ASTGen] Generate PatternBindingDecl at top level #79355

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 13, 2025

  • Hoist VarDecl in ASTGen, instead of do it in the bridging functions.
  • Introduce Decl::forEachDeclToHoist to handle VarDecls in PatternBindingDecl, and EnumElementDecl in EnumCaseDecl.
  • Introduce withBridgedSwiftClosure(closure:call:) as a callback mechanism between Swift and C++
  • In generate(sourceFile:), instead of using generate(codeBlockItem:), handle CodeBlockItemSyntax.Item manually to perform TLCD wrapping and VarDecl hoisting.
  • Make generate(variableDecl:) handle TLCD correctly.

@rintaro rintaro changed the title [ASTGen] Generate TopLevelCodeDecl for PatternBindingDecl [ASTGen] Generate PatternBindingDecl at top level Feb 13, 2025
@rintaro
Copy link
Member Author

rintaro commented Feb 13, 2025

@swift-ci Please smoke test

* Instead of hoisting VarDecl in the bridging functions, do it in
  ASTGen.
* Introduce `Decl::forEachDeclToHoist` to handle VarDecls in
  PatternBindingDecl, and EnumElementDecl in EnumCaseDecl.
* Intorduce `withBridgedSwiftClosure(closure:call:)` as a callback
  mechanism between Swift and C++
* In `generate(sourceFile:)`, instead of using `generate(codeBlockItem:)`
  handle `CodeBlockItemSyntax.Item` manually to handle `TLCD` wrapping
  and `VarDecl` hoisting.
* Make `generate(variableDecl:)` handle TLCD correctly.
@rintaro rintaro force-pushed the astgen-toplevelvardecl branch from a70914f to 6ea6a31 Compare February 13, 2025 08:09
@rintaro
Copy link
Member Author

rintaro commented Feb 13, 2025

@swift-ci Please smoke test

// MARK: BridgedSoureFile
//===----------------------------------------------------------------------===//

BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: BRIDGED_INLINE isn't needed here

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy paste error... let me fix it later along with another BRIDGED_INLINE in this file.

Comment on lines +326 to +337
func withBridgedSwiftClosure(closure: (UnsafeRawPointer?) -> Void, call: (BridgedSwiftClosure) -> Void) {
withoutActuallyEscaping(closure) { escapingClosure in
withUnsafePointer(to: escapingClosure) { ptr in
call(BridgedSwiftClosure(closure: ptr))
}
}
}

@_cdecl("swift_ASTGen_bridgedSwiftClosureCall_1")
func bridgedSwiftClosureCall_1(_ bridged: BridgedSwiftClosure, _ arg: UnsafeRawPointer?) {
bridged.closure.assumingMemoryBound(to: ((UnsafeRawPointer?) -> Void).self).pointee(arg)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't say I love this, I guess the alternative would be having the bridging API take a C function pointer along with an opaque void * context which the caller would then load as the right type. Not sure that would be much of an improvement though :(

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love either, this was the best I came up with 😓

@@ -253,6 +263,23 @@ extension ASTGenVisitor {
return generateSourceRange(node)
}

/// Obtains bridged token source range for a syntax node.
/// Unlike `generateSourceRange(_:)`, this correctly emulates the string/regex literal token SourceLoc in AST.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to not have generateSourceRange handle this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Performance concern, I believe endTok.parent?.kind still implies retain/release. Also, after fully migrating to SwiftParser/ASTGen, I believe generateSourceRange() is more correct. So I hesitate to use this function extensively.

@rintaro rintaro merged commit e066bb2 into swiftlang:main Feb 13, 2025
3 checks passed
@rintaro rintaro deleted the astgen-toplevelvardecl branch February 13, 2025 18:53
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.

2 participants