forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc_cxx_macro_definitions.swift
129 lines (119 loc) · 3.66 KB
/
objc_cxx_macro_definitions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import SwiftParser
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
structObjCMemberFuncMacro:MemberMacro{
staticfunc expansion(
of node:AttributeSyntax,
providingMembersOf declaration:someDeclGroupSyntax,
conformingTo protocols:[TypeSyntax],
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
guardlet decl = declaration.asProtocol(NamedDeclSyntax.self)else{
return[]
}
return["@objc public func member_\(raw: decl.name.text)() {}"]
}
}
structObjCPeerFuncMacro:PeerMacro{
staticfunc expansion(
of node:AttributeSyntax,
providingPeersOf declaration:someDeclSyntaxProtocol,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
guardlet decl = declaration.asProtocol(NamedDeclSyntax.self)else{
return[]
}
return["@objc public func peer_\(raw: decl.name.text)() {}"]
}
}
structObjCFreestandingFuncMacro:DeclarationMacro{
staticfunc expansion(
of node:someFreestandingMacroExpansionSyntax,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
return["@objc public func member_freestanding() {}"]
}
}
structObjCFreestandingClassMacro:DeclarationMacro{
staticfunc expansion(
of node:someFreestandingMacroExpansionSyntax,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
return["""
@objc public class MacroExpandedObjCClass: NSObject {
@objc public func member() {}
}
"""]
}
}
structCDeclFreestandingFuncMacro:DeclarationMacro{
staticfunc expansion(
of node:someFreestandingMacroExpansionSyntax,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
return[#"@_cdecl("c_freestanding") public func cFreestanding() {}"#]
}
}
structObjCExtensionMacro:ExtensionMacro{
staticfunc expansion(
of node:AttributeSyntax,
attachedTo declaration:someDeclGroupSyntax,
providingExtensionsOf type:someTypeSyntaxProtocol,
conformingTo protocols:[TypeSyntax],
in context:someMacroExpansionContext
)throws->[ExtensionDeclSyntax]{
letdecl:DeclSyntax="""
extension \(type): MyObjCProtocol {
public func objcRequirement() {}
}
"""
return[decl.as(ExtensionDeclSyntax.self)!]
}
}
structMemberFuncMacro:MemberMacro{
staticfunc expansion(
of node:AttributeSyntax,
providingMembersOf declaration:someDeclGroupSyntax,
conformingTo protocols:[TypeSyntax],
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
guardlet decl = declaration.asProtocol(NamedDeclSyntax.self)else{
return[]
}
return["public func member_\(raw: decl.name.text)() {}"]
}
}
structPeerFuncMacro:PeerMacro{
staticfunc expansion(
of node:AttributeSyntax,
providingPeersOf declaration:someDeclSyntaxProtocol,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
guardlet decl = declaration.asProtocol(NamedDeclSyntax.self)else{
return[]
}
return["public func peer_\(raw: decl.name.text)() {}"]
}
}
structCxxFreestandingFuncMacro:DeclarationMacro{
staticfunc expansion(
of node:someFreestandingMacroExpansionSyntax,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
return["public func cxxFreestanding() {}"]
}
}
structCxxFreestandingStructMacro:DeclarationMacro{
staticfunc expansion(
of node:someFreestandingMacroExpansionSyntax,
in context:someMacroExpansionContext
)throws->[DeclSyntax]{
return["""
public struct MacroExpandedStruct {
private let x: Int = 0
public func member() {}
}
"""]
}
}