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 { using X = ClassWithDtor; X t[2]; }; 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 struct ClassWithImplicitDtor { __declspec(dllimport) ClassWithImplicitDtor(); ClassWithDtor member; }; 372 __declspec(dllimport) inline void ReferencingDtorThroughDefinition() { ClassWithImplicitDtor x; }; 373 USE(ReferencingDtorThroughDefinition) 374 // MO1-DAG: declare dllimport void @"\01?ReferencingDtorThroughDefinition@@YAXXZ"() 375 __declspec(dllimport) inline void ReferencingDtorThroughTemporary() { ClassWithImplicitDtor(); }; 376 USE(ReferencingDtorThroughTemporary) 377 // MO1-DAG: declare dllimport void @"\01?ReferencingDtorThroughTemporary@@YAXXZ"() 378 379 // A dllimport function with a TLS variable must not be available_externally. 380 __declspec(dllimport) inline void FunctionWithTLSVar() { static __thread int x = 42; } 381 // MO1-DAG: declare dllimport void @"\01?FunctionWithTLSVar@@YAXXZ" 382 __declspec(dllimport) inline void FunctionWithNormalVar() { static int x = 42; } 383 // MO1-DAG: define available_externally dllimport void @"\01?FunctionWithNormalVar@@YAXXZ" 384 USE(FunctionWithTLSVar) 385 USE(FunctionWithNormalVar) 386 387 388 //===----------------------------------------------------------------------===// 389 // Function templates 390 //===----------------------------------------------------------------------===// 391 392 // Import function template declaration. 393 // MSC-DAG: declare dllimport void @"\01??$funcTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 394 // GNU-DAG: declare dllimport void @_Z12funcTmplDeclI21ImplicitInst_ImportedEvv() 395 template<typename T> __declspec(dllimport) void funcTmplDecl(); 396 USE(funcTmplDecl<ImplicitInst_Imported>) 397 398 // Function template definitions cannot be imported. 399 400 // Import inline function template. 401 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl1@UImplicitInst_Imported@@@@YAXXZ"() 402 // GNU-DAG: define linkonce_odr void @_Z15inlineFuncTmpl1I21ImplicitInst_ImportedEvv() 403 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl1@UImplicitInst_Imported@@@@YAXXZ"() 404 // GO1-DAG: define linkonce_odr void @_Z15inlineFuncTmpl1I21ImplicitInst_ImportedEvv() 405 template<typename T> __declspec(dllimport) inline void inlineFuncTmpl1() {} 406 USE(inlineFuncTmpl1<ImplicitInst_Imported>) 407 408 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl2@UImplicitInst_Imported@@@@YAXXZ"() 409 // GNU-DAG: define linkonce_odr void @_Z15inlineFuncTmpl2I21ImplicitInst_ImportedEvv() 410 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl2@UImplicitInst_Imported@@@@YAXXZ"() 411 // GO1-DAG: define linkonce_odr void @_Z15inlineFuncTmpl2I21ImplicitInst_ImportedEvv() 412 template<typename T> inline void __attribute__((dllimport)) inlineFuncTmpl2() {} 413 USE(inlineFuncTmpl2<ImplicitInst_Imported>) 414 415 // MSC-DAG: define linkonce_odr void @"\01??$inlineFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 416 // GNU-DAG: define linkonce_odr void @_Z18inlineFuncTmplDeclI21ImplicitInst_ImportedEvv() 417 // MO1-DAG: define linkonce_odr void @"\01??$inlineFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 418 // GO1-DAG: define linkonce_odr void @_Z18inlineFuncTmplDeclI21ImplicitInst_ImportedEvv() 419 template<typename T> __declspec(dllimport) inline void inlineFuncTmplDecl(); 420 template<typename T> void inlineFuncTmplDecl() {} 421 USE(inlineFuncTmplDecl<ImplicitInst_Imported>) 422 423 // MSC-DAG: define linkonce_odr void @"\01??$inlineFuncTmplDef@UImplicitInst_Imported@@@@YAXXZ"() 424 // GNU-DAG: define linkonce_odr void @_Z17inlineFuncTmplDefI21ImplicitInst_ImportedEvv() 425 // MO1-DAG: define linkonce_odr void @"\01??$inlineFuncTmplDef@UImplicitInst_Imported@@@@YAXXZ"() 426 // GO1-DAG: define linkonce_odr void @_Z17inlineFuncTmplDefI21ImplicitInst_ImportedEvv() 427 template<typename T> __declspec(dllimport) void inlineFuncTmplDef(); 428 template<typename T> inline void inlineFuncTmplDef() {} 429 USE(inlineFuncTmplDef<ImplicitInst_Imported>) 430 431 432 // Redeclarations 433 // MSC-DAG: declare dllimport void @"\01??$funcTmplRedecl1@UImplicitInst_Imported@@@@YAXXZ"() 434 // GNU-DAG: declare dllimport void @_Z15funcTmplRedecl1I21ImplicitInst_ImportedEvv() 435 template<typename T> __declspec(dllimport) void funcTmplRedecl1(); 436 template<typename T> __declspec(dllimport) void funcTmplRedecl1(); 437 USE(funcTmplRedecl1<ImplicitInst_Imported>) 438 439 // MSC-DAG: declare void @"\01??$funcTmplRedecl2@UImplicitInst_NotImported@@@@YAXXZ"() 440 // GNU-DAG: declare void @_Z15funcTmplRedecl2I24ImplicitInst_NotImportedEvv() 441 template<typename T> __declspec(dllimport) void funcTmplRedecl2(); 442 template<typename T> void funcTmplRedecl2(); // dllimport ignored 443 USE(funcTmplRedecl2<ImplicitInst_NotImported>) 444 445 // MSC-DAG: define linkonce_odr void @"\01??$funcTmplRedecl3@UImplicitInst_NotImported@@@@YAXXZ"() 446 // GNU-DAG: define linkonce_odr void @_Z15funcTmplRedecl3I24ImplicitInst_NotImportedEvv() 447 template<typename T> __declspec(dllimport) void funcTmplRedecl3(); 448 template<typename T> void funcTmplRedecl3() {} // dllimport ignored 449 USE(funcTmplRedecl3<ImplicitInst_NotImported>) 450 451 452 // Function template friends 453 // MSC-DAG: declare dllimport void @"\01??$funcTmplFriend1@UImplicitInst_Imported@@@@YAXXZ"() 454 // GNU-DAG: declare dllimport void @_Z15funcTmplFriend1I21ImplicitInst_ImportedEvv() 455 // MSC-DAG: declare void @"\01??$funcTmplFriend2@UImplicitInst_NotImported@@@@YAXXZ"() 456 // GNU-DAG: declare void @_Z15funcTmplFriend2I24ImplicitInst_NotImportedEvv() 457 // MSC-DAG: define linkonce_odr void @"\01??$funcTmplFriend3@UImplicitInst_NotImported@@@@YAXXZ"() 458 // GNU-DAG: define linkonce_odr void @_Z15funcTmplFriend3I24ImplicitInst_NotImportedEvv() 459 // MSC-DAG: define linkonce_odr void @"\01??$funcTmplFriend4@UImplicitInst_Imported@@@@YAXXZ"() 460 // GNU-DAG: define linkonce_odr void @_Z15funcTmplFriend4I21ImplicitInst_ImportedEvv() 461 struct FuncTmplFriend { 462 template<typename T> friend __declspec(dllimport) void funcTmplFriend1(); 463 template<typename T> friend __declspec(dllimport) void funcTmplFriend2(); 464 template<typename T> friend __declspec(dllimport) void funcTmplFriend3(); 465 template<typename T> friend __declspec(dllimport) inline void funcTmplFriend4(); 466 }; 467 template<typename T> __declspec(dllimport) void funcTmplFriend1(); 468 template<typename T> void funcTmplFriend2(); // dllimport ignored 469 template<typename T> void funcTmplFriend3() {} // dllimport ignored 470 template<typename T> inline void funcTmplFriend4() {} 471 USE(funcTmplFriend1<ImplicitInst_Imported>) 472 USE(funcTmplFriend2<ImplicitInst_NotImported>) 473 USE(funcTmplFriend3<ImplicitInst_NotImported>) 474 USE(funcTmplFriend4<ImplicitInst_Imported>) 475 476 // MSC-DAG: declare dllimport void @"\01??$externalFuncTmpl@UImplicitInst_Imported@@@ns@@YAXXZ"() 477 // GNU-DAG: declare dllimport void @_ZN2ns16externalFuncTmplI21ImplicitInst_ImportedEEvv() 478 namespace ns { template<typename T> __declspec(dllimport) void externalFuncTmpl(); } 479 USE(ns::externalFuncTmpl<ImplicitInst_Imported>) 480 481 482 template<typename T> void funcTmpl() {} 483 template<typename T> inline void inlineFuncTmpl() {} 484 template<typename T> __declspec(dllimport) void importedFuncTmplDecl(); 485 template<typename T> __declspec(dllimport) inline void importedFuncTmpl() {} 486 487 // Import implicit instantiation of an imported function template. 488 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UImplicitInst_Imported@@@@YAXXZ"() 489 // GNU-DAG: declare dllimport void @_Z20importedFuncTmplDeclI21ImplicitInst_ImportedEvv() 490 USE(importedFuncTmplDecl<ImplicitInst_Imported>) 491 492 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UImplicitInst_Imported@@@@YAXXZ"() 493 // GNU-DAG: define linkonce_odr void @_Z16importedFuncTmplI21ImplicitInst_ImportedEvv() 494 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UImplicitInst_Imported@@@@YAXXZ"() 495 // GO1-DAG: define linkonce_odr void @_Z16importedFuncTmplI21ImplicitInst_ImportedEvv() 496 USE(importedFuncTmpl<ImplicitInst_Imported>) 497 498 // Import explicit instantiation declaration of an imported function template. 499 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 500 // GNU-DAG: declare void @_Z16importedFuncTmplI21ExplicitDecl_ImportedEvv() 501 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 502 // GO1-DAG: define available_externally void @_Z16importedFuncTmplI21ExplicitDecl_ImportedEvv() 503 extern template void importedFuncTmpl<ExplicitDecl_Imported>(); 504 USE(importedFuncTmpl<ExplicitDecl_Imported>) 505 506 // Import explicit instantiation definition of an imported function template. 507 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 508 // GNU-DAG: define weak_odr void @_Z16importedFuncTmplI21ExplicitInst_ImportedEvv() 509 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 510 // GO1-DAG: define weak_odr void @_Z16importedFuncTmplI21ExplicitInst_ImportedEvv() 511 template void importedFuncTmpl<ExplicitInst_Imported>(); 512 USE(importedFuncTmpl<ExplicitInst_Imported>) 513 514 515 // Import specialization of an imported function template. 516 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Imported@@@@YAXXZ"() 517 // GNU-DAG: declare dllimport void @_Z20importedFuncTmplDeclI21ExplicitSpec_ImportedEvv() 518 template<> __declspec(dllimport) void importedFuncTmplDecl<ExplicitSpec_Imported>(); 519 USE(importedFuncTmplDecl<ExplicitSpec_Imported>) 520 521 // MSC-DAG-FIXME: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 522 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 523 #ifdef MSABI 524 //template<> __declspec(dllimport) void importedFuncTmplDecl<ExplicitSpec_Def_Imported>() {} 525 //USE(importedFuncTmplDecl<ExplicitSpec_Def_Imported>) 526 #endif 527 528 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 529 // GNU-DAG: define linkonce_odr void @_Z20importedFuncTmplDeclI31ExplicitSpec_InlineDef_ImportedEvv() 530 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmplDecl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 531 // GO1-DAG: define linkonce_odr void @_Z20importedFuncTmplDeclI31ExplicitSpec_InlineDef_ImportedEvv() 532 template<> __declspec(dllimport) inline void importedFuncTmplDecl<ExplicitSpec_InlineDef_Imported>() {} 533 USE(importedFuncTmplDecl<ExplicitSpec_InlineDef_Imported>) 534 535 536 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Imported@@@@YAXXZ"() 537 // GNU-DAG: declare dllimport void @_Z16importedFuncTmplI21ExplicitSpec_ImportedEvv() 538 template<> __declspec(dllimport) void importedFuncTmpl<ExplicitSpec_Imported>(); 539 USE(importedFuncTmpl<ExplicitSpec_Imported>) 540 541 // MSC-DAG-FIXME: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 542 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 543 #ifdef MSABI 544 //template<> __declspec(dllimport) void importedFuncTmpl<ExplicitSpec_Def_Imported>() {} 545 //USE(importedFuncTmpl<ExplicitSpec_Def_Imported>) 546 #endif 547 548 // MSC-DAG: declare dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 549 // GNU-DAG: define linkonce_odr void @_Z16importedFuncTmplI31ExplicitSpec_InlineDef_ImportedEvv() 550 // MO1-DAG: define available_externally dllimport void @"\01??$importedFuncTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 551 // GO1-DAG: define linkonce_odr void @_Z16importedFuncTmplI31ExplicitSpec_InlineDef_ImportedEvv() 552 template<> __declspec(dllimport) inline void importedFuncTmpl<ExplicitSpec_InlineDef_Imported>() {} 553 USE(importedFuncTmpl<ExplicitSpec_InlineDef_Imported>) 554 555 556 // Not importing specialization of an imported function template without 557 // explicit dllimport. 558 // MSC-DAG: define void @"\01??$importedFuncTmpl@UExplicitSpec_NotImported@@@@YAXXZ"() 559 // GNU-DAG: define void @_Z16importedFuncTmplI24ExplicitSpec_NotImportedEvv() 560 template<> void importedFuncTmpl<ExplicitSpec_NotImported>() {} 561 USE(importedFuncTmpl<ExplicitSpec_NotImported>) 562 563 564 // Import explicit instantiation declaration of a non-imported function template. 565 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 566 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 567 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitDecl_ImportedEvv() 568 // GNU-DAG: declare void @_Z14inlineFuncTmplI21ExplicitDecl_ImportedEvv() 569 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl@UExplicitDecl_Imported@@@@YAXXZ"() 570 // GO1-DAG: define available_externally void @_Z14inlineFuncTmplI21ExplicitDecl_ImportedEvv() 571 extern template __declspec(dllimport) void funcTmpl<ExplicitDecl_Imported>(); 572 extern template __declspec(dllimport) void inlineFuncTmpl<ExplicitDecl_Imported>(); 573 USE(funcTmpl<ExplicitDecl_Imported>) 574 USE(inlineFuncTmpl<ExplicitDecl_Imported>) 575 576 577 // Import explicit instantiation definition of a non-imported function template. 578 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitInst_Imported@@@@YAXXZ"() 579 // MSC-DAG: declare dllimport void @"\01??$inlineFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 580 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitInst_ImportedEvv() 581 // GNU-DAG: define weak_odr void @_Z14inlineFuncTmplI21ExplicitInst_ImportedEvv() 582 // MO1-DAG: define available_externally dllimport void @"\01??$funcTmpl@UExplicitInst_Imported@@@@YAXXZ"() 583 // MO1-DAG: define available_externally dllimport void @"\01??$inlineFuncTmpl@UExplicitInst_Imported@@@@YAXXZ"() 584 // GO1-DAG: define available_externally dllimport void @_Z8funcTmplI21ExplicitInst_ImportedEvv() 585 // GO1-DAG: define weak_odr void @_Z14inlineFuncTmplI21ExplicitInst_ImportedEvv() 586 template __declspec(dllimport) void funcTmpl<ExplicitInst_Imported>(); 587 template __declspec(dllimport) void inlineFuncTmpl<ExplicitInst_Imported>(); 588 USE(funcTmpl<ExplicitInst_Imported>) 589 USE(inlineFuncTmpl<ExplicitInst_Imported>) 590 591 592 // Import specialization of a non-imported function template. 593 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_Imported@@@@YAXXZ"() 594 // GNU-DAG: declare dllimport void @_Z8funcTmplI21ExplicitSpec_ImportedEvv() 595 template<> __declspec(dllimport) void funcTmpl<ExplicitSpec_Imported>(); 596 USE(funcTmpl<ExplicitSpec_Imported>) 597 598 // MSC-DAG-FIXME: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 599 // MO1-DAG-FIXME: define available_externally dllimport void @"\01??$funcTmpl@UExplicitSpec_Def_Imported@@@@YAXXZ"() 600 #ifdef MSABI 601 //template<> __declspec(dllimport) void funcTmpl<ExplicitSpec_Def_Imported>() {} 602 //USE(funcTmpl<ExplicitSpec_Def_Imported>) 603 #endif 604 605 // MSC-DAG: declare dllimport void @"\01??$funcTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 606 // GNU-DAG: define linkonce_odr void @_Z8funcTmplI31ExplicitSpec_InlineDef_ImportedEvv() 607 // MO1-DAG: define available_externally dllimport void @"\01??$funcTmpl@UExplicitSpec_InlineDef_Imported@@@@YAXXZ"() 608 // GO1-DAG: define linkonce_odr void @_Z8funcTmplI31ExplicitSpec_InlineDef_ImportedEvv() 609 template<> __declspec(dllimport) inline void funcTmpl<ExplicitSpec_InlineDef_Imported>() {} 610 USE(funcTmpl<ExplicitSpec_InlineDef_Imported>) 611 612 613 614 //===----------------------------------------------------------------------===// 615 // Classes 616 //===----------------------------------------------------------------------===// 617 618 struct __declspec(dllimport) T { 619 void a() {} 620 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01?a@T@@QAEXXZ" 621 622 static void StaticMethod(); 623 // MSC-DAG: declare dllimport void @"\01?StaticMethod@T@@SAXXZ"() 624 // GNU-DAG: declare dllimport void @_ZN1T12StaticMethodEv() 625 626 static int b; 627 // MO1-DAG: @"\01?b@T@@2HA" = external dllimport global i32 628 629 T& operator=(T&) = default; 630 // MO1-DAG: define available_externally dllimport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@AAU0@@Z" 631 632 T& operator=(T&&) = default; 633 // Note: Don't mark inline move operators dllimport because current MSVC versions don't export them. 634 // M18-DAG: define linkonce_odr x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@$$QAU0@@Z" 635 // M19-DAG: define available_externally dllimport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.T* @"\01??4T@@QAEAAU0@$$QAU0@@Z" 636 }; 637 USEMEMFUNC(T, a) 638 USESTATICMEMFUNC(T, StaticMethod) 639 USEVAR(T::b) 640 USECOPYASSIGN(T) 641 USEMOVEASSIGN(T) 642 643 template <typename T> struct __declspec(dllimport) U { void foo() {} }; 644 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01?foo@?$U@H@@QAEXXZ" 645 struct __declspec(dllimport) V : public U<int> { }; 646 USEMEMFUNC(V, foo) 647 648 struct __declspec(dllimport) W { virtual void foo() {} }; 649 USECLASS(W) 650 // vftable: 651 // 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*)] } 652 // 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*)] } 653 654 struct __declspec(dllimport) KeyFuncClass { 655 constexpr KeyFuncClass() {} 656 virtual void foo(); 657 }; 658 extern constexpr KeyFuncClass keyFuncClassVar = {}; 659 // G32-DAG: @_ZTV12KeyFuncClass = external dllimport unnamed_addr constant { [3 x i8*] } 660 661 struct __declspec(dllimport) X : public virtual W {}; 662 USECLASS(X) 663 // vbtable: 664 // MO1-DAG: @"\01??_8X@@7B@" = available_externally dllimport unnamed_addr constant [2 x i32] [i32 0, i32 4] 665 666 struct __declspec(dllimport) Y { 667 int x; 668 }; 669 670 struct __declspec(dllimport) Z { virtual ~Z() {} }; 671 USECLASS(Z) 672 // User-defined dtor: 673 // MO1-DAG: define available_externally dllimport x86_thiscallcc void @"\01??1Z@@UAE@XZ" 674 675 namespace DontUseDtorAlias { 676 struct __declspec(dllimport) A { ~A(); }; 677 struct __declspec(dllimport) B : A { ~B(); }; 678 inline A::~A() { } 679 inline B::~B() { } 680 // Emit a real definition of B's constructor; don't alias it to A's. 681 // MO1-DAG: available_externally dllimport x86_thiscallcc void @"\01??1B@DontUseDtorAlias@@QAE@XZ" 682 USECLASS(B) 683 } 684 685 namespace Vtordisp { 686 // Don't dllimport the vtordisp. 687 // MO1-DAG: define linkonce_odr x86_thiscallcc void @"\01?f@?$C@H@Vtordisp@@$4PPPPPPPM@A@AEXXZ" 688 689 class __declspec(dllimport) Base { 690 virtual void f() {} 691 }; 692 template <typename T> 693 class __declspec(dllimport) C : virtual public Base { 694 public: 695 C() {} 696 virtual void f() {} 697 }; 698 USECLASS(C<int>); 699 } 700 701 namespace ClassTemplateStaticDef { 702 // Regular template static field: 703 template <typename T> struct __declspec(dllimport) S { 704 static int x; 705 }; 706 template <typename T> int S<T>::x; 707 // MSC-DAG: @"\01?x@?$S@H@ClassTemplateStaticDef@@2HA" = external dllimport global i32 708 int f() { return S<int>::x; } 709 710 // Partial class template specialization static field: 711 template <typename A> struct T; 712 template <typename A> struct __declspec(dllimport) T<A*> { 713 static int x; 714 }; 715 template <typename A> int T<A*>::x; 716 // GNU-DAG: @_ZN22ClassTemplateStaticDef1TIPvE1xE = external dllimport global i32 717 int g() { return T<void*>::x; } 718 } 719 720 namespace PR19933 { 721 // Don't dynamically initialize dllimport vars. 722 // MSC2-NOT: @llvm.global_ctors 723 // GNU2-NOT: @llvm.global_ctors 724 725 struct NonPOD { NonPOD(); }; 726 template <typename T> struct A { static NonPOD x; }; 727 template <typename T> NonPOD A<T>::x; 728 template struct __declspec(dllimport) A<int>; 729 USEVARTYPE(NonPOD, A<int>::x); 730 // MSC-DAG: @"\01?x@?$A@H@PR19933@@2UNonPOD@2@A" = external dllimport global %"struct.PR19933::NonPOD" 731 732 int f(); 733 template <typename T> struct B { static int x; }; 734 template <typename T> int B<T>::x = f(); 735 template struct __declspec(dllimport) B<int>; 736 USEVAR(B<int>::x); 737 // MSC-DAG: @"\01?x@?$B@H@PR19933@@2HA" = external dllimport global i32 738 739 constexpr int g() { return 42; } 740 template <typename T> struct C { static int x; }; 741 template <typename T> int C<T>::x = g(); 742 template struct __declspec(dllimport) C<int>; 743 USEVAR(C<int>::x); 744 // MSC-DAG: @"\01?x@?$C@H@PR19933@@2HA" = external dllimport global i32 745 746 template <int I> struct D { static int x, y; }; 747 template <int I> int D<I>::x = I + 1; 748 template <int I> int D<I>::y = I + f(); 749 template struct __declspec(dllimport) D<42>; 750 USEVAR(D<42>::x); 751 USEVAR(D<42>::y); 752 // MSC-DAG: @"\01?x@?$D@$0CK@@PR19933@@2HA" = external dllimport global i32 753 // MSC-DAG: @"\01?y@?$D@$0CK@@PR19933@@2HA" = external dllimport global i32 754 } 755 756 namespace PR21355 { 757 struct __declspec(dllimport) S { 758 virtual ~S(); 759 }; 760 S::~S() {} 761 762 // S::~S is a key function, so we would ordinarily emit a strong definition for 763 // the vtable. However, S is imported, so the vtable should be too. 764 765 // GNU-DAG: @_ZTVN7PR213551SE = available_externally dllimport unnamed_addr constant { [4 x i8*] } 766 } 767 768 namespace PR21366 { 769 struct __declspec(dllimport) S { 770 void outOfLineMethod(); 771 void inlineMethod() {} 772 inline void anotherInlineMethod(); 773 void outOfClassInlineMethod(); 774 }; 775 void S::anotherInlineMethod() {} 776 inline void S::outOfClassInlineMethod() {} 777 } 778 779 namespace PR27319 { 780 // Make sure we don't assert due to not having checked for operator delete on 781 // the destructor. 782 template <typename> struct A { 783 virtual ~A() = default; 784 }; 785 extern template struct __declspec(dllimport) A<int>; 786 void f() { new A<int>(); } 787 // MO1-DAG: @"\01??_S?$A@H@PR27319@@6B@" = linkonce_odr unnamed_addr constant { [1 x i8*] } 788 } 789 790 // MS ignores DLL attributes on partial specializations. 791 template <typename T> struct PartiallySpecializedClassTemplate {}; 792 template <typename T> struct __declspec(dllimport) PartiallySpecializedClassTemplate<T*> { void f(); }; 793 USEMEMFUNC(PartiallySpecializedClassTemplate<void*>, f); 794 // M32-DAG: declare x86_thiscallcc void @"\01?f@?$PartiallySpecializedClassTemplate@PAX@@QAEXXZ" 795 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN33PartiallySpecializedClassTemplateIPvE1fEv 796 797 // Attributes on explicit specializations are honored. 798 template <typename T> struct ExplicitlySpecializedClassTemplate {}; 799 template <> struct __declspec(dllimport) ExplicitlySpecializedClassTemplate<void*> { void f(); }; 800 USEMEMFUNC(ExplicitlySpecializedClassTemplate<void*>, f); 801 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?f@?$ExplicitlySpecializedClassTemplate@PAX@@QAEXXZ" 802 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN34ExplicitlySpecializedClassTemplateIPvE1fEv 803 804 // MS inherits DLL attributes to partial specializations. 805 template <typename T> struct __declspec(dllimport) PartiallySpecializedImportedClassTemplate {}; 806 template <typename T> struct PartiallySpecializedImportedClassTemplate<T*> { void f() {} }; 807 USEMEMFUNC(PartiallySpecializedImportedClassTemplate<void*>, f); 808 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$PartiallySpecializedImportedClassTemplate@PAX@@QAEXXZ" 809 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN41PartiallySpecializedImportedClassTemplateIPvE1fEv 810 811 // Attributes on the instantiation take precedence over attributes on the template. 812 template <typename T> struct __declspec(dllexport) ExplicitlyInstantiatedWithDifferentAttr { void f() {} }; 813 template struct __declspec(dllimport) ExplicitlyInstantiatedWithDifferentAttr<int>; 814 USEMEMFUNC(ExplicitlyInstantiatedWithDifferentAttr<int>, f); 815 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitlyInstantiatedWithDifferentAttr@H@@QAEXXZ" 816 817 template <typename T> struct ExplicitInstantiationDeclImportedDefTemplate { void f() {} ExplicitInstantiationDeclImportedDefTemplate() {}}; 818 extern template struct ExplicitInstantiationDeclImportedDefTemplate<int>; 819 template struct __declspec(dllimport) ExplicitInstantiationDeclImportedDefTemplate<int>; 820 USECLASS(ExplicitInstantiationDeclImportedDefTemplate<int>); 821 USEMEMFUNC(ExplicitInstantiationDeclImportedDefTemplate<int>, f); 822 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitInstantiationDeclImportedDefTemplate@H@@QAEXXZ" 823 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc %struct.ExplicitInstantiationDeclImportedDefTemplate* @"\01??0?$ExplicitInstantiationDeclImportedDefTemplate@H@@QAE@XZ" 824 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN44ExplicitInstantiationDeclImportedDefTemplateIiE1fEv 825 826 template <typename T> struct __declspec(dllimport) ExplicitInstantiationDeclExportedDefImportedTemplate { void f() {} ExplicitInstantiationDeclExportedDefImportedTemplate() {} }; 827 extern template struct __declspec(dllimport) ExplicitInstantiationDeclExportedDefImportedTemplate <int>; 828 template struct __declspec(dllexport) ExplicitInstantiationDeclExportedDefImportedTemplate<int>; 829 USECLASS(ExplicitInstantiationDeclExportedDefImportedTemplate<int>); 830 USEMEMFUNC(ExplicitInstantiationDeclExportedDefImportedTemplate<int>, f); 831 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?f@?$ExplicitInstantiationDeclExportedDefImportedTemplate@H@@QAEXXZ" 832 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc %struct.ExplicitInstantiationDeclExportedDefImportedTemplate* @"\01??0?$ExplicitInstantiationDeclExportedDefImportedTemplate@H@@QAE@XZ" 833 834 template <typename T> struct PR23770BaseTemplate { void f() {} }; 835 template <typename T> struct PR23770DerivedTemplate : PR23770BaseTemplate<int> {}; 836 extern template struct PR23770DerivedTemplate<int>; 837 template struct __declspec(dllimport) PR23770DerivedTemplate<int>; 838 USEMEMFUNC(PR23770BaseTemplate<int>, f); 839 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?f@?$PR23770BaseTemplate@H@@QAEXXZ" 840 841 namespace PR27810 { 842 template <class T> 843 struct basic_ostream { 844 struct sentry { 845 sentry() { } 846 void foo() { } 847 }; 848 }; 849 template class __declspec(dllimport) basic_ostream<char>; 850 // The explicit instantiation definition acts as an explicit instantiation 851 // *declaration*, dllimport is not inherited by the inner class, and no 852 // functions are emitted unless they are used. 853 854 USEMEMFUNC(basic_ostream<char>::sentry, foo); 855 // M32-DAG: define linkonce_odr x86_thiscallcc void @"\01?foo@sentry@?$basic_ostream@D@PR27810@@QAEXXZ" 856 // M32-NOT: ??0sentry@?$basic_ostream@D@PR27810@@QAE@XZ 857 } 858 859 namespace PR27811 { 860 template <class T> struct codecvt { 861 virtual ~codecvt() { } 862 }; 863 template class __declspec(dllimport) codecvt<char>; 864 865 // dllimport means this explicit instantiation definition gets treated as a 866 // declaration. Thus, the vtable should not be marked used, and in fact 867 // nothing for this class should be emitted at all since it's not used. 868 // M32-NOT: codecvt 869 } 870 871 //===----------------------------------------------------------------------===// 872 // Classes with template base classes 873 //===----------------------------------------------------------------------===// 874 875 template <typename T> struct ClassTemplate { void func() {} }; 876 template <typename T> struct __declspec(dllexport) ExportedClassTemplate { void func(); }; 877 template <typename T> void ExportedClassTemplate<T>::func() {} 878 template <typename T> struct __declspec(dllimport) ImportedClassTemplate { void func(); }; 879 880 template <typename T> struct ExplicitlySpecializedTemplate { void func() {} }; 881 template <> struct ExplicitlySpecializedTemplate<int> { void func() {} }; 882 template <typename T> struct ExplicitlyExportSpecializedTemplate { void func() {} }; 883 template <> struct __declspec(dllexport) ExplicitlyExportSpecializedTemplate<int> { void func(); }; 884 void ExplicitlyExportSpecializedTemplate<int>::func() {} 885 template <typename T> struct ExplicitlyImportSpecializedTemplate { void func() {} }; 886 template <> struct __declspec(dllimport) ExplicitlyImportSpecializedTemplate<int> { void func(); }; 887 888 template <typename T> struct ExplicitlyInstantiatedTemplate { void func() {} }; 889 template struct ExplicitlyInstantiatedTemplate<int>; 890 template <typename T> struct ExplicitlyExportInstantiatedTemplate { void func(); }; 891 template <typename T> void ExplicitlyExportInstantiatedTemplate<T>::func() {} 892 template struct __declspec(dllexport) ExplicitlyExportInstantiatedTemplate<int>; 893 template <typename T> struct ExplicitlyImportInstantiatedTemplate { void func(); }; 894 template struct __declspec(dllimport) ExplicitlyImportInstantiatedTemplate<int>; 895 896 897 // MS: ClassTemplate<int> gets imported. 898 struct __declspec(dllimport) DerivedFromTemplate : public ClassTemplate<int> {}; 899 USEMEMFUNC(ClassTemplate<int>, func) 900 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?func@?$ClassTemplate@H@@QAEXXZ" 901 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIiE4funcEv 902 903 // ImportedTemplate is explicitly imported. 904 struct __declspec(dllimport) DerivedFromImportedTemplate : public ImportedClassTemplate<int> {}; 905 USEMEMFUNC(ImportedClassTemplate<int>, func) 906 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ImportedClassTemplate@H@@QAEXXZ" 907 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN21ImportedClassTemplateIiE4funcEv 908 909 // ExportedTemplate is explicitly exported. 910 struct __declspec(dllimport) DerivedFromExportedTemplate : public ExportedClassTemplate<int> {}; 911 USEMEMFUNC(ExportedClassTemplate<int>, func) 912 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ExportedClassTemplate@H@@QAEXXZ" 913 // G32-DAG: define weak_odr dllexport x86_thiscallcc void @_ZN21ExportedClassTemplateIiE4funcEv 914 915 // Base class already implicitly instantiated without attribute. 916 struct DerivedFromTemplateD : public ClassTemplate<double> {}; 917 struct __declspec(dllimport) DerivedFromTemplateD2 : public ClassTemplate<double> {}; 918 USEMEMFUNC(ClassTemplate<double>, func) 919 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ClassTemplate@N@@QAEXXZ" 920 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIdE4funcEv 921 922 // MS: Base class already instantiated with dfferent attribute. 923 struct __declspec(dllexport) DerivedFromTemplateB : public ClassTemplate<bool> {}; 924 struct __declspec(dllimport) DerivedFromTemplateB2 : public ClassTemplate<bool> {}; 925 USEMEMFUNC(ClassTemplate<bool>, func) 926 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ClassTemplate@_N@@QAEXXZ" 927 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN13ClassTemplateIbE4funcEv 928 929 // Base class already specialized without dll attribute. 930 struct __declspec(dllimport) DerivedFromExplicitlySpecializedTemplate : public ExplicitlySpecializedTemplate<int> {}; 931 USEMEMFUNC(ExplicitlySpecializedTemplate<int>, func) 932 // M32-DAG: define linkonce_odr x86_thiscallcc void @"\01?func@?$ExplicitlySpecializedTemplate@H@@QAEXXZ" 933 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN29ExplicitlySpecializedTemplateIiE4funcEv 934 935 // Base class alredy specialized with export attribute. 936 struct __declspec(dllimport) DerivedFromExplicitlyExportSpecializedTemplate : public ExplicitlyExportSpecializedTemplate<int> {}; 937 USEMEMFUNC(ExplicitlyExportSpecializedTemplate<int>, func) 938 // M32-DAG: define dllexport x86_thiscallcc void @"\01?func@?$ExplicitlyExportSpecializedTemplate@H@@QAEXXZ" 939 // G32-DAG: define dllexport x86_thiscallcc void @_ZN35ExplicitlyExportSpecializedTemplateIiE4funcEv 940 941 // Base class already specialized with import attribute. 942 struct __declspec(dllimport) DerivedFromExplicitlyImportSpecializedTemplate : public ExplicitlyImportSpecializedTemplate<int> {}; 943 USEMEMFUNC(ExplicitlyImportSpecializedTemplate<int>, func) 944 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitlyImportSpecializedTemplate@H@@QAEXXZ" 945 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN35ExplicitlyImportSpecializedTemplateIiE4funcEv 946 947 // Base class already instantiated without dll attribute. 948 struct __declspec(dllimport) DerivedFromExplicitlyInstantiatedTemplate : public ExplicitlyInstantiatedTemplate<int> {}; 949 USEMEMFUNC(ExplicitlyInstantiatedTemplate<int>, func) 950 // M32-DAG: define weak_odr x86_thiscallcc void @"\01?func@?$ExplicitlyInstantiatedTemplate@H@@QAEXXZ" 951 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN30ExplicitlyInstantiatedTemplateIiE4funcEv 952 953 // Base class already instantiated with export attribute. 954 struct __declspec(dllimport) DerivedFromExplicitlyExportInstantiatedTemplate : public ExplicitlyExportInstantiatedTemplate<int> {}; 955 USEMEMFUNC(ExplicitlyExportInstantiatedTemplate<int>, func) 956 // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ExplicitlyExportInstantiatedTemplate@H@@QAEXXZ" 957 // G32-DAG: define weak_odr dllexport x86_thiscallcc void @_ZN36ExplicitlyExportInstantiatedTemplateIiE4funcEv 958 959 // Base class already instantiated with import attribute. 960 struct __declspec(dllimport) DerivedFromExplicitlyImportInstantiatedTemplate : public ExplicitlyImportInstantiatedTemplate<int> {}; 961 USEMEMFUNC(ExplicitlyImportInstantiatedTemplate<int>, func) 962 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitlyImportInstantiatedTemplate@H@@QAEXXZ" 963 // G32-DAG: declare dllimport x86_thiscallcc void @_ZN36ExplicitlyImportInstantiatedTemplateIiE4funcEv 964 965 // MS: A dll attribute propagates through multiple levels of instantiation. 966 template <typename T> struct TopClass { void func() {} }; 967 template <typename T> struct MiddleClass : public TopClass<T> { }; 968 struct __declspec(dllimport) BottomClass : public MiddleClass<int> { }; 969 USEMEMFUNC(TopClass<int>, func) 970 // M32-DAG: {{declare|define available_externally}} dllimport x86_thiscallcc void @"\01?func@?$TopClass@H@@QAEXXZ" 971 // G32-DAG: define linkonce_odr x86_thiscallcc void @_ZN8TopClassIiE4funcEv 972 973 template <typename T> struct ExplicitInstantiationDeclTemplateBase { void func() {} }; 974 extern template struct ExplicitInstantiationDeclTemplateBase<int>; 975 struct __declspec(dllimport) DerivedFromExplicitInstantiationDeclTemplateBase : public ExplicitInstantiationDeclTemplateBase<int> {}; 976 template struct ExplicitInstantiationDeclTemplateBase<int>; 977 USEMEMFUNC(ExplicitInstantiationDeclTemplateBase<int>, func) 978 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitInstantiationDeclTemplateBase@H@@QAEXXZ" 979 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN37ExplicitInstantiationDeclTemplateBaseIiE4funcEv 980 981 template <typename T> struct ExplicitInstantiationDeclTemplateBase2 { void func() {} }; 982 extern template struct ExplicitInstantiationDeclTemplateBase2<int>; 983 struct __declspec(dllimport) DerivedFromExplicitInstantiationDeclTemplateBase2 : public ExplicitInstantiationDeclTemplateBase2<int> {}; 984 template struct __declspec(dllexport) ExplicitInstantiationDeclTemplateBase2<int>; 985 USEMEMFUNC(ExplicitInstantiationDeclTemplateBase2<int>, func) 986 // M32-DAG: declare dllimport x86_thiscallcc void @"\01?func@?$ExplicitInstantiationDeclTemplateBase2@H@@QAEXXZ" 987 // G32-DAG: define weak_odr x86_thiscallcc void @_ZN38ExplicitInstantiationDeclTemplateBase2IiE4funcEv 988