1// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -emit-llvm -o - %s -fexceptions -std=c++11 -fblocks -fobjc-arc -fobjc-runtime-has-weak -DWEAK_SUPPORTED | 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 4typedef int (^fp)(); 5fp 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* @llvm.objc.retainBlock 24// ARC: call i8* @llvm.objc.autoreleaseReturnValue 25 26typedef int (^fp)(); 27fp global; 28void 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* @llvm.objc.retainBlock 39// ARC: call void @llvm.objc.release 40// ARC-LABEL: define internal i32 @___Z2f2v_block_invoke 41// ARC: call i32 @"_ZZ2f2vENK3$_1clEv 42 43template <class T> void take_lambda(T &&lambda) { lambda(); } 44void 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* nonnull align 4 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-LABEL: define internal void @"_ZZ10-[Foo foo]ENK3$_4clEv"( 75// ARC-NOT: @llvm.objc.storeStrong( 76// ARC: ret void 77 78// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke" 79// ARC: %[[CAPTURE2:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5 80// ARC: store i32 %{{.*}}, i32* %[[CAPTURE2]] 81 82// ARC: define internal void @"___ZZN13LambdaCapture4foo1ERiENK3$_3clEv_block_invoke_2"(i8* %{{.*}}) 83// ARC: %[[CAPTURE3:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32 }>* %{{.*}}, i32 0, i32 5 84// ARC: %[[V1:.*]] = load i32, i32* %[[CAPTURE3]] 85// ARC: store i32 %[[V1]], i32* @_ZN13LambdaCapture1iE 86 87namespace LambdaCapture { 88 int i; 89 void foo1(int &a) { 90 auto lambda = [a]{ 91 auto block1 = ^{ 92 auto block2 = ^{ 93 i = a; 94 }; 95 block2(); 96 }; 97 block1(); 98 }; 99 lambda(); 100 } 101} 102 103// ARC-LABEL: define linkonce_odr i32 ()* @_ZZNK13StaticMembersIfE1fMUlvE_clEvENKUlvE_cvU13block_pointerFivEEv 104 105// Check lines for BlockInLambda test below 106// ARC-LABEL: define internal i32 @___ZZN13BlockInLambda1X1fEvENKUlvE_clEv_block_invoke 107// ARC: [[Y:%.*]] = getelementptr inbounds %"struct.BlockInLambda::X", %"struct.BlockInLambda::X"* {{.*}}, i32 0, i32 1 108// ARC-NEXT: [[YVAL:%.*]] = load i32, i32* [[Y]], align 4 109// ARC-NEXT: ret i32 [[YVAL]] 110 111typedef int (^fptr)(); 112template<typename T> struct StaticMembers { 113 static fptr f; 114}; 115template<typename T> 116fptr StaticMembers<T>::f = [] { auto f = []{return 5;}; return fptr(f); }(); 117template fptr StaticMembers<float>::f; 118 119namespace BlockInLambda { 120 struct X { 121 int x,y; 122 void f() { 123 [this]{return ^{return y;}();}(); 124 }; 125 }; 126 void g(X& x) { 127 x.f(); 128 }; 129} 130 131@interface NSObject @end 132@interface Foo : NSObject @end 133@implementation Foo 134- (void)foo { 135 [&] { 136 ^{ (void)self; }(); 137 }(); 138} 139@end 140 141// Check that the delegating invoke function doesn't destruct the Weak object 142// that is passed. 143 144// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvEN3$_58__invokeENS_4WeakE"( 145// ARC: call void @"_ZZN14LambdaDelegate4testEvENK3$_5clENS_4WeakE"( 146// ARC-NEXT: ret void 147 148// ARC-LABEL: define internal void @"_ZZN14LambdaDelegate4testEvENK3$_5clENS_4WeakE"( 149// ARC: call void @_ZN14LambdaDelegate4WeakD1Ev( 150 151#ifdef WEAK_SUPPORTED 152 153namespace LambdaDelegate { 154 155struct Weak { 156 __weak id x; 157}; 158 159void test() { 160 void (*p)(Weak) = [](Weak a) { }; 161} 162 163}; 164 165#endif 166 167// ARC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} } 168// MRC: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} } 169