Skip to content

[Macros] Introduce member attribute macros. #1220

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
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/_SwiftSyntaxMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_swift_host_library(_SwiftSyntaxMacros
Macro.swift
MacroExpansionContext.swift
MacroSystem.swift
MemberAttributeMacro.swift
MemberDeclarationMacro.swift
PeerDeclarationMacro.swift
Syntax+MacroEvaluation.swift
Expand Down
68 changes: 65 additions & 3 deletions Sources/_SwiftSyntaxMacros/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class MacroApplication: SyntaxRewriter {
var context: MacroExpansionContext
var skipNodes: Set<Syntax> = []

/// A stack of member attribute macos to expand when iterating over a `MemberDeclListSyntax`.
var memberAttributeMacros: [([(CustomAttributeSyntax, MemberAttributeMacro.Type)], DeclSyntax)] = []

init(
macroSystem: MacroSystem,
context: MacroExpansionContext
Expand Down Expand Up @@ -106,7 +109,7 @@ class MacroApplication: SyntaxRewriter {
return true
}

return !(macro is PeerDeclarationMacro.Type || macro is MemberDeclarationMacro.Type || macro is AccessorDeclarationMacro.Type)
return !(macro is PeerDeclarationMacro.Type || macro is MemberDeclarationMacro.Type || macro is AccessorDeclarationMacro.Type || macro is MemberAttributeMacro.Type)
}

if newAttributes.isEmpty {
Expand Down Expand Up @@ -203,9 +206,21 @@ class MacroApplication: SyntaxRewriter {
continue
}

// Expand member attribute members attached to the declaration context.
let attributedMember: MemberDeclListSyntax.Element
if let (macroAttributes, decl) = memberAttributeMacros.last {
attributedMember = expandAttributes(
for: macroAttributes,
attachedTo: decl,
annotating: item
)
} else {
attributedMember = item
}

// Recurse on the child node.
let newDecl = visit(item.decl)
newItems.append(item.withDecl(newDecl))
let newDecl = visit(attributedMember.decl)
newItems.append(attributedMember.withDecl(newDecl))

// Expand any peer declarations triggered by macros used as attributes.
let peers = expandPeers(of: item.decl)
Expand All @@ -222,6 +237,14 @@ class MacroApplication: SyntaxRewriter {
func visit<DeclType: DeclGroupSyntax & DeclSyntaxProtocol>(
declGroup: DeclType
) -> DeclSyntax {
memberAttributeMacros.append(
(
getMacroAttributes(attachedTo: DeclSyntax(declGroup), ofType: MemberAttributeMacro.Type.self),
DeclSyntax(declGroup)
)
)
defer { memberAttributeMacros.removeLast() }

// Expand any attached member macros.
let expandedDeclGroup = expandMembers(of: declGroup)

Expand Down Expand Up @@ -392,6 +415,45 @@ extension MacroApplication {
}
)
}

private func expandAttributes(
for macroAttributes: [(CustomAttributeSyntax, MemberAttributeMacro.Type)],
attachedTo decl: DeclSyntax,
annotating member: MemberDeclListSyntax.Element
) -> MemberDeclListSyntax.Element {
guard let attributedDecl = member.decl.asProtocol(AttributedSyntax.self) else {
return member
}

var attributes: [CustomAttributeSyntax] = []
for (attribute, attributeMacro) in macroAttributes {
do {
try attributes.append(
contentsOf: attributeMacro.expansion(
of: attribute,
attachedTo: DeclSyntax(decl),
annotating: member.decl,
in: &context
)
)
} catch {
// Record the error
context.diagnose(
Diagnostic(
node: Syntax(attribute),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)
}
}

let newAttributes = attributes.reduce(attributedDecl.attributes ?? .init([])) {
$0.appending(AttributeListSyntax.Element($1))
}

let newDecl = attributedDecl.withAttributes(newAttributes).as(DeclSyntax.self)!
return member.withDecl(newDecl)
}
}

extension SyntaxProtocol {
Expand Down
34 changes: 34 additions & 0 deletions Sources/_SwiftSyntaxMacros/MemberAttributeMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

/// Describes a macro that can add attributes to the members inside the
/// declaration it's attached to.
public protocol MemberAttributeMacro: DeclarationMacro {
/// Expand an attached declaration macro to produce an attribute list for
/// a given member.
///
/// - Parameters:
/// - node: The custom attribute describing the attached macro.
/// - declaration: The declaration the macro attribute is attached to.
/// - member: The member delcaration to attach the resulting attributes to.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: the set of attributes to apply to the given member.
static func expansion(
of node: CustomAttributeSyntax,
attachedTo declaration: DeclSyntax,
annotating member: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [CustomAttributeSyntax]
}
142 changes: 142 additions & 0 deletions Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,70 @@ public struct AddBackingStorage: MemberDeclarationMacro {
}
}

public struct WrapAllProperties: MemberAttributeMacro {
public static func expansion(
of node: CustomAttributeSyntax,
attachedTo decl: DeclSyntax,
annotating member: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [CustomAttributeSyntax] {
guard member.is(VariableDeclSyntax.self) else {
return []
}

return [
CustomAttributeSyntax(
attributeName: SimpleTypeIdentifierSyntax(
name: .identifier("Wrapper")
)
)
.withLeadingTrivia([.newlines(1), .spaces(2)])
]
}
}

public struct WrapStoredProperties: MemberAttributeMacro {
public static func expansion(
of node: CustomAttributeSyntax,
attachedTo decl: DeclSyntax,
annotating member: DeclSyntax,
in context: inout MacroExpansionContext
) throws -> [CustomAttributeSyntax] {
guard let property = member.as(VariableDeclSyntax.self),
property.bindings.count == 1
else {
return []
}

let binding = property.bindings.first!
switch binding.accessor {
case .none:
break
case .accessors(let node):
for accessor in node.accessors {
switch accessor.accessorKind.tokenKind {
case .contextualKeyword(.get), .contextualKeyword(.set):
return []
default:
break
}
}
break
case .getter:
return []
}

return [
CustomAttributeSyntax(
attributeName: SimpleTypeIdentifierSyntax(
name: .identifier("Wrapper")
)
)
.withLeadingTrivia([.newlines(1), .spaces(2)])
]
}
}

// MARK: Assertion helper functions

/// Assert that expanding the given macros in the original source produces
Expand Down Expand Up @@ -467,6 +531,8 @@ public let testMacros: [String: Macro.Type] = [
"wrapProperty": PropertyWrapper.self,
"addCompletionHandler": AddCompletionHandler.self,
"addBackingStorage": AddBackingStorage.self,
"wrapAllProperties": WrapAllProperties.self,
"wrapStoredProperties": WrapStoredProperties.self,
]

final class MacroSystemTests: XCTestCase {
Expand Down Expand Up @@ -645,4 +711,80 @@ final class MacroSystemTests: XCTestCase {
"""
)
}

func testWrapAllProperties() {
AssertMacroExpansion(
macros: testMacros,
"""
@wrapAllProperties
struct Point {
var x: Int
var y: Int
var description: String { "" }
var computed: Int {
get { 0 }
set {}
}

func test() {}
}
""",
"""

struct Point {
@Wrapper
var x: Int
@Wrapper
var y: Int
@Wrapper
var description: String { "" }
@Wrapper
var computed: Int {
get { 0 }
set {}
}

func test() {}
}
"""
)

AssertMacroExpansion(
macros: testMacros,
"""
@wrapStoredProperties
struct Point {
var x: Int
var y: Int

var description: String { "" }

var computed: Int {
get { 0 }
set {}
}

func test() {}
}
""",
"""

struct Point {
@Wrapper
var x: Int
@Wrapper
var y: Int

var description: String { "" }

var computed: Int {
get { 0 }
set {}
}

func test() {}
}
"""
)
}
}