]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/CodeGenObjCXX/lambda-expressions.mm
Vendor import of clang trunk r290819:
[FreeBSD/FreeBSD.git] / test / CodeGenObjCXX / lambda-expressions.mm
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks -fobjc-arc | FileCheck -check-prefix=ARC %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks | FileCheck -check-prefix=MRC %s
3
4 typedef int (^fp)();
5 fp f() { auto x = []{ return 3; }; return x; }
6
7 // ARC: %[[LAMBDACLASS:.*]] = type { i32 }
8
9 // MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [5 x i8] c"copy\00"
10 // MRC: @OBJC_METH_VAR_NAME{{.*}} = private unnamed_addr constant [12 x i8] c"autorelease\00"
11 // MRC-LABEL: define i32 ()* @_Z1fv(
12 // MRC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
13 // MRC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
14 // MRC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
15 // MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
16 // MRC: call i32 ()* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i32 ()* (i8*, i8*)*)
17 // MRC: ret i32 ()*
18
19 // ARC-LABEL: define i32 ()* @_Z1fv(
20 // ARC-LABEL: define internal i32 ()* @"_ZZ1fvENK3$_0cvU13block_pointerFivEEv"
21 // ARC: store i8* bitcast (i8** @_NSConcreteStackBlock to i8*)
22 // ARC: store i8* bitcast (i32 (i8*)* @"___ZZ1fvENK3$_0cvU13block_pointerFivEEv_block_invoke" to i8*)
23 // ARC: call i8* @objc_retainBlock
24 // ARC: call i8* @objc_autoreleaseReturnValue
25
26 typedef int (^fp)();
27 fp global;
28 void f2() { global = []{ return 3; }; }
29
30 // MRC: define void @_Z2f2v() [[NUW:#[0-9]+]] {
31 // MRC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
32 // MRC-NOT: call
33 // MRC: ret void
34 // ("global" contains a dangling pointer after this function runs.)
35
36 // ARC: define void @_Z2f2v() [[NUW:#[0-9]+]] {
37 // ARC: store i8* bitcast (i32 (i8*)* @___Z2f2v_block_invoke to i8*),
38 // ARC: call i8* @objc_retainBlock
39 // ARC: call void @objc_release
40 // ARC-LABEL: define internal i32 @___Z2f2v_block_invoke
41 // ARC: call i32 @"_ZZ2f2vENK3$_1clEv
42
43 template <class T> void take_lambda(T &&lambda) { lambda(); }
44 void take_block(void (^block)()) { block(); }
45
46 // rdar://13800041
47 @interface A
48 - (void) test;
49 @end
50 @interface B : A @end
51 @implementation B
52 - (void) test {
53   take_block(^{
54       take_lambda([=]{
55           take_block(^{
56               take_lambda([=] {
57                   [super test];
58               });
59           });
60       });
61    });
62 }
63 @end
64
65 // ARC: define void @_ZN13LambdaCapture4foo1ERi(i32* dereferenceable(4) %{{.*}})
66 // ARC:   %[[CAPTURE0:.*]] = getelementptr inbounds %[[LAMBDACLASS]], %[[LAMBDACLASS]]* %{{.*}}, i32 0, i32 0
67 // ARC:   store i32 %{{.*}}, i32* %[[CAPTURE0]]
68
69 // ARC: define internal void @"_ZZN13LambdaCapture4foo1ERiENK3$_3clEv"(%[[LAMBDACLASS]]* %{{.*}})
70 // ARC:   %[[BLOCK:.*]] = alloca <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>
71 // ARC:   %[[CAPTURE1:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %[[BLOCK]], i32 0, i32 5
72 // ARC:   store i32 %{{.*}}, i32* %[[CAPTURE1]]
73
74 // ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke"
75 // ARC:   %[[CAPTURE2:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5
76 // ARC:   store i32 %{{.*}}, i32* %[[CAPTURE2]]
77
78 // ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke_2"(i8* %{{.*}})
79 // ARC:   %[[CAPTURE3:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5
80 // ARC:   %[[V1:.*]] = load i32, i32* %[[CAPTURE3]]
81 // ARC:   store i32 %[[V1]], i32* @_ZN13LambdaCapture1iE
82
83 namespace LambdaCapture {
84   int i;
85   void foo1(int &a) {
86     auto lambda = [a]{
87       auto block1 = ^{
88         auto block2 = ^{
89           i = a;
90         };
91         block2();
92       };
93       block1();
94     };
95     lambda();
96   }
97 }
98
99 // ARC-LABEL: define linkonce_odr i32 ()* @_ZZNK13StaticMembersIfE1fMUlvE_clEvENKUlvE_cvU13block_pointerFivEEv
100
101 // Check lines for BlockInLambda test below
102 // ARC-LABEL: define internal i32 @___ZZN13BlockInLambda1X1fEvENKUlvE_clEv_block_invoke
103 // ARC: [[Y:%.*]] = getelementptr inbounds %"struct.BlockInLambda::X", %"struct.BlockInLambda::X"* {{.*}}, i32 0, i32 1
104 // ARC-NEXT: [[YVAL:%.*]] = load i32, i32* [[Y]], align 4
105 // ARC-NEXT: ret i32 [[YVAL]]
106
107 typedef int (^fptr)();
108 template<typename T> struct StaticMembers {
109   static fptr f;
110 };
111 template<typename T>
112 fptr StaticMembers<T>::f = [] { auto f = []{return 5;}; return fptr(f); }();
113 template fptr StaticMembers<float>::f;
114
115 namespace BlockInLambda {
116   struct X {
117     int x,y;
118     void f() {
119       [this]{return ^{return y;}();}();
120     };
121   };
122   void g(X& x) {
123     x.f();
124   };
125 }
126
127
128 // ARC: attributes [[NUW]] = { noinline nounwind{{.*}} }
129 // MRC: attributes [[NUW]] = { noinline nounwind{{.*}} }