xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/mangle-ms-templates-memptrs.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -Wno-microsoft -fno-rtti -std=c++11 -emit-llvm %s -o - -triple=i386-pc-win32 | FileCheck %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc struct U;
4*0a6a1f1dSLionel Sambuc static_assert(sizeof(void (U::*)()) == 2 * sizeof(void*) + 2 * sizeof(int), "");
5*0a6a1f1dSLionel Sambuc 
6*0a6a1f1dSLionel Sambuc struct A { int a; };
7*0a6a1f1dSLionel Sambuc struct B { int b; };
8*0a6a1f1dSLionel Sambuc struct I { union { struct { int a, b; }; }; };
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc struct S             { int a, b; void f(); virtual void g(); };
11*0a6a1f1dSLionel Sambuc struct M : A, B      { int a, b; void f(); virtual void g(); };
12*0a6a1f1dSLionel Sambuc struct V : virtual A { int a, b; void f(); virtual void g(); };
13*0a6a1f1dSLionel Sambuc struct U             { int a, b; void f(); virtual void g(); };
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc struct C        { virtual void f(); };
16*0a6a1f1dSLionel Sambuc struct D        { virtual void g(); };
17*0a6a1f1dSLionel Sambuc struct O : C, D { virtual void g(); }; // override of non-primary
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc // Test data member pointers.
20*0a6a1f1dSLionel Sambuc template <typename T, int T::*F>
ReadField(T & o)21*0a6a1f1dSLionel Sambuc int ReadField(T &o) {
22*0a6a1f1dSLionel Sambuc   return F ? o.*F : 0;
23*0a6a1f1dSLionel Sambuc }
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc // Redeclare some of the classes so that the implicit attribute goes on the most
26*0a6a1f1dSLionel Sambuc // recent redeclaration rather than the definition.
27*0a6a1f1dSLionel Sambuc struct V;
28*0a6a1f1dSLionel Sambuc 
ReadFields()29*0a6a1f1dSLionel Sambuc void ReadFields() {
30*0a6a1f1dSLionel Sambuc   A a;
31*0a6a1f1dSLionel Sambuc   I i;
32*0a6a1f1dSLionel Sambuc   S s;
33*0a6a1f1dSLionel Sambuc   M m;
34*0a6a1f1dSLionel Sambuc   V v;
35*0a6a1f1dSLionel Sambuc   U u;
36*0a6a1f1dSLionel Sambuc   ReadField<S, &S::a>(s);
37*0a6a1f1dSLionel Sambuc   ReadField<M, &M::a>(m);
38*0a6a1f1dSLionel Sambuc   ReadField<V, &V::a>(v);
39*0a6a1f1dSLionel Sambuc   ReadField<U, &U::a>(u);
40*0a6a1f1dSLionel Sambuc   ReadField<S, &S::b>(s);
41*0a6a1f1dSLionel Sambuc   ReadField<M, &M::b>(m);
42*0a6a1f1dSLionel Sambuc   ReadField<V, &V::b>(v);
43*0a6a1f1dSLionel Sambuc   ReadField<U, &U::b>(u);
44*0a6a1f1dSLionel Sambuc   ReadField<S, nullptr>(s);
45*0a6a1f1dSLionel Sambuc   ReadField<M, nullptr>(m);
46*0a6a1f1dSLionel Sambuc   ReadField<V, nullptr>(v);
47*0a6a1f1dSLionel Sambuc   ReadField<U, nullptr>(u);
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc   // Non-polymorphic null data memptr vs first field memptr.
50*0a6a1f1dSLionel Sambuc   ReadField<A, &A::a>(a);
51*0a6a1f1dSLionel Sambuc   ReadField<A, nullptr>(a);
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc   // Indirect fields injected from anonymous unions and structs
54*0a6a1f1dSLionel Sambuc   ReadField<I, &I::a>(i);
55*0a6a1f1dSLionel Sambuc   ReadField<I, &I::b>(i);
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define {{.*}}ReadFields
59*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@US@@$03@@YAHAAUS@@@Z"
60*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UM@@$0M@@@YAHAAUM@@@Z"
61*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UV@@$F7A@@@YAHAAUV@@@Z"
62*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UU@@$G3A@A@@@YAHAAUU@@@Z"
63*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@US@@$07@@YAHAAUS@@@Z"
64*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UM@@$0BA@@@YAHAAUM@@@Z"
65*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UV@@$FM@A@@@YAHAAUV@@@Z"
66*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UU@@$G7A@A@@@YAHAAUU@@@Z"
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc // MSVC mangles null member pointers in function templates wrong, but it gets
69*0a6a1f1dSLionel Sambuc // them right in class templates.
70*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@US@@$0A@@@YAHAAUS@@@Z"
71*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UM@@$0A@@@YAHAAUM@@@Z"
72*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UV@@$FA@?0@@YAHAAUV@@@Z"
73*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UU@@$GA@A@?0@@YAHAAUU@@@Z"
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc // Non-polymorphic null data memptr vs first field memptr.  MSVC mangles these
76*0a6a1f1dSLionel Sambuc // the same.
77*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UA@@$0A@@@YAHAAUA@@@Z"
78*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UA@@$0?0@@YAHAAUA@@@Z"
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc // Indirect fields are handled as-if they were simply members of their enclosing
81*0a6a1f1dSLionel Sambuc // record.
82*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UI@@$0A@@@YAHAAUI@@@Z"
83*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$ReadField@UI@@$03@@YAHAAUI@@@Z"
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc // Test member function pointers.
86*0a6a1f1dSLionel Sambuc template <typename T, void (T::*MFP)()>
CallMethod(T & o)87*0a6a1f1dSLionel Sambuc void CallMethod(T &o) {
88*0a6a1f1dSLionel Sambuc   (o.*MFP)();
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc 
CallMethods()91*0a6a1f1dSLionel Sambuc void CallMethods() {
92*0a6a1f1dSLionel Sambuc   S s;
93*0a6a1f1dSLionel Sambuc   M m;
94*0a6a1f1dSLionel Sambuc   V v;
95*0a6a1f1dSLionel Sambuc   U u;
96*0a6a1f1dSLionel Sambuc   O o;
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc   // Non-virtual methods.
99*0a6a1f1dSLionel Sambuc   CallMethod<S, &S::f>(s);
100*0a6a1f1dSLionel Sambuc   CallMethod<M, &M::f>(m);
101*0a6a1f1dSLionel Sambuc   CallMethod<V, &V::f>(v);
102*0a6a1f1dSLionel Sambuc   CallMethod<U, &U::f>(u);
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc   // Virtual methods requiring thunk mangling.
105*0a6a1f1dSLionel Sambuc   CallMethod<S, &S::g>(s);
106*0a6a1f1dSLionel Sambuc   CallMethod<M, &M::g>(m);
107*0a6a1f1dSLionel Sambuc   CallMethod<V, &V::g>(v);
108*0a6a1f1dSLionel Sambuc   CallMethod<U, &U::g>(u);
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc   // A member pointer for a non-primary vbase will have a non-zero this
111*0a6a1f1dSLionel Sambuc   // adjustment.
112*0a6a1f1dSLionel Sambuc   CallMethod<O, &O::g>(o);
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc   // Null member pointers.
115*0a6a1f1dSLionel Sambuc   CallMethod<S, nullptr>(s);
116*0a6a1f1dSLionel Sambuc   CallMethod<M, nullptr>(m);
117*0a6a1f1dSLionel Sambuc   CallMethod<V, nullptr>(v);
118*0a6a1f1dSLionel Sambuc   CallMethod<U, nullptr>(u);
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define {{.*}}CallMethods
122*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@US@@$1?f@1@QAEXXZ@@YAXAAUS@@@Z"
123*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UM@@$H?f@1@QAEXXZA@@@YAXAAUM@@@Z"
124*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UV@@$I?f@1@QAEXXZA@A@@@YAXAAUV@@@Z"
125*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UU@@$J?f@1@QAEXXZA@A@A@@@YAXAAUU@@@Z"
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc // PR17034: MSVC reuses the same thunk for every virtual g method because they
128*0a6a1f1dSLionel Sambuc // are all at vftable offset zero.  They then mangle the name of the first thunk
129*0a6a1f1dSLionel Sambuc // created into the name of the template instantiation, which is definitely a
130*0a6a1f1dSLionel Sambuc // bug.  We don't follow them here.  Instead of ?_91@ backref below, they would
131*0a6a1f1dSLionel Sambuc // get ?_9S@@ in every instantiation after the first.
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@US@@$1??_91@$BA@AE@@YAXAAUS@@@Z"
134*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UM@@$H??_91@$BA@AEA@@@YAXAAUM@@@Z"
135*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UV@@$I??_91@$BA@AEA@A@@@YAXAAUV@@@Z"
136*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UU@@$J??_91@$BA@AEA@A@A@@@YAXAAUU@@@Z"
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UO@@$H??_91@$BA@AE3@@YAXAAUO@@@Z"
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@US@@$0A@@@YAXAAUS@@@Z"
141*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UM@@$0A@@@YAXAAUM@@@Z"
142*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UV@@$0A@@@YAXAAUV@@@Z"
143*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @"\01??$CallMethod@UU@@$0A@@@YAXAAUU@@@Z"
144