Skip to content

[Macros] Support user-defined macros as compiler plugins #1022

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 1 commit into from
Oct 31, 2022

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Oct 26, 2022

Make Macro refine _CompilerPlugin protocol when _CompilerPluginSupport exists. (_CompilerPluginSupport may not exist when ASTGen or SwiftCompilerSources is built with host tools.) This enables macros to talk to the compiler without an ABI-stable SwiftSyntax.

Friend PR: swiftlang/swift#61734

@rxwei rxwei requested a review from ahoppen as a code owner October 26, 2022 02:10
@rxwei
Copy link
Contributor Author

rxwei commented Oct 26, 2022

swiftlang/swift#61734
@swift-ci please test

@rxwei
Copy link
Contributor Author

rxwei commented Oct 26, 2022

swiftlang/swift#61734
@swift-ci please test

1 similar comment
@rxwei
Copy link
Contributor Author

rxwei commented Oct 27, 2022

swiftlang/swift#61734
@swift-ci please test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Yeah, will let us tie into the compiler.

@rxwei rxwei force-pushed the macro-compiler-plugin branch from c1c4edf to 0b70207 Compare October 28, 2022 02:44
@rxwei
Copy link
Contributor Author

rxwei commented Oct 28, 2022

swiftlang/swift#61734
@swift-ci please test

@rxwei
Copy link
Contributor Author

rxwei commented Oct 28, 2022

@swift-ci please test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Looks good!

guard let meeStartToken = sourceFileSyntax.token(at: meePosition),
let mee = meeStartToken.parent?.as(MacroExpansionExprSyntax.self)
else {
fatalError("Unable to locate 'MacroExpansionExprSyntax'")
Copy link
Member

Choose a reason for hiding this comment

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

It makes me pretty nervous to have a fatalError in something that will be in the compiler process. In some follow-up PR, can you find a way to report the failure back out of this API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I'll find a way to allow diagnostics to be reported back

Make `Macro` refine `_CompilerPlugin` protocol when `_CompilerPluginSupport` exists. (`_CompilerPluginSupport` may not exist when ASTGen or SwiftCompilerSources is built with host tools.) This enables macros to talk to the compiler without an ABI-stable SwiftSyntax.
@rxwei rxwei force-pushed the macro-compiler-plugin branch from 0b70207 to e3d2622 Compare October 28, 2022 18:35
@rxwei
Copy link
Contributor Author

rxwei commented Oct 29, 2022

@swift-ci please test

rxwei added a commit to swiftlang/swift that referenced this pull request Oct 31, 2022
Allow user-defined macros to be loaded from dynamic libraries and evaluated.

- Introduce a _CompilerPluginSupport module installed into the toolchain. Its `_CompilerPlugin` protocol acts as a stable interface between the compiler and user-defined macros.
- Introduce a `-load-plugin-library <path>` attribute which allows users to specify dynamic libraries to be loaded into the compiler.

A macro library must declare a public top-level computed property `public var allMacros: [Any.Type]` and be compiled to a dynamic library. The compiler will call the getter of this property to obtain and register all macros.

Known issues:
- We current do not have a way to strip out unnecessary symbols from the plugin dylib, i.e. produce a plugin library that does not contain SwiftSyntax symbols that will collide with the compiler itself.
- `MacroExpansionExpr`'s type is hard-coded as `(Int, String)`. It should instead be specified by the macro via protocol requirements such as `signature` and `genericSignature`. We need more protocol requirements in `_CompilerPlugin` to handle this.
- `dlopen` is not secure and is only for prototyping use here.

Friend PR: swiftlang/swift-syntax#1022
@rxwei rxwei merged commit e14b79c into swiftlang:main Oct 31, 2022
@rxwei rxwei deleted the macro-compiler-plugin branch October 31, 2022 21:03
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