16fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t 2b4127a28a6dd81852469d05c611a487beb233784Richard Smith// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -disable-llvm-optzns -O3 -emit-llvm -o %t.opt 3134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// RUN: FileCheck --check-prefix=CHECK %s < %t 4134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// RUN: FileCheck --check-prefix=CHECK-OPT %s < %t.opt 57ca464366349e6b197cb521794517fa026259ee4Anders Carlsson 67ca464366349e6b197cb521794517fa026259ee4Anders Carlssonnamespace { 77ca464366349e6b197cb521794517fa026259ee4Anders Carlsson struct A { 87ca464366349e6b197cb521794517fa026259ee4Anders Carlsson virtual void f() { } 97ca464366349e6b197cb521794517fa026259ee4Anders Carlsson }; 107ca464366349e6b197cb521794517fa026259ee4Anders Carlsson} 117ca464366349e6b197cb521794517fa026259ee4Anders Carlsson 12152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlssonvoid f() { A b; } 13152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson 14152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlssonstruct B { 15152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson B(); 16152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson virtual void f(); 17152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson}; 18152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson 19152d4dce59a123b9a103b3087dc3be7f0b71033eAnders CarlssonB::B() { } 20152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson 21691222d5c9de39e785f604c41c872279831a507eAnders Carlssonstruct C : virtual B { 22891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson C(); 23891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson virtual void f() { } 24891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson}; 25891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson 26891c8b739917ec4d171a62ceaefc640115089e7dAnders CarlssonC::C() { } 27891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson 285794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlssonstruct D { 295794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson virtual void f(); 305794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson}; 315794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson 325794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlssonvoid D::f() { } 335794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson 34470fb73d4e09e1dfe62cb545d7fe0e567ac6e8d6Eli Friedmanstatic struct : D { } e; 35470fb73d4e09e1dfe62cb545d7fe0e567ac6e8d6Eli Friedman 36074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// The destructor is the key function. 37bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregortemplate<typename T> 38bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregorstruct E { 39bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor virtual ~E(); 40bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor}; 41bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 42bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregortemplate<typename T> E<T>::~E() { } 43bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 44074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// Anchor is the key function 45bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregortemplate<> 46bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregorstruct E<char> { 47bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor virtual void anchor(); 48bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor}; 49bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 50bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregorvoid E<char>::anchor() { } 51bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 52bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregortemplate struct E<short>; 53bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregorextern template struct E<int>; 54bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 55bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregorvoid use_E() { 56bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor E<int> ei; 57bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor (void)ei; 58bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor E<long> el; 59bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor (void)el; 60bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor} 61bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 62074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// No key function 63074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregortemplate<typename T> 64074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregorstruct F { 65074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor virtual void foo() { } 66074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor}; 67074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 68074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// No key function 69074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregortemplate<> 70074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregorstruct F<char> { 71074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor virtual void foo() { } 72074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor}; 73074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 74074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregortemplate struct F<short>; 75074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregorextern template struct F<int>; 76074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 770c7d32bde03ae90367cb0666cf8614d425290aa3John McCallvoid use_F() { 780c7d32bde03ae90367cb0666cf8614d425290aa3John McCall F<char> fc; 790c7d32bde03ae90367cb0666cf8614d425290aa3John McCall fc.foo(); 80074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor F<int> fi; 816fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor fi.foo(); 82074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor F<long> fl; 83074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor (void)fl; 84074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor} 85074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 86152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson// B has a key function that is not defined in this translation unit so its vtable 87152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson// has external linkage. 88134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1B = external unnamed_addr constant 89152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson 903d640e606165daf2eaf18d52c0697f68daec106aJohn McCall// C has no key function, so its vtable should have weak_odr linkage 913d640e606165daf2eaf18d52c0697f68daec106aJohn McCall// and hidden visibility (rdar://problem/7523229). 92134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1C = linkonce_odr unnamed_addr constant 93134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1C = linkonce_odr constant 94651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1C = linkonce_odr constant 95134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTT1C = linkonce_odr unnamed_addr constant 96891c8b739917ec4d171a62ceaefc640115089e7dAnders Carlsson 975794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson// D has a key function that is defined in this translation unit so its vtable is 985794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson// defined in the translation unit. 99134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1D = unnamed_addr constant 100134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1D = constant 101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1D = constant 1025794c9714a7d8c7eb8003d3e2616ab389ba9e45bAnders Carlsson 103bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// E<char> is an explicit specialization with a key function defined 104bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// in this translation unit, so its vtable should have external 105bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// linkage. 106134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1EIcE = unnamed_addr constant 107134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1EIcE = constant 108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1EIcE = constant 109bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 110bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// E<short> is an explicit template instantiation with a key function 111bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// defined in this translation unit, so its vtable should have 112bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// weak_odr linkage. 113134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1EIsE = weak_odr unnamed_addr constant 114134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1EIsE = weak_odr constant 115651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1EIsE = weak_odr constant 116bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 117074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// F<short> is an explicit template instantiation without a key 118074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// function, so its vtable should have weak_odr linkage 119134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1FIsE = weak_odr unnamed_addr constant 120134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1FIsE = weak_odr constant 121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1FIsE = weak_odr constant 122074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 123bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// E<long> is an implicit template instantiation with a key function 124bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// defined in this translation unit, so its vtable should have 125f502d93b0ea970bfbd897e657f8d940a20984de2Anders Carlsson// linkonce_odr linkage. 126134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1EIlE = linkonce_odr unnamed_addr constant 127134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1EIlE = linkonce_odr constant 128651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1EIlE = linkonce_odr constant 129bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 130074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// F<long> is an implicit template instantiation with no key function, 131f502d93b0ea970bfbd897e657f8d940a20984de2Anders Carlsson// so its vtable should have linkonce_odr linkage. 132134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1FIlE = linkonce_odr unnamed_addr constant 133134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1FIlE = linkonce_odr constant 134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1FIlE = linkonce_odr constant 135074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 136074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor// F<int> is an explicit template instantiation declaration without a 13735d646133ba70e5d44bc2d112ffd5e2ca262dbeeRafael Espindola// key function, so its vtable should have external linkage. 138134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1FIiE = external unnamed_addr constant 139889a6758554c27ca4cf93502cfb5dc788cb47990Rafael Espindola// CHECK-OPT-DAG: @_ZTV1FIiE = external unnamed_addr constant 140074a2cf3c51229b90b77e998463530c9b06470e6Douglas Gregor 141bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// E<int> is an explicit template instantiation declaration. It has a 142c84622ae02283985ae39a0ff2ae99b43d1eebc3fDouglas Gregor// key function that is not instantiated, so we should only reference 143bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor// its vtable, not define it. 144134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1EIiE = external unnamed_addr constant 145889a6758554c27ca4cf93502cfb5dc788cb47990Rafael Espindola// CHECK-OPT-DAG: @_ZTV1EIiE = external unnamed_addr constant 146bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 1476c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman// The anonymous struct for e has no linkage, so the vtable should have 1486c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman// internal linkage. 149134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @"_ZTV3$_0" = internal unnamed_addr constant 150134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @"_ZTS3$_0" = internal constant 151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @"_ZTI3$_0" = internal constant 1526c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman 153152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson// The A vtable should have internal linkage since it is inside an anonymous 154152d4dce59a123b9a103b3087dc3be7f0b71033eAnders Carlsson// namespace. 155134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant 156134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant 157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTIN12_GLOBAL__N_11AE = internal constant 158bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor 1590c7d32bde03ae90367cb0666cf8614d425290aa3John McCall// F<char> is an explicit specialization without a key function, so 160f502d93b0ea970bfbd897e657f8d940a20984de2Anders Carlsson// its vtable should have linkonce_odr linkage. 161134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1FIcE = linkonce_odr unnamed_addr constant 162134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTS1FIcE = linkonce_odr constant 163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// CHECK-DAG: @_ZTI1FIcE = linkonce_odr constant 1640c7d32bde03ae90367cb0666cf8614d425290aa3John McCall 165134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1GIiE = linkonce_odr unnamed_addr constant 166b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbartemplate <typename T> 167b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbarclass G { 168b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbarpublic: 169b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbar G() {} 170b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbar virtual void f0(); 171b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbar virtual void f1(); 172b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbar}; 173b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbartemplate <> 174b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbarvoid G<int>::f1() {} 175b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbartemplate <typename T> 176b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbarvoid G<T>::f0() {} 177b9aefa775158b68c453e4ac0e4e2ada561900d26Daniel Dunbarvoid G_f0() { new G<int>(); } 178d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis 179d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis// H<int> has a key function without a body but it's a template instantiation 180fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// so its VTable must be emitted. 181134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1HIiE = linkonce_odr unnamed_addr constant 182d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidistemplate <typename T> 183d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidisclass H { 184d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidispublic: 185d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis virtual ~H(); 186d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis}; 187d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis 188d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidisvoid use_H() { 189d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis H<int> h; 190d2c47bde3bc0c0debd8a9728781d3375efe22bc5Argyrios Kyrtzidis} 191b4127a28a6dd81852469d05c611a487beb233784Richard Smith 192b4127a28a6dd81852469d05c611a487beb233784Richard Smith// I<int> has an explicit instantiation declaration and needs a VTT and 193889a6758554c27ca4cf93502cfb5dc788cb47990Rafael Espindola// construction vtables. 194b4127a28a6dd81852469d05c611a487beb233784Richard Smith 195134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTV1IIiE = external unnamed_addr constant 196134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-DAG: @_ZTT1IIiE = external unnamed_addr constant 197134bf5d6ea6821afad5d840f594162e129f38413Rafael Espindola// CHECK-NOT: @_ZTC1IIiE 198b4127a28a6dd81852469d05c611a487beb233784Richard Smith// 199889a6758554c27ca4cf93502cfb5dc788cb47990Rafael Espindola// CHECK-OPT-DAG: @_ZTV1IIiE = external unnamed_addr constant 200889a6758554c27ca4cf93502cfb5dc788cb47990Rafael Espindola// CHECK-OPT-DAG: @_ZTT1IIiE = external unnamed_addr constant 201b4127a28a6dd81852469d05c611a487beb233784Richard Smithstruct VBase1 { virtual void f(); }; struct VBase2 : virtual VBase1 {}; 202b4127a28a6dd81852469d05c611a487beb233784Richard Smithtemplate<typename T> 203b4127a28a6dd81852469d05c611a487beb233784Richard Smithstruct I : VBase2 {}; 204b4127a28a6dd81852469d05c611a487beb233784Richard Smithextern template struct I<int>; 205b4127a28a6dd81852469d05c611a487beb233784Richard SmithI<int> i; 206