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