-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathmacros.swift
155 lines (120 loc) · 4.06 KB
/
macros.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// REQUIRES: swift_swift_parser, asserts
// REQUIRES: swift_feature_ParserASTGen
// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/../Macros/Inputs/syntax_macro_definitions.swift
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -enable-experimental-feature ParserASTGen
@freestanding(declaration) macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
func foo(a: Int) {
_ = #stringify(a + 1)
}
struct Outer {
#anonymousTypes { "test" }
}
@attached(extension, conformances: P1, P2)
macro AddAllConformances() = #externalMacro(module: "MacroDefinition", type: "AddAllConformancesMacro")
protocol P1 {}
protocol P2 {}
@AddAllConformances
struct MultipleConformances {}
func testConformances() {
func eat(arg: some P1) {}
eat(arg: MultipleConformances())
}
protocol DefaultInit {
init()
}
@attached(extension, conformances: Equatable, names: named(==))
macro Equatable() = #externalMacro(module: "MacroDefinition", type: "EquatableViaMembersMacro")
@propertyWrapper
struct NotEquatable<T> {
var wrappedValue: T
}
@Equatable
struct HasPropertyWrappers {
@NotEquatable
var value: Int = 0
}
func requiresEquatable<T: Equatable>(_: T) { }
func testHasPropertyWrappers(hpw: HasPropertyWrappers) {
requiresEquatable(hpw)
}
@attached(extension, conformances: DefaultInit)
@attached(member, conformances: DefaultInit, names: named(init()), named(f()))
macro DefaultInit() = #externalMacro(module: "MacroDefinition", type: "RequiredDefaultInitMacro")
@DefaultInit
class C { }
@DefaultInit
class D: C { }
@DefaultInit
struct E { }
@attached(memberAttribute)
@attached(member, names: named(_storage))
macro myTypeWrapper() = #externalMacro(module: "MacroDefinition", type: "TypeWrapperMacro")
@attached(accessor)
macro accessViaStorage() = #externalMacro(module: "MacroDefinition", type: "AccessViaStorageMacro")
struct _Storage {
var x: Int = 0 {
willSet { print("setting \(newValue)") }
}
var y: Int = 0 {
willSet { print("setting \(newValue)") }
}
}
@myTypeWrapper
struct S {
var x: Int
var y: Int
}
@attached(body)
macro Remote() = #externalMacro(module: "MacroDefinition", type: "RemoteBodyMacro")
protocol ConjureRemoteValue {
static func conjureValue() -> Self
}
extension String: ConjureRemoteValue {
static func conjureValue() -> String { "" }
}
func remoteCall<Result: ConjureRemoteValue>(function: String, arguments: [String: Any]) async throws -> Result {
let printedArgs = arguments.keys.sorted().map { key in
"\(key): \(arguments[key]!)"
}.joined(separator: ", ")
print("Remote call \(function)(\(printedArgs))")
return Result.conjureValue()
}
@Remote
func f(a: Int, b: String) async throws -> String
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
struct TestArbitrary {
#bitwidthNumberedStructs("MyIntOne")
}
// Stored properties generated by a peer macro
@attached(accessor)
@attached(peer, names: prefixed(_))
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
struct MyWrapperThingy<T> {
var storage: T
var wrappedValue: T {
get {
print("Getting value \(storage)")
return storage
}
set {
print("Setting value \(newValue)")
storage = newValue
}
}
}
struct S3 {
@myPropertyWrapper
var x: Int = 0
init(x: Int) {
self._x = MyWrapperThingy(storage: x)
}
}
protocol MyType {
associatedtype Value
associatedtype Entity
}
@attached(peer, names: named(bar))
macro Wrapper<Value>(get: (Value.Entity) async throws -> Value.Value) = #externalMacro(module: "MacroDefinition", type: "WrapperMacro") where Value: MyType