| 1 |
// Test lambda mangling |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
// { dg-require-weak "" } |
| 4 |
// { dg-options "-fno-inline" } |
| 5 |
|
| 6 |
template<typename F> int algo(F fn) { return fn(); } |
| 7 |
inline void g(int n) { |
| 8 |
int bef(int i = []{ return 1; }()); |
| 9 |
// Default arguments of block-extern function declarations |
| 10 |
// remain in the context of the encloding function body. |
| 11 |
// The closure type is encoded as Z1giEUlvE_. |
| 12 |
// The call operator of that type is _ZZ1giENKUlvE_clEv. |
| 13 |
|
| 14 |
// { dg-final { scan-assembler "_ZZ1giENKUlvE_clEv" } } |
| 15 |
// { dg-final { scan-assembler "weak\[^\n\r\]*_?_ZZ1giENKUlvE_clEv" { target { ! { *-*-darwin* *-*-mingw* *-*-cygwin } } } } } |
| 16 |
|
| 17 |
algo([=]{return n+bef();}); |
| 18 |
// The captured entities do not participate in <lambda-sig> |
| 19 |
// and so this closure type has the same <lambda-sig> as |
| 20 |
// the previous one. It encoding is therefore Z1giEUlvE0_ |
| 21 |
// and the call operator is _ZZ1giENKUlvE0_clEv. The |
| 22 |
// instance of "algo" being called is then |
| 23 |
// _Z4algoIZ1giEUlvE0_EiT_. |
| 24 |
|
| 25 |
// { dg-final { scan-assembler "_Z4algoIZ1giEUlvE0_EiT_" } } |
| 26 |
// { dg-final { scan-assembler "_ZZ1giENKUlvE0_clEv" } } |
| 27 |
|
| 28 |
int i = []{return 1;}(); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
struct S { |
| 33 |
void f(int = |
| 34 |
// Type: ZN1S1fEiiEd0_UlvE_ |
| 35 |
// Operator: _ZZN1S1fEiiEd0_NKUlvE_clEv |
| 36 |
// { dg-final { scan-assembler "_ZZN1S1fEiiEd0_NKUlvE_clEv" } } |
| 37 |
// { dg-final { scan-assembler "weak\[^\n\r\]*_?_ZZN1S1fEiiEd0_NKUlvE_clEv" { target { ! { *-*-darwin* *-*-mingw* *-*-cygwin } } } } } |
| 38 |
[]{return 1;}() |
| 39 |
// Type: ZN1S1fEiiEd0_UlvE0_ |
| 40 |
// Operator: _ZZN1S1fEiiEd0_NKUlvE0_clEv |
| 41 |
// { dg-final { scan-assembler "_ZZN1S1fEiiEd0_NKUlvE0_clEv" } } |
| 42 |
+ []{return 2;}(), |
| 43 |
int = |
| 44 |
// Type: ZN1S1fEiiEd_UlvE_ |
| 45 |
// Operator: _ZZN1S1fEiiEd_NKUlvE_clEv |
| 46 |
// { dg-final { scan-assembler "_ZZN1S1fEiiEd_NKUlvE_clEv" } } |
| 47 |
[]{return 3;}()); |
| 48 |
}; |
| 49 |
|
| 50 |
void bar() |
| 51 |
{ |
| 52 |
// lambdas in non-vague linkage functions have internal linkage. |
| 53 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*bar\[^\n\r\]*Ul" } } |
| 54 |
[]{}(); |
| 55 |
} |
| 56 |
|
| 57 |
// lambdas used in namespace-scope initializers have the linkage of |
| 58 |
// the decl |
| 59 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*_ZNKUlv" } } |
| 60 |
// { dg-final { scan-assembler "weak\[^\n\r\]*variableMUlvE_clEv" { target c++14_down } } } |
| 61 |
// in c++17 and up, this operator() become constexpr, no not emitted |
| 62 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*variableMUlvE_clEv" { target c++17 } } } |
| 63 |
int variable = []{return 1;}(); |
| 64 |
|
| 65 |
// And a template instantiated with such a lambda is also internal. |
| 66 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*algoIUl" } } |
| 67 |
int var2 = algo([]{return 1;}); |
| 68 |
|
| 69 |
// As are lambdas used in non-class-body default arguments. |
| 70 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*function" } } |
| 71 |
void function (int i = []{return 1;}()+[]{return 1;}()); |
| 72 |
|
| 73 |
struct Foo |
| 74 |
{ |
| 75 |
static int Int; |
| 76 |
void Bar(int); |
| 77 |
}; |
| 78 |
|
| 79 |
int Foo::Int = []{return 1;}(); |
| 80 |
// Even default arguments for member functions that appear outside the |
| 81 |
// class body are internal. |
| 82 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*Foo" } } |
| 83 |
void Foo::Bar(int i = []{return 1;}()) {} |
| 84 |
|
| 85 |
// Even default arguments for function templates. |
| 86 |
// { dg-final { scan-assembler-not "weak\[^\n\r\]*fn2\[^\n\r\]*Ulv" } } |
| 87 |
template <class T> |
| 88 |
void fn2 (T t = []{return 1;}()) {} |
| 89 |
|
| 90 |
int main() |
| 91 |
{ |
| 92 |
g(42); |
| 93 |
S().f(); |
| 94 |
function(); |
| 95 |
Foo().Bar(); |
| 96 |
fn2<int>(); |
| 97 |
} |