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