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