forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransparent_attribute.swift
212 lines (186 loc) · 5.94 KB
/
transparent_attribute.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// RUN: %target-swift-frontend -emit-silgen -emit-verbose-sil %s | %FileCheck %s
// Test that the attribute gets set on default argument generators.
@_transparent func transparentFuncWithDefaultArgument (x: Int = 1) -> Int {
return x
}
func useTransparentFuncWithDefaultArgument() -> Int {
return transparentFuncWithDefaultArgument();
// CHECK-LABEL: sil hidden @_TF21transparent_attribute37useTransparentFuncWithDefaultArgumentFT_Si
// CHECK: apply {{.*}} line:8:44
// CHECK: apply {{.*}} line:8:10
// CHECK: return
}
func transparentFuncWithoutDefaultArgument (x: Int = 1) -> Int {
return x
}
func useTransparentFuncWithoutDefaultArgument() -> Int {
return transparentFuncWithoutDefaultArgument();
// CHECK-LABEL: sil hidden @_TF21transparent_attribute40useTransparentFuncWithoutDefaultArgumentFT_Si
// CHECK: apply {{.*}} line:21:47
// CHECK-NOT: transparent
// CHECK: apply {{.*}} line:21:10
// CHECK: return
}
// Make sure the transparent attribute is set on constructors (allocating and initializing).
struct StructWithTranspConstructor {
@_transparent init () {}
}
func testStructWithTranspConstructor() -> StructWithTranspConstructor {
return StructWithTranspConstructor()
// transparent_attribute.StructWithTranspConstructor.constructor
// CHECK-APPLY: sil hidden @_TFV21transparent_attribute27StructWithTranspConstructorC
// testStructWithTranspConstructor
// CHECK-APPLY: _T21transparent_attribute31testStructWithTranspConstructorFT_VS_27StructWithTranspConstructor
// CHECK: apply {{.*}} line:36:10
}
struct MySt {}
var _x = MySt()
var x1 : MySt {
@_transparent
get {
return _x
}
@_transparent
set {
_x = newValue
}
}
var x2 : MySt {
@_transparent
set(v) {
_x = v
}
@_transparent
get {
return _x
}
}
func testProperty(z: MySt) {
x1 = z
x2 = z
var m1 : MySt = x1
var m2 : MySt = x2
// CHECK-APPLY: sil hidden @_TF21transparent_attribute12testPropertyFT1zVS_4MySt_T_
// CHECK: function_ref @_TF21transparent_attributes2x1VS_4MySt
// CHECK-NEXT: apply
// CHECK: function_ref @_TF21transparent_attributes2x2VS_4MySt
// CHECK-NEXT: apply
// CHECK: function_ref @_TF21transparent_attributeg2x1VS_4MySt
// CHECK-NEXT: apply
// CHECK: function_ref @_TF21transparent_attributeg2x2VS_4MySt
// CHECK-NEXT: apply
}
var _tr2 = MySt()
var _tr3 = MySt()
struct MyTranspStruct {}
extension MyTranspStruct {
@_transparent
init(input : MySt) {}
mutating
func tr1() {}
var tr2: MySt {
get {
return _tr2
}
set {
_tr2 = newValue
}
}
}
extension MyTranspStruct {
@_transparent
var tr3: MySt {
get {
return _tr3
}
set {
_tr3 = newValue
}
}
}
func testStructExtension() {
var c : MyTranspStruct = MyTranspStruct(input: _x)
c.tr1()
var s : MySt = c.tr2
var t : MySt = c.tr3
// CHECK-APPLY: sil hidden @_TF21transparent_attribute13testStructExtensionFT_T_
// CHECK: [[INIT:%[0-9]+]] = function_ref @_TFV21transparent_attribute14MyTranspStructC
// CHECK: apply [[INIT]]
// CHECK: [[TR1:%[0-9]+]] = function_ref @_TFV21transparent_attribute14MyTranspStruct3tr1
// CHECK: apply [[TR1]]
// CHECK: [[TR2:%[0-9]+]] = function_ref @_TFV21transparent_attribute14MyTranspStructg3tr2VS_4MySt
// CHECK: apply [[TR2]]
// CHECK: [[TR3:%[0-9]+]] = function_ref @_TFV21transparent_attribute14MyTranspStructg3tr3VS_4MySt
// CHECK: apply [[TR3]]
}
enum MyEnum {
case onetransp
case twotransp
}
extension MyEnum {
@_transparent
func tr3() {}
}
func testEnumExtension() {
MyEnum.onetransp.tr3()
// CHECK-APPLY: sil hidden @_TF21transparent_attribute17testEnumExtensionFT_T_
// CHECK: [[TR3:%[0-9]+]] = function_ref @_TFO21transparent_attribute6MyEnum3tr3
// CHECK: [[INIT:%[0-9]+]] = enum $MyEnum, #MyEnum.onetransp!enumelt
// CHECK: apply [[TR3]]([[INIT]])
}
struct testVarDecl {
@_transparent var max: Int {
get {
return 0xFF
}
mutating
set {
max = 0xFF
}
}
func testVarDeclFoo () {
var z: Int = max
// CHECK-APPLY: sil hidden @_TFV21transparent_attribute11testVarDecl14testVarDeclFoo
// CHECK: [[TR4:%[0-9]+]] = function_ref @_TFV21transparent_attribute11testVarDeclg3maxSi
// CHECK: apply [[TR4]]
}
}
struct testVarDeclShortenedSyntax {
@_transparent static var max: Int { return 0xFF };
func testVarDeclShortenedSyntaxfoo () {
var z: Int = testVarDeclShortenedSyntax.max
// CHECK-APPLY: sil hidden @_TFV21transparent_attribute26testVarDeclShortenedSyntax29testVarDeclShortenedSyntaxfoo
// CHECK: [[TR5:%[0-9]+]] = function_ref @_TZFV21transparent_attribute26testVarDeclShortenedSyntaxg3maxSi
// CHECK: apply [[TR5]]
}
};
@_transparent var transparentOnGlobalVar: Int {
get {
return 0xFF
}
}
// CHECK: sil hidden [transparent] @_TF21transparent_attributeg22transparentOnGlobalVarSi
// Local functions in transparent context are fragile.
@_transparent public func foo() {
// CHECK-LABEL: sil shared [fragile] @_TFF21transparent_attribute3fooFT_T_L_3barFT_T_ : $@convention(thin) () -> ()
func bar() {}
bar()
// CHECK-LABEL: sil shared [fragile] @_TFF21transparent_attribute3fooFT_T_U_FT_T_ : $@convention(thin) () -> () {
let f: () -> () = {}
f()
// CHECK-LABEL: sil shared [fragile] @_TFF21transparent_attribute3fooFT_T_L_3zimFT_T_ : $@convention(thin) () -> () {
func zim() {
// CHECK-LABEL: sil shared [fragile] @_TFFF21transparent_attribute3fooFT_T_L_3zimFT_T_L_4zangFT_T_ : $@convention(thin) () -> () {
func zang() {
}
zang()
}
zim()
}
// Check that @_versioned entities have public linkage.
// CHECK-LABEL: sil @_TF21transparent_attribute25referencedFromTransparentFT_T_ : $@convention(thin) () -> () {
@_versioned func referencedFromTransparent() {}
// CHECK-LABEL: sil [transparent] [fragile] @_TF21transparent_attribute23referencesVersionedFuncFT_FT_T_ : $@convention(thin) () -> @owned @callee_owned () -> () {
@_transparent public func referencesVersionedFunc() -> () -> () {
return referencedFromTransparent
}