-
Notifications
You must be signed in to change notification settings - Fork 440
[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
Conversation
swiftlang/swift#61734 |
swiftlang/swift#61734 |
1 similar comment
swiftlang/swift#61734 |
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.
Yeah, will let us tie into the compiler.
c1c4edf
to
0b70207
Compare
swiftlang/swift#61734 |
@swift-ci please test |
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.
Looks good!
guard let meeStartToken = sourceFileSyntax.token(at: meePosition), | ||
let mee = meeStartToken.parent?.as(MacroExpansionExprSyntax.self) | ||
else { | ||
fatalError("Unable to locate 'MacroExpansionExprSyntax'") |
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.
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?
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 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.
0b70207
to
e3d2622
Compare
@swift-ci please test |
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
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