Rebase.
[official-gcc.git] / gcc / testsuite / g++.dg / cpp0x / lambda / lambda-mangle.C
blob220817a733ff7628de94849b508c4f84ceb15d9a
1 // Test lambda mangling
2 // { dg-require-weak "" }
3 // { dg-do compile { target c++11 } }
4 // { dg-options "-fno-inline" }
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.
14 // { dg-final { scan-assembler "_ZZ1giENKUlvE_clEv" } }
15 // { dg-final { scan-assembler "weak\[^\n\r\]*_?_ZZ1giENKUlvE_clEv" { target { ! { *-*-darwin* *-*-mingw* *-*-cygwin } } } } }
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_.
25 // { dg-final { scan-assembler "_Z4algoIZ1giEUlvE0_EiT_" } }
26 // { dg-final { scan-assembler "_ZZ1giENKUlvE0_clEv" } }
28   int i = []{return 1;}();
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;}());
50 template<typename T> struct R {
51   static int x;
53 template<typename T> int R<T>::x = []{return 1;}();
54 template int R<int>::x;
55 // Type of lambda in intializer of R<int>::x: N1RIiE1xMUlvE_E
56 // Corresponding operator(): _ZNK1RIiE1xMUlvE_clEv
57 // { dg-final { scan-assembler "_ZNK1RIiE1xMUlvE_clEv" } }
58 // { dg-final { scan-assembler "weak\[^\n\r\]*_?_ZNK1RIiE1xMUlvE_clEv" { target { ! { *-*-mingw* *-*-cygwin } } } } }
60 void bar()
62   // lambdas in non-vague linkage functions have internal linkage.
63   // { dg-final { scan-assembler-not "weak\[^\n\r\]*bar\[^\n\r\]*Ul" } }
64   []{}();
67 // lambdas used in non-template, non-class body initializers are internal.
68 // { dg-final { scan-assembler-not "weak\[^\n\r\]*_ZNKUlv" } }
69 // { dg-final { scan-assembler-not "weak\[^\n\r\]*variable" } }
70 int variable = []{return 1;}();
72 // And a template instantiated with such a lambda is also internal.
73 // { dg-final { scan-assembler-not "weak\[^\n\r\]*algoIUl" } }
74 int var2 = algo([]{return 1;});
76 // As are lambdas used in non-class-body default arguments.
77 // { dg-final { scan-assembler-not "weak\[^\n\r\]*function" } }
78 void function (int i = []{return 1;}()+[]{return 1;}());
80 struct Foo
82   static int Int;
83   void Bar(int);
86 int Foo::Int = []{return 1;}();
87 // Even default arguments for member functions that appear outside the
88 // class body are internal.
89 // { dg-final { scan-assembler-not "weak\[^\n\r\]*Foo" } }
90 void Foo::Bar(int i = []{return 1;}()) {}
92 // Even default arguments for function templates.
93 // { dg-final { scan-assembler-not "weak\[^\n\r\]*fn2\[^\n\r\]*Ulv" } }
94 template <class T>
95 void fn2 (T t = []{return 1;}()) {}
97 int main()
99   g(42);
100   S().f();
101   function();
102   Foo().Bar();
103   fn2<int>();