-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Initial support for Swift macros #6185
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // swift-tools-version: 999.0 | ||
| import PackageDescription | ||
| import CompilerPluginSupport | ||
|
|
||
| let settings: [SwiftSetting] = [ | ||
| .enableExperimentalFeature("Macros"), | ||
| .unsafeFlags(["-Xfrontend", "-dump-macro-expansions"]) | ||
| ] | ||
|
|
||
| let package = Package( | ||
| name: "MacroPackage", | ||
| platforms: [ | ||
| .macOS(.v10_15), | ||
| ], | ||
| targets: [ | ||
| .macro(name: "MacroImpl"), | ||
| .target(name: "MacroDef", dependencies: ["MacroImpl"], swiftSettings: settings), | ||
| .executableTarget(name: "MacroClient", dependencies: ["MacroDef"], swiftSettings: settings), | ||
| ] | ||
| ) |
8 changes: 8 additions & 0 deletions
8
Fixtures/Macros/MacroPackage/Sources/MacroClient/client.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import MacroDef | ||
|
|
||
| struct Font: ExpressibleByFontLiteral { | ||
| init(fontLiteralName: String, size: Int, weight: MacroDef.FontWeight) { | ||
| } | ||
| } | ||
|
|
||
| let _: Font = #fontLiteral(name: "Comic Sans", size: 14, weight: .thin) |
16 changes: 16 additions & 0 deletions
16
Fixtures/Macros/MacroPackage/Sources/MacroDef/definition.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
|
|
||
| public enum FontWeight { | ||
| case thin | ||
| case normal | ||
| case medium | ||
| case semiBold | ||
| case bold | ||
| } | ||
|
|
||
| public protocol ExpressibleByFontLiteral { | ||
| init(fontLiteralName: String, size: Int, weight: FontWeight) | ||
| } | ||
|
|
||
| /// Font literal similar to, e.g., #colorLiteral. | ||
| @freestanding(expression) public macro fontLiteral<T>(name: String, size: Int, weight: FontWeight) -> T = #externalMacro(module: "MacroImpl", type: "FontLiteralMacro") | ||
| where T: ExpressibleByFontLiteral |
39 changes: 39 additions & 0 deletions
39
Fixtures/Macros/MacroPackage/Sources/MacroImpl/macro.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import SwiftSyntax | ||
| import SwiftSyntaxBuilder | ||
| import SwiftSyntaxMacros | ||
|
|
||
| /// Implementation of the `#fontLiteral` macro, which is similar in spirit | ||
| /// to the built-in expressions `#colorLiteral`, `#imageLiteral`, etc., but in | ||
| /// a small macro. | ||
| public struct FontLiteralMacro: ExpressionMacro { | ||
| public static func expansion( | ||
| of macro: some FreestandingMacroExpansionSyntax, | ||
| in context: some MacroExpansionContext | ||
| ) -> ExprSyntax { | ||
| let argList = replaceFirstLabel( | ||
| of: macro.argumentList, | ||
| with: "fontLiteralName" | ||
| ) | ||
| let initSyntax: ExprSyntax = ".init(\(argList))" | ||
| if let leadingTrivia = macro.leadingTrivia { | ||
| return initSyntax.with(\.leadingTrivia, leadingTrivia) | ||
| } | ||
| return initSyntax | ||
| } | ||
| } | ||
|
|
||
| /// Replace the label of the first element in the tuple with the given | ||
| /// new label. | ||
| private func replaceFirstLabel( | ||
| of tuple: TupleExprElementListSyntax, | ||
| with newLabel: String | ||
| ) -> TupleExprElementListSyntax { | ||
| guard let firstElement = tuple.first else { | ||
| return tuple | ||
| } | ||
|
|
||
| return tuple.replacing( | ||
| childAt: 0, | ||
| with: firstElement.with(\.label, .identifier(newLabel)) | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This essentially flips the logic for now since dylib macros are the only ones which can be tested end-to-end right now.