Skip to content

[5.9][Macro] Attributes for freestanding macro expansion #1675

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
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
3 changes: 2 additions & 1 deletion CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public let COMMON_NODES: [Node] = [
description: "In case the source code is missing a declaration, this node stands in place of the missing declaration.",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down
77 changes: 55 additions & 22 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "accessor",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes"
],
parserFunction: "parseAccessorDecl",
children: [
Expand Down Expand Up @@ -154,7 +154,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -213,7 +214,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -270,7 +272,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -401,7 +404,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "deinitializer",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -505,7 +509,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "parameter",
kind: "Syntax",
traits: [
"WithTrailingComma"
"WithTrailingComma",
"WithModifiers",
],
parserFunction: "parseEnumCaseParameter",
children: [
Expand Down Expand Up @@ -559,7 +564,8 @@ public let DECL_NODES: [Node] = [
description: "A `case` declaration of a Swift `enum`. It can have 1 or more `EnumCaseElement`s inside, each declaring a different case of the enum.",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -642,7 +648,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -710,7 +717,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"DeclGroup",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -758,7 +766,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -822,7 +831,8 @@ public let DECL_NODES: [Node] = [
kind: "Syntax",
traits: [
"WithTrailingComma",
"Attributed",
"WithAttributes",
"WithModifiers",
],
parserFunction: "parseFunctionParameter",
children: [
Expand Down Expand Up @@ -983,7 +993,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "import",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1073,7 +1084,8 @@ public let DECL_NODES: [Node] = [
""",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1136,7 +1148,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1191,9 +1204,23 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "macro expansion",
kind: "Decl",
traits: [
"FreestandingMacroExpansion"
"FreestandingMacroExpansion",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
name: "Attributes",
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
nameForDiagnostics: "attributes",
isOptional: true
),
Child(
name: "Modifiers",
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
nameForDiagnostics: "modifiers",
isOptional: true
),
Child(
name: "PoundToken",
kind: .token(choices: [.token(tokenKind: "PoundToken")]),
Expand Down Expand Up @@ -1331,7 +1358,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1607,7 +1635,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1709,7 +1738,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1811,7 +1841,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1864,7 +1895,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "subscript",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1965,7 +1997,7 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
],
children: [
Child(
Expand Down Expand Up @@ -2012,7 +2044,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "variable",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down
6 changes: 4 additions & 2 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ public let EXPR_NODES: [Node] = [
nameForDiagnostics: "parameter",
kind: "Syntax",
traits: [
"WithTrailingComma"
"WithTrailingComma",
"WithAttributes",
"WithModifiers",
],
parserFunction: "parseClosureParameter",
children: [
Expand Down Expand Up @@ -461,7 +463,7 @@ public let EXPR_NODES: [Node] = [
nameForDiagnostics: "closure signature",
kind: "Syntax",
traits: [
"Attributed"
"WithAttributes"
],
children: [
Child(
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public let GENERIC_NODES: [Node] = [
kind: "Syntax",
traits: [
"WithTrailingComma",
"Attributed",
"WithAttributes",
],
children: [
Child(
Expand Down
18 changes: 12 additions & 6 deletions CodeGeneration/Sources/SyntaxSupport/Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public class Trait {
}

public let TRAITS: [Trait] = [
Trait(
traitName: "Attributed",
children: [
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
]
),
Trait(
traitName: "Braced",
children: [
Expand Down Expand Up @@ -80,12 +74,24 @@ public let TRAITS: [Trait] = [
Child(name: "RightParen", kind: .token(choices: [.token(tokenKind: "RightParenToken")])),
]
),
Trait(
traitName: "WithAttributes",
children: [
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
]
),
Trait(
traitName: "WithCodeBlock",
children: [
Child(name: "Body", kind: .node(kind: "CodeBlock"))
]
),
Trait(
traitName: "WithModifiers",
children: [
Child(name: "Modifiers", kind: .node(kind: "ModifierList"), isOptional: true)
]
),
Trait(
traitName: "WithStatements",
children: [
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public let TYPE_NODES: [Node] = [
nameForDiagnostics: "type",
kind: "Type",
traits: [
"Attributed"
"WithAttributes"
],
children: [
Child(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax(
"""
public protocol SyntaxCollection: SyntaxProtocol, Sequence where Element: SyntaxProtocol {
/// Creates a new collection with the elements.
init(_ children: [Element])
/// The number of elements, `present` or `missing`, in this collection.
var count: Int { get }
}
Expand Down
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let package = Package(
.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]),
.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]),
.library(name: "SwiftSyntaxMacros", targets: ["SwiftSyntaxMacros"]),
.library(name: "SwiftSyntaxMacroExpansion", targets: ["SwiftSyntaxMacroExpansion"]),
.library(name: "SwiftSyntaxMacrosTestSupport", targets: ["SwiftSyntaxMacrosTestSupport"]),
],
targets: [
Expand Down Expand Up @@ -113,7 +114,7 @@ let package = Package(

.target(
name: "SwiftCompilerPluginMessageHandling",
dependencies: ["SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxMacros"],
dependencies: ["SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxMacros", "SwiftSyntaxMacroExpansion"],
exclude: ["CMakeLists.txt"]
),

Expand Down Expand Up @@ -183,6 +184,14 @@ let package = Package(
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
),

// MARK: SwiftSyntaxMacroExpansion

.target(
name: "SwiftSyntaxMacroExpansion",
dependencies: ["SwiftSyntax", "SwiftSyntaxMacros"],
exclude: ["CMakeLists.txt"]
),

// MARK: SwiftSyntaxMacrosTestSupport

.target(
Expand Down
1 change: 1 addition & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ add_subdirectory(SwiftParserDiagnostics)
add_subdirectory(SwiftOperators)
add_subdirectory(SwiftSyntaxBuilder)
add_subdirectory(SwiftSyntaxMacros)
add_subdirectory(SwiftSyntaxMacroExpansion)
add_subdirectory(SwiftCompilerPluginMessageHandling)
add_subdirectory(SwiftIDEUtils)
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC
SwiftDiagnostics
SwiftParser
SwiftSyntaxMacros
SwiftSyntaxMacroExpansion
SwiftOperators)
Loading