1 // RUN: %clang_cc1 -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M32 %s 2 // RUN: %clang_cc1 -triple x86_64-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC --check-prefix=M64 %s 3 // RUN: %clang_cc1 -triple i686-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -w | FileCheck --check-prefix=GNU --check-prefix=G32 %s 4 // RUN: %clang_cc1 -triple x86_64-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -w | FileCheck --check-prefix=GNU --check-prefix=G64 %s 5 // RUN: %clang_cc1 -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -fms-compatibility-version=18.00 -emit-llvm -std=c++1y -O1 -disable-llvm-optzns -o - %s -DMSABI -w | FileCheck --check-prefix=MO1 --check-prefix=M18 %s 6 // RUN: %clang_cc1 -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -fms-compatibility-version=19.00 -emit-llvm -std=c++1y -O1 -disable-llvm-optzns -o - %s -DMSABI -w | FileCheck --check-prefix=MO1 --check-prefix=M19 %s 7 // RUN: %clang_cc1 -triple i686-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O1 -o - %s -w | FileCheck --check-prefix=GO1 %s 8 9 // CHECK-NOT doesn't play nice with CHECK-DAG, so use separate run lines. 10 // RUN: %clang_cc1 -triple i686-windows-msvc -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -DMSABI -w | FileCheck --check-prefix=MSC2 %s 11 // RUN: %clang_cc1 -triple i686-windows-gnu -fno-rtti -fno-threadsafe-statics -fms-extensions -emit-llvm -std=c++1y -O0 -o - %s -w | FileCheck --check-prefix=GNU2 %s 12 13 // Helper structs to make templates more expressive. 14 struct ImplicitInst_Imported {}; 15 struct ImplicitInst_NotImported {}; 16 struct ExplicitDecl_Imported {}; 17 struct ExplicitInst_Imported {}; 18 struct ExplicitSpec_Imported {}; 19 struct ExplicitSpec_Def_Imported {}; 20 struct ExplicitSpec_InlineDef_Imported {}; 21 struct ExplicitSpec_NotImported {}; 22 23 #define JOIN2(x, y) x##y 24 #define JOIN(x, y) JOIN2(x, y) 25 #define UNIQ(name) JOIN(name, __LINE__) 26 #define USEVARTYPE(type, var) type UNIQ(use)() { return var; } 27 #define USEVAR(var) USEVARTYPE(int, var) 28 #define USE(func) void UNIQ(use)() { func(); } 29 #define USEMEMFUNC(class, func) void (class::*UNIQ(use)())() { return &class::func; } 30 #define USESTATICMEMFUNC(class, func) void (*UNIQ(use)())() { return &class::func; } 31 #define USECLASS(class) void UNIQ(USE)() { class x; } 32 #define USECOPYASSIGN(class) class& (class::*UNIQ(use)())(class&) { return &class::operator=; } 33 #define USEMOVEASSIGN(class) class& (class::*UNIQ(use)())(class&&) { return &class::operator=; } 34 35 //===----------------------------------------------------------------------===// 36 // Globals 37 //===----------------------------------------------------------------------===// 38 39 // Import declaration. 40 // MSC-DAG: @"\01?ExternGlobalDecl@@3HA" = external dllimport global i32 41 // GNU-DAG: @ExternGlobalDecl = external dllimport global i32 42 __declspec(dllimport) extern int ExternGlobalDecl; 43 USEVAR(ExternGlobalDecl) 44 45 // dllimport implies a declaration. 46 // MSC-DAG: @"\01?GlobalDecl@@3HA" = external dllimport global i32 47 // GNU-DAG: @GlobalDecl = external dllimport global i32 48 __declspec(dllimport) int GlobalDecl; 49 USEVAR(GlobalDecl) 50 51 // Redeclarations 52 // MSC-DAG: @"\01?GlobalRedecl1@@3HA" = external dllimport global i32 53 // GNU-DAG: @GlobalRedecl1 = external dllimport global i32 54 __declspec(dllimport) extern int GlobalRedecl1; 55 __declspec(dllimport) extern int GlobalRedecl1; 56 USEVAR(GlobalRedecl1) 57 58 // MSC-DAG: @"\01?GlobalRedecl2a@@3HA" = external dllimport global i32 59 // GNU-DAG: @GlobalRedecl2a = external dllimport global i32 60 __declspec(dllimport) int GlobalRedecl2a; 61 __declspec(dllimport) int GlobalRedecl2a; 62 USEVAR(GlobalRedecl2a) 63 64 // M32-DAG: @"\01?GlobalRedecl2b@@3PAHA" = external dllimport global i32* 65 // M64-DAG: @"\01?GlobalRedecl2b@@3PEAHEA" = external dllimport global i32* 66 // GNU-DAG: @GlobalRedecl2b = external dllimport global i32* 67 int *__attribute__((dllimport)) GlobalRedecl2b; 68 int *__attribute__((dllimport)) GlobalRedecl2b; 69 USEVARTYPE(int*, GlobalRedecl2b) 70 71 // MSC-DAG: @"\01?GlobalRedecl2c@@3HA" = external dllimport global i32 72 // GNU-DAG: @GlobalRedecl2c = external dllimport global i32 73 int GlobalRedecl2c __attribute__((dllimport)); 74 int GlobalRedecl2c __attribute__((dllimport)); 75 USEVAR(GlobalRedecl2c) 76 77 // NB: MSC issues a warning and makes GlobalRedecl3 dllexport. We follow GCC 78 // and drop the dllimport with a warning. 79 // MSC-DAG: @"\01?GlobalRedecl3@@3HA" = external global i32 80 // GNU-DAG: @GlobalRedecl3 = external global i32 81 __declspec(dllimport) extern int GlobalRedecl3; 82 extern int GlobalRedecl3; // dllimport ignored 83 USEVAR(GlobalRedecl3) 84 85 // MSC-DAG: @"\01?ExternalGlobal@ns@@3HA" = external dllimport global i32 86 // GNU-DAG: @_ZN2ns14ExternalGlobalE = external dllimport global i32 87 namespace ns { __declspec(dllimport) int ExternalGlobal; } 88 USEVAR(ns::ExternalGlobal) 89 90 int __declspec(dllimport) f(); 91 // MO1-DAG: @"\01?x@?1??inlineStaticLocalsFunc@@YAHXZ@4HA" = available_externally dllimport global i32 0 92 // MO1-DAG: @"\01??_B?1??inlineStaticLocalsFunc@@YAHXZ@51" = available_externally dllimport global i32 0 93 inline int __declspec(dllimport) inlineStaticLocalsFunc() { 94 static int x = f(); 95 return x++; 96 }; 97 USE(inlineStaticLocalsFunc); 98 99 // The address of a dllimport global cannot be used in constant initialization. 100 // M32-DAG: @"\01?arr@?1??initializationFunc@@YAPAHXZ@4QBQAHB" = internal global [1 x i32*] zeroinitializer 101 // GNU-DAG: @_ZZ18initializationFuncvE3arr = internal global [1 x i32*] zeroinitializer 102 int *initializationFunc() { 103 static int *const arr[] = {&ExternGlobalDecl}; 104 return arr[0]; 105 } 106 USE(initializationFunc); 107 108 109 //===----------------------------------------------------------------------===// 110 // Variable templates 111 //===----------------------------------------------------------------------===// 112 113 // Import declaration. 114 // MSC-DAG: @"\01??$ExternVarTmplDecl@UImplicitInst_Imported@@@@3HA" = external dllimport global i32 115 // GNU-DAG: @_Z17ExternVarTmplDeclI21ImplicitInst_ImportedE = external dllimport global i32 116 template<typename T> __declspec(dllimport) extern int ExternVarTmplDecl; 117 USEVAR(ExternVarTmplDecl<ImplicitInst_Imported>) 118 119 // dllimport implies a declaration. 120 // MSC-DAG: @"\01??$VarTmplDecl@UImplicitInst_Imported@@@@3HA" = external dllimport global i32 121 // GNU-DAG: @_Z11VarTmplDeclI21ImplicitInst_ImportedE = external dllimport global i32 122 template<typename T> __declspec(dllimport) int VarTmplDecl; 123 USEVAR(VarTmplDecl<ImplicitInst_Imported>) 124 125 // Redeclarations 126 // MSC-DAG: @"\01??$VarTmplRedecl1@UImplicitInst_Imported@@@@3HA" = external dllimport global i32 127 // GNU-DAG: @_Z14VarTmplRedecl1I21ImplicitInst_ImportedE = external dllimport global i32 128 template<typename T> __declspec(dllimport) extern int VarTmplRedecl1; 129 template<typename T> __declspec(dllimport) extern int VarTmplRedecl1; 130 USEVAR(VarTmplRedecl1<ImplicitInst_Imported>) 131 132 // MSC-DAG: @"\01??$VarTmplRedecl2@UImplicitInst_Imported@@@@3HA" = external dllimport global i32 133 // GNU-DAG: @_Z14VarTmplRedecl2I21ImplicitInst_ImportedE = external dllimport global i32 134 template<typename T> __declspec(dllimport) int VarTmplRedecl2; 135 template<typename T> __declspec(dllimport) int VarTmplRedecl2; 136 USEVAR(VarTmplRedecl2<ImplicitInst_Imported>) 137 138 // MSC-DAG: @"\01??$VarTmplRedecl3@UImplicitInst_Imported@@@@3HA" = external global i32 139 // GNU-DAG: @_Z14VarTmplRedecl3I21ImplicitInst_ImportedE = external global i32 140 template<typename T> __declspec(dllimport) extern int VarTmplRedecl3; 141 template<typename T> extern int VarTmplRedecl3; // dllimport ignored 142 USEVAR(VarTmplRedecl3<ImplicitInst_Imported>) 143 144 145 // MSC-DAG: @"\01??$ExternalVarTmpl@UImplicitInst_Imported@@@ns@@3HA" = external dllimport global i32 146 // GNU-DAG: @_ZN2ns15ExternalVarTmplI21ImplicitInst_ImportedEE = external dllimport global i32 147 namespace ns { template<typename T> __declspec(dllimport) int ExternalVarTmpl; } 148 USEVAR(ns::ExternalVarTmpl<ImplicitInst_Imported>) 149 150 151 template<typename T> int VarTmpl; 152 template<typename T> __declspec(dllimport) int ImportedVarTmpl; 153 154 // Import implicit instantiation of an imported variable template. 155 // MSC-DAG: @"\01??$ImportedVarTmpl@UImplicitInst_Imported@@@@3HA" = external dllimport global i32 156 // GNU-DAG: @_Z15ImportedVarTmplI21ImplicitInst_ImportedE = external dllimport global i32 157 USEVAR(ImportedVarTmpl<ImplicitInst_Imported>) 158 159 // Import explicit instantiation declaration of an imported variable template. 160 // MSC-DAG: @"\01??$ImportedVarTmpl@UExplicitDecl_Imported@@@@3HA" = external dllimport global i32 161 // GNU-DAG: @_Z15ImportedVarTmplI21ExplicitDecl_ImportedE = external dllimport global i32 162 extern template int ImportedVarTmpl<ExplicitDecl_Imported>; 163 USEVAR(ImportedVarTmpl<ExplicitDecl_Imported>) 164 165 // An explicit instantiation definition of an imported variable template cannot 166 // be imported because the template must be defined which is illegal. 167 168 // Import specialization of an imported variable template. 169 // MSC-DAG: @"\01??$ImportedVarTmpl@UExplicitSpec_Imported@@@@3HA" = external dllimport global i32 170 // GNU-DAG: @_Z15ImportedVarTmplI21ExplicitSpec_ImportedE = external dllimport global i32 171 template<> __declspec(dllimport) int ImportedVarTmpl<ExplicitSpec_Imported>; 172 USEVAR(ImportedVarTmpl<ExplicitSpec_Imported>) 173 174 // Not importing specialization of an imported variable template without 175 // explicit dllimport. 176 // MSC-DAG: @"\01??$ImportedVarTmpl@UExplicitSpec_NotImported@@@@3HA" = global i32 0, align 4 177 // GNU-DAG: @_Z15ImportedVarTmplI24ExplicitSpec_NotImportedE = global i32 0, align 4 178 template<> int ImportedVarTmpl<ExplicitSpec_NotImported>; 179 USEVAR(ImportedVarTmpl<ExplicitSpec_NotImported>) 180 181 // Import explicit instantiation declaration of a non-imported variable template. 182 // MSC-DAG: @"\01??$VarTmpl@UExplicitDecl_Imported@@@@3HA" = external dllimport global i32 183 // GNU-DAG: @_Z7VarTmplI21ExplicitDecl_ImportedE = external dllimport global i32 184 extern template __declspec(dllimport) int VarTmpl<ExplicitDecl_Imported>; 185 USEVAR(VarTmpl<ExplicitDecl_Imported>) 186 187 // Import explicit instantiation definition of a non-imported variable template. 188 // MSC-DAG: @"\01??$VarTmpl@UExplicitInst_Imported@@@@3HA" = external dllimport global i32 189 // GNU-DAG: @_Z7VarTmplI21ExplicitInst_ImportedE = external dllimport global i32 190 template __declspec(dllimport) int VarTmpl<ExplicitInst_Imported>; 191 USEVAR(VarTmpl<ExplicitInst_Imported>) 192 193 // Import specialization of a non-imported variable template. 194 // MSC-DAG: @"\01??$VarTmpl@UExplicitSpec_Imported@@@@3HA" = external dllimport global i32 195 // GNU-DAG: @_Z7VarTmplI21ExplicitSpec_ImportedE = external dllimport global i32 196 template<> __declspec(dllimport) int VarTmpl<ExplicitSpec_Imported>; 197 USEVAR(VarTmpl<ExplicitSpec_Imported>) 198 199 200 201 //===----------------------------------------------------------------------===// 202 // Functions 203 //===----------------------------------------------------------------------===// 204 205 // Import function declaration. 206 // MSC-DAG: declare dllimport void @"\01?decl@@YAXXZ"() 207 // GNU-DAG: declare dllimport void @_Z4declv() 208 __declspec(dllimport) void decl(); 209 USE(decl) 210 211 // extern "C" 212 // MSC-DAG: declare dllimport void @externC() 213 // GNU-DAG: declare dllimport void @externC() 214 extern "C" __declspec(dllimport) void externC(); 215 USE(externC) 216 217 // Import inline function. 218 // MSC-DAG: declare dllimport void @"\01?inlineFunc@@YAXXZ"() 219 // GNU-DAG: define linkonce_odr void @_Z10inlineFuncv() 220 // MO1-DAG: define available_externally dllimport void @"\01?inlineFunc@@YAXXZ"() 221 // GO1-DAG: define linkonce_odr void @_Z10inlineFuncv() 222 __declspec(dllimport) inline void inlineFunc() {} 223 USE(inlineFunc) 224 225 // MSC-DAG: declare dllimport void @"\01?inlineDecl@@YAXXZ"() 226 // GNU-DAG: define linkonce_odr void @_Z10inlineDeclv() 227 // MO1-DAG: define available_externally dllimport void @"\01?inlineDecl@@YAXXZ"() 228 // GO1-DAG: define linkonce_odr void @_Z10inlineDeclv() 229 __declspec(dllimport) inline void inlineDecl(); 230 void inlineDecl() {} 231 USE(inlineDecl) 232 233 // MSC-DAG: declare dllimport void @"\01?inlineDef@@YAXXZ"() 234 // GNU-DAG: define linkonce_odr void @_Z9inlineDefv() 235 // MO1-DAG: define available_externally dllimport void @"\01?inlineDef@@YAXXZ"() 236 // GO1-DAG: define linkonce_odr void @_Z9inlineDefv() 237 __declspec(dllimport) void inlineDef(); 238 inline void inlineDef() {} 239 USE(inlineDef) 240 241 // inline attributes 242 // MSC-DAG: declare dllimport void @"\01?noinline@@YAXXZ"() 243 // GNU-DAG: define linkonce_odr void @_Z8noinlinev() 244 __declspec(dllimport) __attribute__((noinline)) inline void noinline() {} 245 USE(noinline) 246 247 // MSC2-NOT: @"\01?alwaysInline@@YAXXZ"() 248 // GNU2-NOT: @_Z12alwaysInlinev() 249 __declspec(dllimport) __attribute__((always_inline)) inline void alwaysInline() {} 250 USE(alwaysInline) 251 252 // Redeclarations 253 // MSC-DAG: declare dllimport void @"\01?redecl1@@YAXXZ"() 254 // GNU-DAG: declare dllimport void @_Z7redecl1v() 255 __declspec(dllimport) void redecl1(); 256 __declspec(dllimport) void redecl1(); 257 USE(redecl1) 258 259 // NB: MSC issues a warning and makes redecl2/redecl3 dllexport. We follow GCC 260 // and drop the dllimport with a warning. 261 // MSC-DAG: declare void @"\01?redecl2@@YAXXZ"() 262 // GNU-DAG: declare void @_Z7redecl2v() 263 __declspec(dllimport) void redecl2(); 264 void redecl2(); 265 USE(redecl2) 266 267 // MSC-DAG: define dllexport void @"\01?redecl3@@YAXXZ"() 268 // GNU-DAG: define void @_Z7redecl3v() 269 __declspec(dllimport) void redecl3(); 270 void redecl3() {} // dllimport ignored 271 USE(redecl3) 272 273 274 // Friend functions 275 // MSC-DAG: declare dllimport void @"\01?friend1@@YAXXZ"() 276 // GNU-DAG: declare dllimport void @_Z7friend1v() 277 // MSC-DAG: declare void @"\01?friend2@@YAXXZ"() 278 // GNU-DAG: declare void @_Z7friend2v() 279 // MSC-DAG: define dllexport void @"\01?friend3@@YAXXZ"() 280 // GNU-DAG: define void @_Z7friend3v() 281 // MSC-DAG: declare void @"\01?friend4@@YAXXZ"() 282 // GNU-DAG: declare void @_Z7friend4v() 283 // MSC-DAG: declare dllimport void @"\01?friend5@@YAXXZ"() 284 // GNU-DAG: declare dllimport void @_Z7friend5v() 285 286 struct FuncFriend { 287 friend __declspec(dllimport) void friend1(); 288 friend __declspec(dllimport) void friend2(); 289 friend __declspec(dllimport) void friend3(); 290 }; 291 __declspec(dllimport) void friend1(); 292 void friend2(); // dllimport ignored 293 void friend3() {} // dllimport ignored 294 295 __declspec(dllimport) void friend4(); 296 __declspec(dllimport) void friend5(); 297 struct FuncFriendRedecl { 298 friend void friend4(); // dllimport ignored 299 friend void ::friend5(); 300 }; 301 USE(friend1) 302 USE(friend2) 303 USE(friend3) 304 USE(friend4) 305 USE(friend5) 306 307 // Implicit declarations can be redeclared with dllimport. 308 // MSC-DAG: declare dllimport noalias i8* @"\01??2@{{YAPAXI|YAPEAX_K}}@Z"( 309 // GNU-DAG: declare dllimport noalias i8* @_Znw{{[yj]}}( 310 __declspec(dllimport) void* operator new(__SIZE_TYPE__ n); 311 void UNIQ(use)() { ::operator new(42); } 312 313 // MSC-DAG: declare dllimport void @"\01?externalFunc@ns@@YAXXZ"() 314 // GNU-DAG: declare dllimport void @_ZN2ns12externalFuncEv() 315 namespace ns { __declspec(dllimport) void externalFunc(); } 316 USE(ns::externalFunc) 317 318 // A dllimport function referencing non-imported vars or functions must not be available_externally. 319 __declspec(dllimport) int ImportedVar; 320 int NonImportedVar; 321 __declspec(dllimport) int ImportedFunc(); 322 int NonImportedFunc(); 323 __declspec(dllimport) inline int ReferencingImportedVar() { return ImportedVar; } 324 // MO1-DAG: define available_externally dllimport i32 @"\01?ReferencingImportedVar@@YAHXZ" 325 __declspec(dllimport) inline int ReferencingNonImportedVar() { return NonImportedVar; } 326 // MO1-DAG: declare dllimport i32 @"\01?ReferencingNonImportedVar@@YAHXZ"() 327 __declspec(dllimport) inline int ReferencingImportedFunc() { return ImportedFunc(); } 328 // MO1-DAG: define available_externally dllimport i32 @"\01?ReferencingImportedFunc@@YAHXZ" 329 __declspec(dllimport) inline int ReferencingNonImportedFunc() { return NonImportedFunc(); } 330 // MO1-DAG: declare dllimport i32 @"\01?ReferencingNonImportedFunc@@YAHXZ"() 331 USE(ReferencingImportedVar) 332 USE(ReferencingNonImportedVar) 333 USE(ReferencingImportedFunc) 334 USE(ReferencingNonImportedFunc) 335 // References to operator new and delete count too, despite not being DeclRefExprs. 336 __declspec(dllimport) inline int *ReferencingNonImportedNew() { return new int[2]; } 337 // MO1-DAG: declare dllimport i32* @"\01?ReferencingNonImportedNew@@YAPAHXZ" 338 __declspec(dllimport) inline int *ReferencingNonImportedDelete() { delete (int*)nullptr; } 339 // MO1-DAG: declare dllimport i32* @"\01?ReferencingNonImportedDelete@@YAPAHXZ" 340 USE(ReferencingNonImportedNew) 341 USE(ReferencingNonImportedDelete) 342 __declspec(dllimport) void* operator new[](__SIZE_TYPE__); 343 __declspec(dllimport) void operator delete(void*); 344 __declspec(dllimport) inline int *ReferencingImportedNew() { return new int[2]; } 345 // MO1-DAG: define available_externally dllimport i32* @"\01?ReferencingImportedNew@@YAPAHXZ" 346 __declspec(dllimport) inline int *ReferencingImportedDelete() { delete (int*)nullptr; } 347 // MO1-DAG: define available_externally dllimport i32* @"\01?ReferencingImportedDelete@@YAPAHXZ" 348 USE(ReferencingImportedNew) 349 USE(ReferencingImportedDelete) 350 351 // A dllimport function with a TLS variable must not be available_externally. 352 __declspec(dllimport) inline void FunctionWithTLSVar() { static __thread int x = 42; } 353 // MO1-DAG: declare dllimport void @"\01?FunctionWithTLSVar@@YAXXZ" 354 __declspec(dllimport) inline void FunctionWithNormalVar() { static int x = 42; } 355 // MO1-DAG: define available_externally dllimport void @"\01?FunctionWithNormalVar@@YAXXZ" 356 USE(FunctionWithTLSVar) 357 USE(FunctionWithNormalVar) 358 359 360 //===----------------------------------------------------------------------===// 361 // Function templates 362 //===----------------------------------------------------------------------===// 363 364 // Import function template declaration. 365 // MSC-DAG: declare dllimport void @"\01??$funcTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 366 // GNU-DAG: declare dllimport void @_Z12funcTmplDeclI21ImplicitInst_ImportedEvv() 367 template<typename T> __declspec(dllimport) void funcTmplDecl(); 368 USE(funcTmplDecl<ImplicitInst_Imported>) 369 370 // Function template definitions cannot be imported. 371 372 // Import inline function template. 373 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl1@UImplicitInst_Imported@@@@YAXXZ"() 374 // GNU-DAG: define linkonce_odr void @_Z15inlineFuncTmpl1I21ImplicitInst_ImportedEvv() 375 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl1@UImplicitInst_Imported@@@@YAXXZ"() 376 // GO1-DAG: define linkonce_odr void @_Z15inlineFuncTmpl1I21ImplicitInst_ImportedEvv() 377 template<typename T> __declspec(dllimport) inline void inlineFuncTmpl1() {} 378 USE(inlineFuncTmpl1<ImplicitInst_Imported>) 379 380 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl2@UImplicitInst_Imported@@@@YAXXZ"() 381 // GNU-DAG: define linkonce_odr void @_Z15inlineFuncTmpl2I21ImplicitInst_ImportedEvv() 382 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl2@UImplicitInst_Imported@@@@YAXXZ"() 383 // GO1-DAG: define linkonce_odr void @_Z15inlineFuncTmpl2I21ImplicitInst_ImportedEvv() 384 template<typename T> inline void __attribute__((dllimport)) inlineFuncTmpl2() {} 385 USE(inlineFuncTmpl2<ImplicitInst_Imported>) 386 387 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 388 // GNU-DAG: define linkonce_odr void @_Z18inlineFuncTmplDeclI21ImplicitInst_ImportedEvv() 389 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 390 // GO1-DAG: define linkonce_odr void @_Z18inlineFuncTmplDeclI21ImplicitInst_ImportedEvv() 391 template<typename T> __declspec(dllimport) inline void inlineFuncTmplDecl(); 392 template<typename T> void inlineFuncTmplDecl() {} 393 USE(inlineFuncTmplDecl<ImplicitInst_Imported>) 394 395 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmplDef@UImplicitInst_Imported@@@@YAXXZ"() 396 // GNU-DAG: define linkonce_odr void @_Z17inlineFuncTmplDefI21ImplicitInst_ImportedEvv() 397 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmplDef@UImplicitInst_Imported@@@@YAXXZ"() 398 // GO1-DAG: define linkonce_odr void @_Z17inlineFuncTmplDefI21ImplicitInst_ImportedEvv() 399 template<typename T> __declspec(dllimport) void inlineFuncTmplDef(); 400 template<typename T> inline void inlineFuncTmplDef() {} 401 USE(inlineFuncTmplDef<ImplicitInst_Imported>) 402 403 404 // Redeclarations 405 // MSC-DAG: declare dllimport void @"\01??$funcTmplRedecl1@UImplicitInst_Imported@@@@YAXXZ"() 406 // GNU-DAG: declare dllimport void @_Z15funcTmplRedecl1I21ImplicitInst_ImportedEvv() 407 template<typename T> __declspec(dllimport) void funcTmplRedecl1(); 408 template<typename T> __declspec(dllimport) void funcTmplRedecl1(); 409 USE(funcTmplRedecl1<ImplicitInst_Imported>) 410 411 // MSC-DAG: declare void @"\01??$funcTmplRedecl2@UImplicitInst_NotImported@@@@YAXXZ"() 412 // GNU-DAG: declare void @_Z15funcTmplRedecl2I24ImplicitInst_NotImportedEvv() 413 template<typename T> __declspec(dllimport) void funcTmplRedecl2(); 414 template<typename T> void funcTmplRedecl2(); // dllimport ignored 415 USE(funcTmplRedecl2<ImplicitInst_NotImported>) 416 417 // MSC-DAG: define linkonce_odr void @"\01??$funcTmplRedecl3@UImplicitInst_NotImported@@@@YAXXZ"() 418 // GNU-DAG: define linkonce_odr void @_Z15funcTmplRedecl3I24ImplicitInst_NotImportedEvv() 419 template<typename T> __declspec(dllimport) void funcTmplRedecl3(); 420 template<typename T> void funcTmplRedecl3() {} // dllimport ignored 421 USE(funcTmplRedecl3<ImplicitInst_NotImported>) 422 423 424 // Function template friends 425 // MSC-DAG: declare dllimport void @"\01??$funcTmplFriend1@UImplicitInst_Imported@@@@YAXXZ"() 426 // GNU-DAG: declare dllimport void @_Z15funcTmplFriend1I21ImplicitInst_ImportedEvv() 427 // MSC-DAG: declare void @"\01??$funcTmplFriend2@UImplicitInst_NotImported@@@@YAXXZ"() 428 // GNU-DAG: declare void @_Z15funcTmplFriend2I24ImplicitInst_NotImportedEvv() 429 // MSC-DAG: define linkonce_odr void @"\01??$funcTmplFriend3@UImplicitInst_NotImported@@@@YAXXZ"() 430 // GNU-DAG: define linkonce_odr void @_Z15funcTmplFriend3I24ImplicitInst_NotImportedEvv() 431 // MSC-DAG: declare dllimport void @"\01??$funcTmplFriend4@UImplicitInst_Imported@@@@YAXXZ"() 432 // GNU-DAG: define linkonce_odr void @_Z15funcTmplFriend4I21ImplicitInst_ImportedEvv() 433 struct FuncTmplFriend { 434 template<typename T> friend __declspec(dllimport) void funcTmplFriend1(); 435 template<typename T> friend __declspec(dllimport) void funcTmplFriend2(); 436 template<typename T> friend __declspec(dllimport) void funcTmplFriend3(); 437 template<typename T> friend __declspec(dllimport) inline void funcTmplFriend4(); 438 }; 439 template<typename T> __declspec(dllimport) void funcTmplFriend1(); 440 template<typename T> void funcTmplFriend2(); // dllimport ignored 441 template<typename T> void funcTmplFriend3() {} // dllimport ignored 442 template<typename T> inline void funcTmplFriend4() {} 443 USE(funcTmplFriend1<ImplicitInst_Imported>) 444 USE(funcTmplFriend2<ImplicitInst_NotImported>) 445 USE(funcTmplFriend3<ImplicitInst_NotImported>) 446 USE(funcTmplFriend4<ImplicitInst_Imported>) 447 448 // MSC-DAG: declare dllimport void @"\01??$externalFuncTmpl@UImplicitInst_Imported@@@ns@@YAXXZ"() 449 // GNU-DAG: declare dllimport void @_ZN2ns16externalFuncTmplI21ImplicitInst_ImportedEEvv() 450 namespace ns { template<typename T> __declspec(dllimport) void externalFuncTmpl(); } 451 USE(ns::externalFuncTmpl<ImplicitInst_Imported>) 452 453 454 template<typename T> void funcTmpl() {} 455 template<typename T> inline void inlineFuncTmpl() {} 456 template<typename T> __declspec(dllimport) void importedFuncTmplDecl(); 457 template<typename T> __declspec(dllimport) inline void importedFuncTmpl() {} 458 459 // Import implicit instantiation of an imported function template. 460 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 461 // GNU-DAG: declare dllimport void @_Z20importedFuncTmplDeclI21ImplicitInst_ImportedEvv() 462 USE(importedFuncTmplDecl<ImplicitInst_Imported>) 463 464 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UImplicitInst_Imported@@@@YAXXZ"() 465 // GNU-DAG: define linkonce_odr void @_Z16importedFuncTmplI21ImplicitInst_ImportedEvv() 466 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UImplicitInst_Imported@@@@YAXXZ"() 467 // GO1-DAG: define linkonce_odr void @_Z16importedFuncTmplI21ImplicitInst_ImportedEvv() 468 USE(importedFuncTmpl<ImplicitInst_Imported>) 469 470 // Import explicit instantiation declaration of an imported function template. 471 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 472 // GNU-DAG: declare void @_Z16importedFuncTmplI21ExplicitDecl_ImportedEvv() 473 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 474 // GO1-DAG: define available_externally void @_Z16importedFuncTmplI21ExplicitDecl_ImportedEvv() 475 extern template void importedFuncTmpl<ExplicitDecl_Imported>(); 476 USE(importedFuncTmpl<ExplicitDecl_Imported>) 477 478 // Import explicit instantiation definition of an imported function template. 479 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 480 // GNU-DAG: define weak_odr void @_Z16importedFuncTmplI21ExplicitInst_ImportedEvv() 481 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 482 // GO1-DAG: define weak_odr void @_Z16importedFuncTmplI21ExplicitInst_ImportedEvv() 483 template void importedFuncTmpl<ExplicitInst_Imported>(); 484 USE(importedFuncTmpl<ExplicitInst_Imported>) 485 486 487 // Import specialization of an imported function template. 488 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Imported@@@@YAXXZ"() 489 // GNU-DAG: declare dllimport void @_Z20importedFuncTmplDeclI21ExplicitSpec_ImportedEvv() 490 template<> __declspec(dllimport) void importedFuncTmplDecl<ExplicitSpec_Imported>(); 491 USE(importedFuncTmplDecl<ExplicitSpec_Imported>) 492 493 // MSC-DAG-FIXME: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 494 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 495 #ifdef MSABI 496 //template<> __declspec(dllimport) void importedFuncTmplDecl<ExplicitSpec_Def_Imported>() {} 497 //USE(importedFuncTmplDecl<ExplicitSpec_Def_Imported>) 498 #endif 499 500 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 501 // GNU-DAG: define linkonce_odr void @_Z20importedFuncTmplDeclI31ExplicitSpec_InlineDef_ImportedEvv() 502 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 503 // GO1-DAG: define linkonce_odr void @_Z20importedFuncTmplDeclI31ExplicitSpec_InlineDef_ImportedEvv() 504 template<> __declspec(dllimport) inline void importedFuncTmplDecl<ExplicitSpec_InlineDef_Imported>() {} 505 USE(importedFuncTmplDecl<ExplicitSpec_InlineDef_Imported>) 506 507 508 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Imported@@@@YAXXZ"() 509 // GNU-DAG: declare dllimport void @_Z16importedFuncTmplI21ExplicitSpec_ImportedEvv() 510 template<> __declspec(dllimport) void importedFuncTmpl<ExplicitSpec_Imported>(); 511 USE(importedFuncTmpl<ExplicitSpec_Imported>) 512 513 // MSC-DAG-FIXME: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 514 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 515 #ifdef MSABI 516 //template<> __declspec(dllimport) void importedFuncTmpl<ExplicitSpec_Def_Imported>() {} 517 //USE(importedFuncTmpl<ExplicitSpec_Def_Imported>) 518 #endif 519 520 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 521 // GNU-DAG: define linkonce_odr void @_Z16importedFuncTmplI31ExplicitSpec_InlineDef_ImportedEvv() 522 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 523 // GO1-DAG: define linkonce_odr void @_Z16importedFuncTmplI31ExplicitSpec_InlineDef_ImportedEvv() 524 template<> __declspec(dllimport) inline void importedFuncTmpl<ExplicitSpec_InlineDef_Imported>() {} 525 USE(importedFuncTmpl<ExplicitSpec_InlineDef_Imported>) 526 527 528 // Not importing specialization of an imported function template without 529 // explicit dllimport. 530 // MSC-DAG: define void @"\01??$importedFuncTmpl@UExplicitSpec_NotImported@@@@YAXXZ"() 531 // GNU-DAG: define void @_Z16importedFuncTmplI24ExplicitSpec_NotImportedEvv() 532 template<> void importedFuncTmpl<ExplicitSpec_NotImported>() {} 533 USE(importedFuncTmpl<ExplicitSpec_NotImported>) 534 535 536 // Import explicit instantiation declaration of a non-imported function template. 537 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 538 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 539 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitDecl_ImportedEvv() 540 // GNU-DAG: declare void @_Z14inlineFuncTmplI21ExplicitDecl_ImportedEvv() 541 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 542 // GO1-DAG: define available_externally void @_Z14inlineFuncTmplI21ExplicitDecl_ImportedEvv() 543 extern template __declspec(dllimport) void funcTmpl<ExplicitDecl_Imported>(); 544 extern template __declspec(dllimport) void inlineFuncTmpl<ExplicitDecl_Imported>(); 545 USE(funcTmpl<ExplicitDecl_Imported>) 546 USE(inlineFuncTmpl<ExplicitDecl_Imported>) 547 548 549 // Import explicit instantiation definition of a non-imported function template. 550 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitInst_Imported@@@@YAXXZ"() 551 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 552 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitInst_ImportedEvv() 553 // GNU-DAG: define weak_odr void @_Z14inlineFuncTmplI21ExplicitInst_ImportedEvv() 554 // MO1-DAG: define available_externally dllimport void @"\01??$funcTmpl@UExplicitInst_Imported@@@@YAXXZ"() 555 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 556 // GO1-DAG: define available_externally dllimport void @_Z8funcTmplI21ExplicitInst_ImportedEvv() 557 // GO1-DAG: define weak_odr void @_Z14inlineFuncTmplI21ExplicitInst_ImportedEvv() 558 template __declspec(dllimport) void funcTmpl<ExplicitInst_Imported>(); 559 template __declspec(dllimport) void inlineFuncTmpl<ExplicitInst_Imported>(); 560 USE(funcTmpl<ExplicitInst_Imported>) 561 USE(inlineFuncTmpl<ExplicitInst_Imported>) 562 563 564 // Import specialization of a non-imported function template. 565 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_Imported@@@@YAXXZ"() 566 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitSpec_ImportedEvv() 567 template<> __declspec(dllimport) void funcTmpl<ExplicitSpec_Imported>(); 568 USE(funcTmpl<ExplicitSpec_Imported>) 569 570 // MSC-DAG-FIXME: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 571 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$funcTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 572 #ifdef MSABI 573 //template<> __declspec(dllimport) void funcTmpl<ExplicitSpec_Def_Imported>() {} 574 //USE(funcTmpl<ExplicitSpec_Def_Imported>) 575 #endif 576 577 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 578 // GNU-DAG: define linkonce_odr void @_Z8funcTmplI31ExplicitSpec_InlineDef_ImportedEvv() 579 // MO1-DAG: define available_externally dllimport void @"\01??$funcTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 580 // GO1-DAG: define linkonce_odr void @_Z8funcTmplI31ExplicitSpec_InlineDef_ImportedEvv() 581 template<> __declspec(dllimport) inline void funcTmpl<ExplicitSpec_InlineDef_Imported>() {} 582 USE(funcTmpl<ExplicitSpec_InlineDef_Imported>) 583 584 585 586 //===----------------------------------------------------------------------===// 587 // Classes 588 //===----------------------------------------------------------------------===// 589 590 struct __declspec(dllimport) T { 591 void a() {} 592 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01?a@T@@QAEXXZ" 593 594 static void StaticMethod(); 595 // MSC-DAG: declare dllimport void @"\01?StaticMethod@T@@SAXXZ"() 596 // GNU-DAG: declare dllimport void @_ZN1T12StaticMethodEv() 597 598 static int b; 599 // MO1-DAG: @"\01?b@T@@2HA" = external dllimport global i32 600 601 T& operator=(T&) = default; 602 // MO1-DAG: define available_externally dllimport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@AAU0@@Z" 603 604 T& operator=(T&&) = default; 605 // Note: Don't mark inline move operators dllimport because current MSVC versions don't export them. 606 // M18-DAG: define linkonce_odr x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@$$QAU0@@Z" 607 // M19-DAG: define available_externally dllimport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@$$QAU0@@Z" 608 }; 609 USEMEMFUNC(T, a) 610 USESTATICMEMFUNC(T, StaticMethod) 611 USEVAR(T::b) 612 USECOPYASSIGN(T) 613 USEMOVEASSIGN(T) 614 615 template <typename T> struct __declspec(dllimport) U { void foo() {} }; 616 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01?foo@?$U@H@@QAEXXZ" 617 struct __declspec(dllimport) V : public U<int> { }; 618 USEMEMFUNC(V, foo) 619 620 struct __declspec(dllimport) W { virtual void foo() {} }; 621 USECLASS(W) 622 // vftable: 623 // MO1-DAG: @"\01??_SW@@6B@" = linkonce_odr unnamed_addr constant [1 x i8*] [i8* bitcast (void (%struct.W*)* @"\01?foo@W@@UAEXXZ" to i8*)] 624 // GO1-DAG: @_ZTV1W = available_externally dllimport unnamed_addr constant [3 x i8*] [i8* null, i8* null, i8* bitcast (void (%struct.W*)* @_ZN1W3fooEv to i8*)] 625 626 struct __declspec(dllimport) KeyFuncClass { 627 constexpr KeyFuncClass() {} 628 virtual void foo(); 629 }; 630 extern constexpr KeyFuncClass keyFuncClassVar = {}; 631 // G32-DAG: @_ZTV12KeyFuncClass = external dllimport unnamed_addr constant [3 x i8*] 632 633 struct __declspec(dllimport) X : public virtual W {}; 634 USECLASS(X) 635 // vbtable: 636 // MO1-DAG: @"\01??_8X@@7B@" = available_externally dllimport unnamed_addr constant [2 x i32] [i32 0, i32 4] 637 638 struct __declspec(dllimport) Y { 639 int x; 640 }; 641 642 struct __declspec(dllimport) Z { virtual ~Z() {} }; 643 USECLASS(Z) 644 // User-defined dtor: 645 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01??1Z@@UAE@XZ" 646 647 namespace DontUseDtorAlias { 648 struct __declspec(dllimport) A { ~A(); }; 649 struct __declspec(dllimport) B : A { ~B(); }; 650 inline A::~A() { } 651 inline B::~B() { } 652 // Emit a real definition of B's constructor; don't alias it to A's. 653 // MO1-DAG: available_externally dllimport x86_thiscallcc void @"\01??1B@DontUseDtorAlias@@QAE@XZ" 654 USECLASS(B) 655 } 656 657 namespace Vtordisp { 658 // Don't dllimport the vtordisp. 659 // MO1-DAG: define linkonce_odr x86_thiscallcc void @"\01?f@?$C@H@Vtordisp@@$4PPPPPPPM@A@AEXXZ" 660 661 class Base { 662 virtual void f() {} 663 }; 664 template <typename T> 665 class __declspec(dllimport) C : virtual public Base { 666 public: 667 C() {} 668 virtual void f() {} 669 }; 670 USECLASS(C<int>); 671 } 672 673 namespace ClassTemplateStaticDef { 674 // Regular template static field: 675 template <typename T> struct __declspec(dllimport) S { 676 static int x; 677 }; 678 template <typename T> int S<T>::x; 679 // MSC-DAG: @"\01?x@?$S@H@ClassTemplateStaticDef@@2HA" = external dllimport global i32 680 int f() { return S<int>::x; } 681 682 // Partial class template specialization static field: 683 template <typename A> struct T; 684 template <typename A> struct __declspec(dllimport) T<A*> { 685 static int x; 686 }; 687 template <typename A> int T<A*>::x; 688 // GNU-DAG: @_ZN22ClassTemplateStaticDef1TIPvE1xE = external dllimport global i32 689 int g() { return T<void*>::x; } 690 } 691 692 namespace PR19933 { 693 // Don't dynamically initialize dllimport vars. 694 // MSC2-NOT: @llvm.global_ctors 695 // GNU2-NOT: @llvm.global_ctors 696 697 struct NonPOD { NonPOD(); }; 698 template <typename T> struct A { static NonPOD x; }; 699 template <typename T> NonPOD A<T>::x; 700 template struct __declspec(dllimport) A<int>; 701 USEVARTYPE(NonPOD, A<int>::x); 702 // MSC-DAG: @"\01?x@?$A@H@PR19933@@2UNonPOD@2@A" = external dllimport global %"struct.PR19933::NonPOD" 703 704 int f(); 705 template <typename T> struct B { static int x; }; 706 template <typename T> int B<T>::x = f(); 707 template struct __declspec(dllimport) B<int>; 708 USEVAR(B<int>::x); 709 // MSC-DAG: @"\01?x@?$B@H@PR19933@@2HA" = external dllimport global i32 710 711 constexpr int g() { return 42; } 712 template <typename T> struct C { static int x; }; 713 template <typename T> int C<T>::x = g(); 714 template struct __declspec(dllimport) C<int>; 715 USEVAR(C<int>::x); 716 // MSC-DAG: @"\01?x@?$C@H@PR19933@@2HA" = external dllimport global i32 717 718 template <int I> struct D { static int x, y; }; 719 template <int I> int D<I>::x = I + 1; 720 template <int I> int D<I>::y = I + f(); 721 template struct __declspec(dllimport) D<42>; 722 USEVAR(D<42>::x); 723 USEVAR(D<42>::y); 724 // MSC-DAG: @"\01?x@?$D@$0CK@@PR19933@@2HA" = external dllimport global i32 725 // MSC-DAG: @"\01?y@?$D@$0CK@@PR19933@@2HA" = external dllimport global i32 726 } 727 728 namespace PR21355 { 729 struct __declspec(dllimport) S { 730 virtual ~S(); 731 }; 732 S::~S() {} 733 734 // S::~S is a key function, so we would ordinarily emit a strong definition for 735 // the vtable. However, S is imported, so the vtable should be too. 736 737 // GNU-DAG: @_ZTVN7PR213551SE = available_externally dllimport unnamed_addr constant [4 x i8*] 738 } 739 740 namespace PR21366 { 741 struct __declspec(dllimport) S { 742 void outOfLineMethod(); 743 void inlineMethod() {} 744 inline void anotherInlineMethod(); 745 void outOfClassInlineMethod(); 746 }; 747 void S::anotherInlineMethod() {} 748 inline void S::outOfClassInlineMethod() {} 749 } 750 751 namespace PR27319 { 752 // Make sure we don't assert due to not having checked for operator delete on 753 // the destructor. 754 template <typename> struct A { 755 virtual ~A() = default; 756 }; 757 extern template struct __declspec(dllimport) A<int>; 758 void f() { new A<int>(); } 759 // MO1-DAG: @"\01??_S?$A@H@PR27319@@6B@" = linkonce_odr unnamed_addr constant [1 x i8*] 760 } 761 762 // MS ignores DLL attributes on partial specializations. 763 template <typename T> struct PartiallySpecializedClassTemplate {}; 764 template <typename T> struct __declspec(dllimport) PartiallySpecializedClassTemplate<T*> { void f(); }; 765 USEMEMFUNC(PartiallySpecializedClassTemplate<void*>, f); 766 // M32-DAG: declare x86_thiscallcc void @"\01?f@?$PartiallySpecializedClassTemplate@PAX@@QAEXXZ" 767 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN33PartiallySpecializedClassTemplateIPvE1fEv 768 769 // Attributes on explicit specializations are honored. 770 template <typename T> struct ExplicitlySpecializedClassTemplate {}; 771 template <> struct __declspec(dllimport) ExplicitlySpecializedClassTemplate<void*> { void f(); }; 772 USEMEMFUNC(ExplicitlySpecializedClassTemplate<void*>, f); 773 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?f@?$ExplicitlySpecializedClassTemplate@PAX@@QAEXXZ" 774 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN34ExplicitlySpecializedClassTemplateIPvE1fEv 775 776 // MS inherits DLL attributes to partial specializations. 777 template <typename T> struct __declspec(dllimport) PartiallySpecializedImportedClassTemplate {}; 778 template <typename T> struct PartiallySpecializedImportedClassTemplate<T*> { void f() {} }; 779 USEMEMFUNC(PartiallySpecializedImportedClassTemplate<void*>, f); 780 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$PartiallySpecializedImportedClassTemplate@PAX@@QAEXXZ" 781 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN41PartiallySpecializedImportedClassTemplateIPvE1fEv 782 783 // Attributes on the instantiation take precedence over attributes on the template. 784 template <typename T> struct __declspec(dllexport) ExplicitlyInstantiatedWithDifferentAttr { void f() {} }; 785 template struct __declspec(dllimport) ExplicitlyInstantiatedWithDifferentAttr<int>; 786 USEMEMFUNC(ExplicitlyInstantiatedWithDifferentAttr<int>, f); 787 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitlyInstantiatedWithDifferentAttr@H@@QAEXXZ" 788 789 template <typename T> struct ExplicitInstantiationDeclImportedDefTemplate { void f() {} ExplicitInstantiationDeclImportedDefTemplate() {}}; 790 extern template struct ExplicitInstantiationDeclImportedDefTemplate<int>; 791 template struct __declspec(dllimport) ExplicitInstantiationDeclImportedDefTemplate<int>; 792 USECLASS(ExplicitInstantiationDeclImportedDefTemplate<int>); 793 USEMEMFUNC(ExplicitInstantiationDeclImportedDefTemplate<int>, f); 794 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitInstantiationDeclImportedDefTemplate@H@@QAEXXZ" 795 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc %struct.ExplicitInstantiationDeclImportedDefTemplate* @"\01??0?$ExplicitInstantiationDeclImportedDefTemplate@H@@QAE@XZ" 796 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN44ExplicitInstantiationDeclImportedDefTemplateIiE1fEv 797 798 template <typename T> struct __declspec(dllimport) ExplicitInstantiationDeclExportedDefImportedTemplate { void f() {} ExplicitInstantiationDeclExportedDefImportedTemplate() {} }; 799 extern template struct __declspec(dllimport) ExplicitInstantiationDeclExportedDefImportedTemplate <int>; 800 template struct __declspec(dllexport) ExplicitInstantiationDeclExportedDefImportedTemplate<int>; 801 USECLASS(ExplicitInstantiationDeclExportedDefImportedTemplate<int>); 802 USEMEMFUNC(ExplicitInstantiationDeclExportedDefImportedTemplate<int>, f); 803 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitInstantiationDeclExportedDefImportedTemplate@H@@QAEXXZ" 804 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc %struct.ExplicitInstantiationDeclExportedDefImportedTemplate* @"\01??0?$ExplicitInstantiationDeclExportedDefImportedTemplate@H@@QAE@XZ" 805 806 template <typename T> struct PR23770BaseTemplate { void f() {} }; 807 template <typename T> struct PR23770DerivedTemplate : PR23770BaseTemplate<int> {}; 808 extern template struct PR23770DerivedTemplate<int>; 809 template struct __declspec(dllimport) PR23770DerivedTemplate<int>; 810 USEMEMFUNC(PR23770BaseTemplate<int>, f); 811 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?f@?$PR23770BaseTemplate@H@@QAEXXZ" 812 813 namespace PR27810 { 814 template <class T> 815 struct basic_ostream { 816 struct sentry { 817 sentry() { } 818 void foo() { } 819 }; 820 }; 821 template class __declspec(dllimport) basic_ostream<char>; 822 // The explicit instantiation definition acts as an explicit instantiation 823 // *declaration*, dllimport is not inherited by the inner class, and no 824 // functions are emitted unless they are used. 825 826 USEMEMFUNC(basic_ostream<char>::sentry, foo); 827 // M32-DAG: define linkonce_odr x86_thiscallcc void @"\01?foo@sentry@?$basic_ostream@D@PR27810@@QAEXXZ" 828 // M32-NOT: ??0sentry@?$basic_ostream@D@PR27810@@QAE@XZ 829 } 830 831 namespace PR27811 { 832 template <class T> struct codecvt { 833 virtual ~codecvt() { } 834 }; 835 template class __declspec(dllimport) codecvt<char>; 836 837 // dllimport means this explicit instantiation definition gets treated as a 838 // declaration. Thus, the vtable should not be marked used, and in fact 839 // nothing for this class should be emitted at all since it's not used. 840 // M32-NOT: codecvt 841 } 842 843 //===----------------------------------------------------------------------===// 844 // Classes with template base classes 845 //===----------------------------------------------------------------------===// 846 847 template <typename T> struct ClassTemplate { void func() {} }; 848 template <typename T> struct __declspec(dllexport) ExportedClassTemplate { void func(); }; 849 template <typename T> void ExportedClassTemplate<T>::func() {} 850 template <typename T> struct __declspec(dllimport) ImportedClassTemplate { void func(); }; 851 852 template <typename T> struct ExplicitlySpecializedTemplate { void func() {} }; 853 template <> struct ExplicitlySpecializedTemplate<int> { void func() {} }; 854 template <typename T> struct ExplicitlyExportSpecializedTemplate { void func() {} }; 855 template <> struct __declspec(dllexport) ExplicitlyExportSpecializedTemplate<int> { void func(); }; 856 void ExplicitlyExportSpecializedTemplate<int>::func() {} 857 template <typename T> struct ExplicitlyImportSpecializedTemplate { void func() {} }; 858 template <> struct __declspec(dllimport) ExplicitlyImportSpecializedTemplate<int> { void func(); }; 859 860 template <typename T> struct ExplicitlyInstantiatedTemplate { void func() {} }; 861 template struct ExplicitlyInstantiatedTemplate<int>; 862 template <typename T> struct ExplicitlyExportInstantiatedTemplate { void func(); }; 863 template <typename T> void ExplicitlyExportInstantiatedTemplate<T>::func() {} 864 template struct __declspec(dllexport) ExplicitlyExportInstantiatedTemplate<int>; 865 template <typename T> struct ExplicitlyImportInstantiatedTemplate { void func(); }; 866 template struct __declspec(dllimport) ExplicitlyImportInstantiatedTemplate<int>; 867 868 869 // MS: ClassTemplate<int> gets imported. 870 struct __declspec(dllimport) DerivedFromTemplate : public ClassTemplate<int> {}; 871 USEMEMFUNC(ClassTemplate<int>, func) 872 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?func@?$ClassTemplate@H@@QAEXXZ" 873 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIiE4funcEv 874 875 // ImportedTemplate is explicitly imported. 876 struct __declspec(dllimport) DerivedFromImportedTemplate : public ImportedClassTemplate<int> {}; 877 USEMEMFUNC(ImportedClassTemplate<int>, func) 878 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ImportedClassTemplate@H@@QAEXXZ" 879 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN21ImportedClassTemplateIiE4funcEv 880 881 // ExportedTemplate is explicitly exported. 882 struct __declspec(dllimport) DerivedFromExportedTemplate : public ExportedClassTemplate<int> {}; 883 USEMEMFUNC(ExportedClassTemplate<int>, func) 884 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ExportedClassTemplate@H@@QAEXXZ" 885 // G32-DAG: define weak_odr dllexport x86_thiscallcc void @_ZN21ExportedClassTemplateIiE4funcEv 886 887 // Base class already implicitly instantiated without attribute. 888 struct DerivedFromTemplateD : public ClassTemplate<double> {}; 889 struct __declspec(dllimport) DerivedFromTemplateD2 : public ClassTemplate<double> {}; 890 USEMEMFUNC(ClassTemplate<double>, func) 891 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ClassTemplate@N@@QAEXXZ" 892 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIdE4funcEv 893 894 // MS: Base class already instantiated with dfferent attribute. 895 struct __declspec(dllexport) DerivedFromTemplateB : public ClassTemplate<bool> {}; 896 struct __declspec(dllimport) DerivedFromTemplateB2 : public ClassTemplate<bool> {}; 897 USEMEMFUNC(ClassTemplate<bool>, func) 898 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ClassTemplate@_N@@QAEXXZ" 899 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIbE4funcEv 900 901 // Base class already specialized without dll attribute. 902 struct __declspec(dllimport) DerivedFromExplicitlySpecializedTemplate : public ExplicitlySpecializedTemplate<int> {}; 903 USEMEMFUNC(ExplicitlySpecializedTemplate<int>, func) 904 // M32-DAG: define linkonce_odr x86_thiscallcc void @"\01?func@?$ExplicitlySpecializedTemplate@H@@QAEXXZ" 905 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN29ExplicitlySpecializedTemplateIiE4funcEv 906 907 // Base class alredy specialized with export attribute. 908 struct __declspec(dllimport) DerivedFromExplicitlyExportSpecializedTemplate : public ExplicitlyExportSpecializedTemplate<int> {}; 909 USEMEMFUNC(ExplicitlyExportSpecializedTemplate<int>, func) 910 // M32-DAG: define dllexport x86_thiscallcc void @"\01?func@?$ExplicitlyExportSpecializedTemplate@H@@QAEXXZ" 911 // G32-DAG: define dllexport x86_thiscallcc void @_ZN35ExplicitlyExportSpecializedTemplateIiE4funcEv 912 913 // Base class already specialized with import attribute. 914 struct __declspec(dllimport) DerivedFromExplicitlyImportSpecializedTemplate : public ExplicitlyImportSpecializedTemplate<int> {}; 915 USEMEMFUNC(ExplicitlyImportSpecializedTemplate<int>, func) 916 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitlyImportSpecializedTemplate@H@@QAEXXZ" 917 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN35ExplicitlyImportSpecializedTemplateIiE4funcEv 918 919 // Base class already instantiated without dll attribute. 920 struct __declspec(dllimport) DerivedFromExplicitlyInstantiatedTemplate : public ExplicitlyInstantiatedTemplate<int> {}; 921 USEMEMFUNC(ExplicitlyInstantiatedTemplate<int>, func) 922 // M32-DAG: define weak_odr x86_thiscallcc void @"\01?func@?$ExplicitlyInstantiatedTemplate@H@@QAEXXZ" 923 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN30ExplicitlyInstantiatedTemplateIiE4funcEv 924 925 // Base class already instantiated with export attribute. 926 struct __declspec(dllimport) DerivedFromExplicitlyExportInstantiatedTemplate : public ExplicitlyExportInstantiatedTemplate<int> {}; 927 USEMEMFUNC(ExplicitlyExportInstantiatedTemplate<int>, func) 928 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ExplicitlyExportInstantiatedTemplate@H@@QAEXXZ" 929 // G32-DAG: define weak_odr dllexport x86_thiscallcc void @_ZN36ExplicitlyExportInstantiatedTemplateIiE4funcEv 930 931 // Base class already instantiated with import attribute. 932 struct __declspec(dllimport) DerivedFromExplicitlyImportInstantiatedTemplate : public ExplicitlyImportInstantiatedTemplate<int> {}; 933 USEMEMFUNC(ExplicitlyImportInstantiatedTemplate<int>, func) 934 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitlyImportInstantiatedTemplate@H@@QAEXXZ" 935 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN36ExplicitlyImportInstantiatedTemplateIiE4funcEv 936 937 // MS: A dll attribute propagates through multiple levels of instantiation. 938 template <typename T> struct TopClass { void func() {} }; 939 template <typename T> struct MiddleClass : public TopClass<T> { }; 940 struct __declspec(dllimport) BottomClass : public MiddleClass<int> { }; 941 USEMEMFUNC(TopClass<int>, func) 942 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?func@?$TopClass@H@@QAEXXZ" 943 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN8TopClassIiE4funcEv 944 945 template <typename T> struct ExplicitInstantiationDeclTemplateBase { void func() {} }; 946 extern template struct ExplicitInstantiationDeclTemplateBase<int>; 947 struct __declspec(dllimport) DerivedFromExplicitInstantiationDeclTemplateBase : public ExplicitInstantiationDeclTemplateBase<int> {}; 948 template struct ExplicitInstantiationDeclTemplateBase<int>; 949 USEMEMFUNC(ExplicitInstantiationDeclTemplateBase<int>, func) 950 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitInstantiationDeclTemplateBase@H@@QAEXXZ" 951 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN37ExplicitInstantiationDeclTemplateBaseIiE4funcEv 952 953 template <typename T> struct ExplicitInstantiationDeclTemplateBase2 { void func() {} }; 954 extern template struct ExplicitInstantiationDeclTemplateBase2<int>; 955 struct __declspec(dllimport) DerivedFromExplicitInstantiationDeclTemplateBase2 : public ExplicitInstantiationDeclTemplateBase2<int> {}; 956 template struct __declspec(dllexport) ExplicitInstantiationDeclTemplateBase2<int>; 957 USEMEMFUNC(ExplicitInstantiationDeclTemplateBase2<int>, func) 958 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitInstantiationDeclTemplateBase2@H@@QAEXXZ" 959 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN38ExplicitInstantiationDeclTemplateBase2IiE4funcEv 960