1651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// RUN: %clang_cc1 -Wno-microsoft -fno-rtti -std=c++11 -emit-llvm %s -o - -triple=i386-pc-win32 | FileCheck %s
2651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct U;
4651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic_assert(sizeof(void (U::*)()) == 2 * sizeof(void*) + 2 * sizeof(int), "");
5651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
6651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct A { int a; };
7651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct B { int b; };
8651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct I { union { struct { int a, b; }; }; };
9651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
10651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct S             { int a, b; void f(); virtual void g(); };
11651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct M : A, B      { int a, b; void f(); virtual void g(); };
12651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct V : virtual A { int a, b; void f(); virtual void g(); };
13651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct U             { int a, b; void f(); virtual void g(); };
14651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
15651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct C        { virtual void f(); };
16651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct D        { virtual void g(); };
17651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct O : C, D { virtual void g(); }; // override of non-primary
18651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
19651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// Test data member pointers.
20651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinestemplate <typename T, int T::*F>
21651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesint ReadField(T &o) {
22651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return F ? o.*F : 0;
23651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
24651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
25651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// Redeclare some of the classes so that the implicit attribute goes on the most
26651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// recent redeclaration rather than the definition.
27651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstruct V;
28651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
29651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid ReadFields() {
30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  A a;
31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  I i;
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  S s;
33651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  M m;
34651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  V v;
35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  U u;
36651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<S, &S::a>(s);
37651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<M, &M::a>(m);
38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<V, &V::a>(v);
39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<U, &U::a>(u);
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<S, &S::b>(s);
41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<M, &M::b>(m);
42651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<V, &V::b>(v);
43651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<U, &U::b>(u);
44651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<S, nullptr>(s);
45651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<M, nullptr>(m);
46651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<V, nullptr>(v);
47651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<U, nullptr>(u);
48651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
49651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Non-polymorphic null data memptr vs first field memptr.
50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<A, &A::a>(a);
51651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<A, nullptr>(a);
52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Indirect fields injected from anonymous unions and structs
54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<I, &I::a>(i);
55651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ReadField<I, &I::b>(i);
56651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
57651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
58651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-LABEL: define {{.*}}ReadFields
59651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@US@@$03@@YAHAAUS@@@Z"
60651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UM@@$0M@@@YAHAAUM@@@Z"
61651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UV@@$F7A@@@YAHAAUV@@@Z"
62651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UU@@$G3A@A@@@YAHAAUU@@@Z"
63651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@US@@$07@@YAHAAUS@@@Z"
64651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UM@@$0BA@@@YAHAAUM@@@Z"
65651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UV@@$FM@A@@@YAHAAUV@@@Z"
66651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UU@@$G7A@A@@@YAHAAUU@@@Z"
67651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
68651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// MSVC mangles null member pointers in function templates wrong, but it gets
69651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// them right in class templates.
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@US@@$0A@@@YAHAAUS@@@Z"
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UM@@$0A@@@YAHAAUM@@@Z"
72a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// CHECK: call {{.*}} @"\01??$ReadField@UV@@$0A@@@YAHAAUV@@@Z"
73a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// CHECK: call {{.*}} @"\01??$ReadField@UU@@$0A@@@YAHAAUU@@@Z"
74651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
75651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// Non-polymorphic null data memptr vs first field memptr.  MSVC mangles these
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// the same.
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UA@@$0A@@@YAHAAUA@@@Z"
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UA@@$0?0@@YAHAAUA@@@Z"
79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// Indirect fields are handled as-if they were simply members of their enclosing
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// record.
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UI@@$0A@@@YAHAAUI@@@Z"
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$ReadField@UI@@$03@@YAHAAUI@@@Z"
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// Test member function pointers.
86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinestemplate <typename T, void (T::*MFP)()>
87651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid CallMethod(T &o) {
88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  (o.*MFP)();
89651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesvoid CallMethods() {
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  S s;
93651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  M m;
94651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  V v;
95651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  U u;
96651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  O o;
97651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
98651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Non-virtual methods.
99651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<S, &S::f>(s);
100651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<M, &M::f>(m);
101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<V, &V::f>(v);
102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<U, &U::f>(u);
103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Virtual methods requiring thunk mangling.
105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<S, &S::g>(s);
106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<M, &M::g>(m);
107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<V, &V::g>(v);
108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<U, &U::g>(u);
109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // A member pointer for a non-primary vbase will have a non-zero this
111651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // adjustment.
112651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<O, &O::g>(o);
113651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
114651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Null member pointers.
115651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<S, nullptr>(s);
116651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<M, nullptr>(m);
117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<V, nullptr>(v);
118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CallMethod<U, nullptr>(u);
119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-LABEL: define {{.*}}CallMethods
122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@US@@$1?f@1@QAEXXZ@@YAXAAUS@@@Z"
123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UM@@$H?f@1@QAEXXZA@@@YAXAAUM@@@Z"
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UV@@$I?f@1@QAEXXZA@A@@@YAXAAUV@@@Z"
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UU@@$J?f@1@QAEXXZA@A@A@@@YAXAAUU@@@Z"
126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// PR17034: MSVC reuses the same thunk for every virtual g method because they
128651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// are all at vftable offset zero.  They then mangle the name of the first thunk
129651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// created into the name of the template instantiation, which is definitely a
130651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// bug.  We don't follow them here.  Instead of ?_91@ backref below, they would
131651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// get ?_9S@@ in every instantiation after the first.
132651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@US@@$1??_91@$BA@AE@@YAXAAUS@@@Z"
134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UM@@$H??_91@$BA@AEA@@@YAXAAUM@@@Z"
135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UV@@$I??_91@$BA@AEA@A@@@YAXAAUV@@@Z"
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UU@@$J??_91@$BA@AEA@A@A@@@YAXAAUU@@@Z"
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UO@@$H??_91@$BA@AE3@@YAXAAUO@@@Z"
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@US@@$0A@@@YAXAAUS@@@Z"
141651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UM@@$0A@@@YAXAAUM@@@Z"
142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UV@@$0A@@@YAXAAUV@@@Z"
143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK: call {{.*}} @"\01??$CallMethod@UU@@$0A@@@YAXAAUU@@@Z"
144a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
145a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarnamespace NegativeNVOffset {
146a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstruct A {};
147a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstruct B : virtual A {};
148a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstruct C : B {
149a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  virtual void f();
150a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar};
151a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
152a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
153a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainartemplate void CallMethod<NegativeNVOffset::C, &NegativeNVOffset::C::f>(NegativeNVOffset::C &);
154a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
155a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// CHECK-LABEL: define {{.*}} @"\01??$CallMethod@UC@NegativeNVOffset@@$I??_912@$BA@AEPPPPPPPM@A@@@YAXAAUC@NegativeNVOffset@@@Z"
156